How to get Microsoft SQL server port programatically - java

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.

Related

How do I use port forwarding to connect to an Oracle database using netsh?

I have a machine(A) that has access to the host running the Oracle listener. I can connect to it fine using SQL Developer.
I have another machine(B) that I would also like to make a connection to the same database but its doesn't have direct access to the host running the listener. However, it does have access to machine (A).
I would like to forward a port on machine A to the Oracle listener host which is listening on port 1521.
Then I should be able to make a database connection from machine B to machine A and then forward a port to the host that has the Oracle listener. Sort of a man in the middle.
However, I'm running into issues. I can't even connect to the Oracle database from machine B using the forwarded port. I have experience using ssh tunnels to do the same thing. In this case, I'm not using ssh and I don't understand why this will not work.
Below is the procedure I'm going through:
Log into machine B - Oracle client is installed. SQL Developer is installed.
Open SQL Developer - Select TNS connection. Enter in credentials. Connection Successful
TNS Names entry:
CRYSTAL=(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=yes)(FAILOVER=ON)(ADDRESS=(PROTOCOL=TCP)(HOST=testhost.com)(PORT=1521)))(CONNECT_DATA=(service_name=svc1)))
Now setup forwarding:
C:\Windows\system32>netsh interface portproxy add v4tov4 listenaddress=127.0.0.1
listenport=1521 connectaddress=testhost.com connectport=1521
protocol=tcp
If I telnet localhost 1521 , it is able to make a connection.
As a simple test, I make sure I can connect on Machine B using SQL Developer. But this time, I set the connection type to advanced so I can enter the jdbc url.Note, I use 127.0.01 instead of testhost.com because it will be forwarded to testhost.com
jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=yes)(FAILOVER=ON)(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)))(CONNECT_DATA=(service_name=svc1)))
This connection fails with "ORA-12516, TNS: listener could not find available handler with matching protocol stack"
I've checked for firewall issues and made sure port 1521 is open.
Any ideas what I might be missing or has anyone else had success forwarding a port to an Oracle listener?
Thanks to the comment from psaraj12, I knew my approach should work. The problem I was having was due to the host name I was given to connect to the database. It works fine if you enter it into SQL Developer as is. However, when I used it in the port forwarding, I could not connect to the database using SQL Developer using the host name localhost. The host name I was given had "scan" in the name. I think this is a Virtual IP. We use Oracle RAC which is a clustering technology. So I suspected to that the VIP selected a node on the cluster and the forwarder could not handle it. So this is how I solved the problem:
Installed Wireshark.
Start recording packets.
Used SQL Developer To Connect with the VIP - Success
In Wireshark, find the TCP Stream that contains something unique about the connection. In this case, the Oracle service name. You should see the IP of the VIP. Something like:
.........6.,.A ...O........:..............................(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=11.3.68.171)(PORT=1521))(CONNECT_DATA=(CID=(PROGRAM=SQL Developer)(HOST=jdbc)(USER=adpc))(SERVICE_NAME=svc1)(CID=(PROGRAM=SQL Developer)(HOST=jdbc)(USER=adpc)))).................6.,.A ...O........:..............................(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=11.3.68.171)(PORT=1521))(CONNECT_DATA=(CID=(PROGRAM=SQL Developer)(HOST=jdbc)(USER=adpc))(SERVICE_NAME=svc1)(CID=(PROGRAM=SQL Developer)(HOST=jdbc)(USER=adpc)))). .......6.A ........ ..........................
Now, go back to the packet and find out what the "real" address is. Here is an example:
Wire shark packet information. This is the destination IP where Oracle listener is.
Now use this to forward the port.
netsh interface portproxy add v4tov4 listenport=1521 connect address=11.3.68.135 connectport=1521 protocol=tcp
Now, when you log in using SQL Developer Basic Connection Type, you can use localhost instead of the VIP and you should be able to connect.
Now, go to another machine that has access to the machine you just set up and you should be able to connect to the database from there as well using the IP of the machine where you have port forwarding setup.
The same forward connection is working fine in both basic and advanced connection in SQL developer in my machine.
The only difference is machine A,B and Database are in the same network in my case where B can directly connect to the database also
Steps
1)You have to set up forwarding in machine A with machine A ip address as the listen address
2) Use the machine A ipaddress in TNS to connect to Database in machine B
If still, you are facing problems then you can try enabling log and trace files to identify the issue as mentioned in these articles
Oracle Database 10g Debugging Connection problems 1
Oracle Database 10g Debugging Connection problems 2

What exactly is my database URL for mysql server connecting remotely?

Trying to connect to a mysql database using JDBC automatic driver loading with syntax that requires the following: jdbc:subProtocolName:databaseURL
I know how to work it with a local host that's providing the mysql server, but I haven't done this remotely, and I want to get it right the first time, rather than coding my application and not being able to set breakpoints in the .xml file etc and confusing the source of other issues I might be having.
So far I have: "jdbc:mysql:[url???]:3306/dbname?autoReconnect=true"
Is it simply the ip address of the server, or is it myusername#[ipAddressofServer], or something else entirely? It doesn't sit with me that it would be name#localhost:[port#] because that would indicate the server resides locally. I've seen things online that say to use the 'server name' here. Is that equivalent to the hostname? Example would be great that doesn't involve mysql running on localhost. Thanks!
(Warning: Here be some slight oversimplifications.)
As you may be aware, computers (directly) connected to the Internet can be identified in two ways:
an "IP address", historically of the form 151.101.1.69 (but may be longer and scarier nowadays), or
a "DNS (Domain Name Service) name", e.g. stackoverflow.com, sometimes called a "host name".
When an application wants to connect to its own computer it can use two special cases of the above. 127.0.0.1 and localhost both mean "this here computer".
So if you want to connect to a remote machine you can use either its DNS name or its IP address as the "server name" in the connection string. For example, if there was a MySQL Server running on the machine in the examples above then your connection string could be
jdbc:mysql://151.101.1.69:3306/databasename?useUnicode=true&characterEncoding=utf8
or
jdbc:mysql://stackoverflow.com:3306/databasename?useUnicode=true&characterEncoding=utf8
Notes:
You don't actually need to specify port 3306 because that is the default port for MySQL. If you omit the port number from your connection string the driver will try to use 3306.
useUnicode=true&characterEncoding=utf8 are just examples of common attributes that are added to the connection string. There are lots of them, and you can read more about them here.

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.

Java Program which uses MySql running on other machine

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.

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