Unable to get SQL Server connection object - java

I wrote a simple code to connect to SQL Server database and to does a simple select statement, this code runs successfully on my local machine over VPN but when I deploy the same code in the servers, then my code is stopping at getting the DB connection object.
It is not even throwing any timeout exception, so I thought of a port issue and checked using telnet which is successful. Can anyone please suggest why this might happen? I am able to get connection object of the same DB from my local machine over VPN.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = props.getProperty("DBURI");
Connection conn=null;
System.out.println(connectionUrl.trim());
conn = DriverManager.getConnection(connectionUrl.trim());

Problem with the sqljdbc jar file , Used any 32 bit one , After replacing the jar , everything worked fine. Thanks

Related

Unable to connect to a Oracle db from Linux machine in java

I have registered a Linux machine as runner in my gitlab and tried to execute my scripts there.
While executing I got an error for database connection
Issue : java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
When I connect from my local machine (Windows 7 & 10) it is working fine.
Attempts:
Tried to connect using jtds but I'm getting some other unknown issue.
Checked few other answers and they have provided that it could be because of firewall enabled but since we did not have full access to that db server , I could not disable and check.(Could this be the issue?)
My Connection code :
ConURL = "jdbc:oracle:thin:#" + ConstructSQLConnectionURL(host,sid,port);
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(ConURL, dbUserName, dbPassword);
I am stuck with this issue and blocked from executing pipeline.
Can anyone let me know what could be the possible reason for this and help me with this?
Thanks in advance
It appears to be a network connectivity issue. I'd start with local firewalls on each system. Can you ping the database from the client? If not, look for network routing or firewall blockers. Also look at host-based firewalls, or confirm with the DBA whether there is something like valid node checking in place to limit database clients.

Unable to connect Java application to SQL Server database

I am trying to write a Java desktop app that can connect to my database made with Microsoft SQL Server Manager to allow me to view and update it. But, I am having trouble getting the connection to work. I've read through a bunch of tutorials and threads here on Stack Exchange of similar problems, and I'm not sure what I'm doing wrong.
The server is called "SQLEXPRESS" using Windows authentication. I downloaded the JDBC driver found here: https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774 installed it in NetBeans by going to "Services-Databases(right click)-New Connection-Add", but I also added it as a library in my project.
When I try this code, I get the exception that the TCP/IP connection failed either because the server isn't running or port 1433 is locked:
try{
String
URL="jdbc:sqlserver://sqlexpress:1433;DatabaseName=GreenhouseManagement";
Connection conn = DriverManager.getConnection(URL,"","");
System.out.println("connected");
}catch(Exception e){
System.out.println("Oops\n"+e);
}
What do I need to change to fix this?
You might need to reconfigure your connection string into this format.
jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE
HOST in this case is most likely to be "localhost" since you are connecting on a local machine.
DATABASE will be the name of your database
Reference: http://alvinalexander.com/java/jdbc-connection-string-mysql-postgresql-sqlserver

Could not create connection to database server. Attempted reconnect 3 times. Giving up

I am trying to connect to db4free.net but am getting this error. It was working yesterday. I was able to connect yesterday but today, I can't.
URL:
jdbc:mysql://db4free.net:3306/mydb?autoReconnect=true
Line of code error is at:
connection = DriverManager.getConnection(url, username, password);
JDBC driver is loading correctly so that shouldn't be the error.
That's what you get for free. I would suggest using a different database hosting provider, or check into hosting the database server yourself.

Can't connect to SQL Server using jTDS

I'm trying to connect to SQL Server 2008 R2 via Java, and I'm unable to do so using jTDS 1.2.8. The odd thing is that it works fine using the Microsoft JDBC driver. Is there some server-side setting that needs to be turned on to enable jTDS to access it? Or am I just missing something in the URL?
I'm not using Windows integrated authentication to specify credentials, nor am I attempting to connect using SSL encryption (those are issues I found that can generate the exception I'm seeing.)
If I use the following with the Microsoft driver, it works as expected, I can access the database with no problems:
Connection connection = DriverManager.getConnection("jdbc:sqlserver://PHSSQL792\\PHSSQL792:1433", user, password);
(user and password are variables declared earlier, so I can be sure I use the same values when connecting with either driver.)
However, if I use the following with the jTDS driver:
Connection connection = DriverManager.getConnection("jdbc:jtds:sqlserver://PHSSQL792:1433;instance=PHSSQL792", user, password);
I get the following error:
java.sql.SQLException: I/O Error: DB server closed connection.
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2387)
at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:614)
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:356)
at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:185)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at database.db_access.SqlServerDatabaseTestApp.main(SqlServerDatabaseTestApp.java:28)
Caused by: java.io.IOException: DB server closed connection.
at net.sourceforge.jtds.jdbc.SharedSocket.readPacket(SharedSocket.java:853)
at net.sourceforge.jtds.jdbc.SharedSocket.getNetPacket(SharedSocket.java:732)
at net.sourceforge.jtds.jdbc.ResponseStream.getPacket(ResponseStream.java:477)
at net.sourceforge.jtds.jdbc.ResponseStream.read(ResponseStream.java:114)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2281)
at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:614)
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:356)
at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:185)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at database.db_access.SqlServerDatabaseTestApp.main(SqlServerDatabaseTestApp.java:28)
I've tried connecting with and without the database name, and/or with/without the instance name, and got the same results. Any suggestions?
Edit:
Other jTDS connection URLs I've tried (which all gave me the same error as above):
"jdbc:jtds:sqlserver://PHSSQL792:1433"
"jdbc:jtds:sqlserver://PHSSQL792:1433/pacsdb"
"jdbc:jtds:sqlserver://PHSSQL792:1433/pacsdb;instance=PHSSQL792"
The corresponding Microsoft URLS (which all worked):
"jdbc:sqlserver://PHSSQL792:1433"
"jdbc:sqlserver://PHSSQL792:1433;databasename=pacsdb"
"jdbc:sqlserver://PHSSQL792\\PHSSQL792:1433;databasename=pacsdb"
Also, I can successfully connect to a different SQL Server 2008 R2 database (on a different server) using the jTDS driver, so it's not the jar.
In case anyone ever runs into this, I came back to this much later and finally figured out the problem. The SQL Server instance in question was configured to require SSL connections! I just added ssl=request to the URL to make it work.
I think the reason the Microsoft driver works without explicitly setting SSL encryption is that it ALWAYS initially connects with SSL to encrypt username/password for login. The encrypt property only controls whether data after login is encrypted.
Here's the format we are using, which looks very close to your:
jdbc:jtds:sqlserver://localhost:1433/Dev_DB;tds=8.0;lastupdatecount=true
It's strange, yours connection strings looks correct. Try to set user and password directly:
"jdbc:jtds:sqlserver://PHSSQL792:1433/pacsdb;instance=PHSSQL792;user=sa;password=pass"
String driver="net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();
//First way
String connString="jdbc:jtds:sqlserver://192.168.1.123:1433/database_name;encrypt=false;user=sa;password=mypass;";
String username="sa";
String password="mypass";
Connection con=DriverManager.getConnection(connString,username,password);
//Second way
String connString="jdbc:jtds:sqlserver://127.0.0.1:1433/database_name;encrypt=false;user=sa;password=mypass;integratedSecurity=true;instance=SQLEXPRESS;";
String username="sa";
String password="mypass";
Connection con=DriverManager.getConnection(connString,username,password);

Connecting to hosted MySQL server with Java

I've been recently trying to connect to a hosted MySQL using Java but can't get it to work. I can connect to a local MySQL with localhost using:
connect = DriverManager.getConnection("jdbc:mysql://localhost/lego?"
+ "user=******&password=*******");
(Replacing the astrisks withmy username and password)
I can connect to the hosted MySQL database fine with PHP using:
mysql_connect('mysql.hosts.co.uk','******','**********');
mysql_select_db('test');
My problem is, I cannot connect via Java. I have an Exception which is caught if the connection doesn't work and this is always printed out.
Any ideas why it isn't working? Am I doing something wrong?
Thanks for your time,
InfinitiFizz
since it works in php (i guess you didn't try to connect from a local place with php???) it shouldn't be a port problem... but you should check that port 3306 is open... and ask the hosts company about that.
Have you noticed that in the DriverManager
http://java.sun.com/javase/6/docs/api/java/sql/DriverManager.html
you have:
getConnection(String url)
but also:
getConnection(String url, String user, String password)
Perhaps it would work better...
My guess is that you need to select a non-standard port, since I'd imagine the hosting server is serving lots of MySQL instances and they can't all use the normal one. I don't see selection of a port here.
If that's not it, perhaps there is a firewall issue somewhere along the way that's blocking the port or connection.

Categories

Resources