Unable to connect Java application to SQL Server database - java

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

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 get SQL Server connection object

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

Connecting to AS400/iSeries using Oracle WebLogic Server

I have encountered a problem when setting up a Data Source in Oracle WebLogic Server that is an AS400. When setting it up and testing the connection I get the error message:
"The application requester cannot establish the connection"
When checking on IBMs website I found the following:
What does "The application requester cannot establish the connection"
mean?
The Toolbox JDBC driver uses the system name, user ID, and password
provided in the call to DriverManager.getConnection() to establish a
connection to the IBM i database. If any of these are not provided,
the Toolbox JDBC driver will display a sign-on prompt. If the Toolbox
JDBC driver can not sign on to the system for any reason, it will
throw an SQLException with the message "The application requester
cannot establish the connection". Here is a list of potential causes
for this exception: The system name is incorrect. One way to verify
this is to use ping with the specified system name. If ping fails,
then there is a TCP/IP configuration problem between your client and
the system. The user ID or password is incorrect. The IBM i database
host server is not started. You can start this by running STRHOSTSVR
*DATABASE on the IBM i command line. You can verify if the IBM i database host server is running
using the utilities.JPing application.
(http://www-03.ibm.com/systems/power/software/i/toolbox/faq/jdbc.html#faqC6)
When I ping the server, it fails - which also makes sense. However, when I use just a plain java file that opens a connection, fires an sql and closes the connection like
connection1 = DriverManager.getConnection("server", "user", "password")
Statement statement1 = connection1.createStatement();
ResultSet result1 = statement1.executeQuery("SQLquery")
i get a perfect connection and correct result. How is this possible? How can I connect via WebLogic and ODI?
PS: I already installed the AS400 driver according to this manual

The Network Adapter could not establish the connection when connecting with Oracle DB

When trying to connect with a remote Oracle database via JDBC I receive the following exception:
java.sql.SQLRecoverableException: IO-fout: The Network Adapter could not establish the connection
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:419)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:536)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:228)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
at java.sql.DriverManager.getConnection(DriverManager.java:322)
at java.sql.DriverManager.getConnection(DriverManager.java:358)
The following is my set-up:
Database: Oracle 10g Release 2 Standard Edition
JDBC library: ojdbc6.jar
JDBC driver: oracle.jdbc.driver.OracleDriver
JDBC URL: jdbc:oracle:thin:#9.2.2.2:1521:ORCL where ORCL is database's SID
JDBC User/pwd: Correct username / password
Strange about this problem is that the connection works just fine when I work from work. When I try to connect however from home via an AT&T VPN connection, it doesn't work.
I have confirmed that I can reach the IP address and have also telnetted the ip on port 1521, which works just fine. Connecting to the datasource from a local WebLogic Application Server also works alright. Furthermore, when trying to connect to the database via sqldeveloper I can also reach the database.
I need to reach the database however from a standalone application (for testing purposes). Does anyone have an idea why this problem occurs? And whether there are alternatives for connecting to a remote Oracle Database, alternatives which sqldeveloper and weblogic perhaps use?
Here's an excerpt of the code attempting to connect with the database:
public static void main(String args[]) throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:#9.2.2.2:1521:ORCL", "user", "pwd");
}
When a client connects to an Oracle server, it first connnects to the Oracle listener service. It often redirects the client to another port. So the client has to open another connection on a different port, which is blocked by the firewall.
So you might in fact have encountered a firewall problem due to Oracle port redirection. It should be possible to diagnose it with a network monitor on the client machine or with the firewall management software on the firewall.
If it is on a Linux box, I would suggest you add the database IP name and IP resolution to the /etc/hosts.
I have the same error and when we do the above, it works fine.
Take a look at this post on Java Ranch:
http://www.coderanch.com/t/300287/JDBC/java/Io-Exception-Network-Adapter-could
"The solution for my "Io exception: The Network Adapter could not establish the connection" exception was to replace the IP of the database server to the DNS name."
I had similar problem before. But this was resolved when I started using hostname instead of IP address in my connection string.

Connecting to external database through telnet

I have a java program that connects to a MS SQL database. The program works perfectly when running through eclipse however I get an error when I run it through AIX:
java.sql.SQLException: Network error IOException: A remote host refused an attempted connect operation.
I can successfully ping the server but am not able to telnet into the server. I am also not able to telnet from my windows desktop.
I am using jtds to connect:
String connectionString = "jdbc:jtds:sqlserver://"+dropez_ip_address+"/"+dropez_db_name;
ResultSet rs = null;
Statement stmt = null;
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection(connectionString, dropez_db_username, dropez_db_password);
stmt = conn.createStatement();
}catch(Exception e){}
Here is some documentation from jTDS regarding the issue, but I am still not able to resolve the issue.
Why do I get java.sql.SQLException: "Network error IOException: Connection refused: connect" when trying to get a connection?
The "Connection refused" exception is thrown by jTDS when it is unable to connect to the server. There may be a number of reasons why this could happen:
- The server name is misspelled or the port number is incorrect.
- SQL Server is not configured to use TCP/IP. Either enable TCP/IP from SQL Server's Network Utility app or have jTDS connect via named pipes (see the URL format for information on how to do this).
- There is a firewall blocking port 1433 on the server.
To check whether TCP/IP is enabled and the port is not blocked you can use "telnet 1433". Until telnet doesn't connect, jTDS won't either. If you can't figure out why, ask your network administrator for help.
If you can't telnet on port 1433, you are blocked by a firewall somewhere in the middle between your machine and the server. That's not a java related problem.
May it be that when you say "it runs perfectly under eclipse but not AIX" you are taking about 2 different computers ? If so, the one with eclipse is not firewalled, the one where you deploy your app is blocked.
But again, nothing to do with java. It's a level 3 error (TCP layer) of TCP-IP model.
Regards,
Stéphane
Your SQL Server database probably doesn't have the TCP/IP protocol enabled, to enable it:
From the Microsoft SQL Server 2005 -> Configuration Tools, open the 'Microsoft SQL Server Configuration Manager'.
Expand ‘SQL Server 2005 Network Configuration’, and then click ‘Protocols for ’.
Right-click ‘TCP/IP’ and then click ‘Enable’. The icon for the protocol will change to show that the protocol is enabled.
For SQL Server 2008:

Categories

Resources