Java Program which uses MySql running on other machine - java

I am new to programming world, and now I am writing program in Java, which connects to my internal(?!) mysql server, where is a database called testDB and table called testTable. So I am wondering, what I need to do if I want to run that program on a different machine, where is no mysql server installed/running? I bet this is not easy to make, but I really want to know how to do it - curiosity is killing me. Thank you.

You would need to change the jdbc URL from localhost to the DBs hostname
e.g.
jdbc:mysql://dbhost:3306/dbname?user=admin&password=secret

For this, you have to write the "IP address of that machine" in the connection string instead of writing localhost in it.
like : jdbc:mysql://[IP-Address]:3306/dbname

Besides the fact that you should change the JDBC connection string from (probably)
jdbc:mysql://localhost:3306/testDB
or
jdbc:mysql://127.0.0.1:3306/testDB
To
jdbc:mysql://IP_OF_MACHINE:3306/testDB
or
jdbc:mysql://NAME_OF_MACHINE:3306/testDB
You must make sure that the machine were the mysql server is running allows TCP connections on port 3306 (default port for mysql DB). Besides this, you must make sure that the mysql engine allows connections from other machines as well.

Related

How to connect to SQL Server 2017 on a network computer (That is listening to a local port)

I would like to connect to a database that runs locally on one of our network computers here at work. I can connect to it just fine with the application that I developed that uses Java's sql driver manager. Now I would like to distribute it to the different computers on the network. To do this, I need a url that will point to the database through the network. The database is listening to port 1434. (Static URL string is "//localhost:1434")
MatsysUI.setConnection(DriverManager.getConnection("jdbc:sqlserver:" + MatsysIO.getStaticURL(), txtUser.getText(), txtPassword.getText()));
Problem is, I don't know where to start to find that, and I would like to avoid using an internet connection to connect to this database. Is there a way to route the connection to the network computer, then to its local port?
There are several possible answers to this.
I just want to restate your situation to make sure I understand:
You have an application, written in Java, which requires access to a SQL Server database.
You want to distribute multiple copies of that application to different client machines.
The client machines and the database server are all on a local network.
SQL Server is listening on port 1434
You need to construct the JDBC connection string on the client machines to access SQL Server.
You give the current connection string as //localhost:1434; in that scheme, localhost is the hostname. You can replace this with the fully qualified domain name of the SQL Server machine (this is almost certainly the simplest option). Using the FQDN allows you to replace the machine, or load balance it, etc. without worrying about the connections - but if the machine name ever changes, all your connections break!
You can also connect by IP address - this allows you to change the machine name, but obviously means the IP address can never change. In most circumstances, that's a bad thing.
Your final option is to look for alL SQL Servers on your network, and show them in a drop down for your user (your code suggests you're using username and password inputs). This means you don't have to distribute a new properties file if you want to change your server details.

How to connect multiple computers to the same PostgreSQL database?

I am working in an application that connect to a PostgreSQL Database and will allow to access from differents computers connected in the same local network.
It's already working, but when it is used in more than one computer at the same time, the server disconnect the current computer to attend to a new connection.
There's some way to make that the PostgreSQL server attend to more than one computer at time?
I think that maybe, I'm doing something wrong in the way that I'm using the server.
When the application start I set the pgdata and pgport variables and check the server status with pg_isready, if it has no answer, I use pg_ctl start.
I'm using the 3389 port.
EDIT:
My problem was a logic error in the application, it was trying to open a new server with each connection instead of use the one that was already running.
The configuration file 'C:\Program Files\PostgreSQL\Y.X\data\postgresql.conf' has a parameter called max_connections, which controls the maximun numbers of connections allowed to your DB. Change it and you can allow more connections after restarting postgres.

How to get Microsoft SQL server port programatically

I'm trying to connect to a Microsoft SQL server via java, and I have no guarantees as to what the port number will be.
There are methods that I have found for getting the port number, but they all seem to not work or require signifigant user interraction: How to find the port for MS SQL Server 2008?
(The netstat method only works if a session is already open to the SQL server, which is not guaranteed)
The simplest one perhaps would be the registry, but there are many places in the registry to look based on the "instance name" and if there is a simpler way I would love to know it.

IO Error: The Network Adapter could not establish the connection

I am new to Oracle, and am trying to run a simple example code with Java, but am getting this error when executing the code.. I am able to start up the listener via CMD and am also able to run SQL Plus. Can anyone give me a hand and tell me what I might be doing wrong?
Update:
I am using JDBC.
Database is local, and I actually had it working but it stopped working just today. I'm not really sure why though. Would you mind giving me some procedures to follow by since I don't know much.
Either:
The database isn't running
You got the URL wrong
There is a firewall in the way.
(This strange error message is produced by Oracle's JDBC driver when it can't connect to the database server. 'Network adapter' appears to refer to some component of their code, which isn't very useful. Real network adapters (NICs) don't establish connections at all: TCP protocol stacks do that. It would have been a lot more useful if they had just let the original ConnectException be thrown, or at least used its error message and let it appear in the stack trace.)
I had the same problem, and this is how I fixed it.
I was using the wrong port for my connection.
private final String DB_URL = "jdbc:oracle:thin:#localhost:1521:orcll"; // 1521 my wrong port
go to your localhost
(my localhost address) : https://localhost:1158/em
login
user name
password
connect as --> normal
Below 'General' click on LISTENER_localhost
look at you port number
Net Address (ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1522))
Connect to port 1522
Edit you connection
change port 1521 to 1522.
done
Another thing you might want to check that the listener.ora file matches the way you are trying to connect to the DB. If you were connecting via a localhost reference and your listener.ora file got changed from:
HOST = localhost
to
HOST = 192.168.XX.XX
then this can cause the error that you had unless you update your hosts file to accommodate for this. Someone might have made this change to allow for remote connections to the DB from other machines.
I figured out that in my case, my database was in different subnet than the subnet from where i was trying to access the db.
I had this error when i renamed the pc in the windows-properties. The pc-name must be updated in the listener.ora-file
Most probably you have listener configured wrongly, the hostname you specify in connection string must be the same as in the listener.
First check the Firewall and network related issues.
Check if Oracle Listener service is available and running. If not you may use Oracle Net Configuration Assistant tool to add and register new listener.
If the above steps are ok then you need to configure Oracle Listener appropriately. You may use Oracle Net Manager tool or edit “%ORACLE_HOME%\network\admin\listener.ora” file manually.
There are 2 options that need to be considered carefully:
Listening Locations associated with the Listener – Hostname(IP) and Port in Listening Location must exactly match the ones used in the connection string.
For example, if you use 192.168.74.139 as target hostname, then there must be Listening Location registered with the same IP address.
Also make sure the you use the same SID as indicated in Database Service associated with the Listener.
https://adhoctuts.com/fix-oracle-io-error-the-network-adapter-could-not-establish-the-connection-error/
IO Error: The Network Adapter could not establish the connection (CONNECTION_ID=iKQM6lBbSLiArrYuDqud8A==)
if you are facing this issue
1- make sure you have downloaded oracle databases like oracle 11g,19c, 21c, or any latest databases.
2- search for services in your computer or type win+r then services.mis then search for oracleservice you will find orcl or xe or any other sid like oracleserviceorcl;
after that you can test your connection using sql developer, sql plus or cmd
To resolve the Network Adapter Error I had to remove the - in the name of the computer name.
In my case, I needed to specify a viahost and viauser. Worth trying if you're in a complex system. :)
For me the basic oracle only was not installed. Please ensure you have oracle installed and then try checking host and port.
I was having issues with this as well. I was using the jdbc connection string to connect to the database. The hostname was incorrectly configured in the string. I am using Mac, and the same string was being used on Windows machines without an issue. On my connection string, I had to make sure that I had the full url with the appending "organizationname.com" to the end of the hostname.
Hope this helps.
Just try to re-create connection. In my situation one of jdbc connection stopped working for no reason. From console sqlplus was working ok.
It took me 2 hours to realize that If i create the same connection - it works.

Whats my hostname on my local MySQL server?

I just set up a MySQL server on my PC for testing Java with JDBC.
At the moment "localhost" works perfectly as hostname for my applications, when running them on the same system.
However what would be the hostname for my MySQL server for applications that are running on different computers? Something like "my_ip:port" would work? I was thinking of writing an applet, which I could upload on a web server and try to connect to my database here. Is it possible to achieve that?
Something like "my_ip:port" would work?
If the MySQL instance has bound to your public interface, and if your firewall allows it, yes. If you connect to the 'net via a router that does NAT (for instance, a combined DSL modem and wireless router allowing you to connect multiple computers), you'll have to set up forwarding rules in the router to tell it which of the local machines to forward requests to.
You don't have to use an IP address. Your machine will also probably have a host name of some kind (either one you've assigned or, if you connect through an ISP, more likely one they've assigned). That would work too.
I was thinking of writing an applet, which I could upload on a web server and try to connect to my database here. Is it possible to achieve that?
With a signed Java applet, yes; otherwise, no. That's because the security sandbox that Java applets run in doesn't let them access servers other than the one they were loaded from (the web server).
A much better approach is to have your client-side code (Java applet, or just DHTML+Ajax stuff) talk to server-side code on the web server, which in turn talks to your DB. That way, the DB is never directly exposed to the outside world, and you don't have to do things like signed applets.
You can always use the ip address of the server running mysql as the hostname or its fully qualified domain name.
That should work, but you also should consider port-forwarding through your firewall.
Go here to get your IP: http://www.whatsmyip.org/
The port is the port mysql is setup on.

Categories