Connecting to external database using java - java

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 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){}

From jTDS documentation:
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.
My bet is your firewall does not allow the AIX host to connect.

Related

Failure while connecting to MS SQL server

Trying to connect o MS SQL server that runs on the same Win 10 machine with my project in Eclipse:
String url = "jdbc:sqlserver://GOLD\\SQLEXPRESS;databaseName=PrintLog;";
Connection conn = DriverManager.getConnection(url ,"user","passwd");
But got exception:
com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host GOLD, named instance sqlexpress failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
Computer name is GOLD. I can connect to DB from C# without problem. Authentication is enabled both windows and SQL server. I'm expecting to get Windows authentication while connect
SQL server properties:
How to find the problem?
UPD
I got better result by adding port number to connect string:
String url = "jdbc:sqlserver://GOLD\\SQLEXPRESS:1433;databaseName=PrintLog;";
Does it means that default port number is different?

Failed to connect to host MyServerName

Hi guys I am trying to connect NetBeans IDE with SqlServer, everything I did is correct, setting ports to 1433 in SQL Server Configurtion Manager and here is my code:
public Db(String login,String password, String dbname) throws SQLException{
try {
String url = "jdbc:sqlserver://MJRLGUE\\SWING;databaseName="+dbname+";integratedSecurity = false;";
Driver monDriver = new SQLServerDriver();
DriverManager.registerDriver(monDriver);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(url,login,password);
this.connecte = true;
System.out.println("Succes");
} catch (ClassNotFoundException e) {
System.out.println("echec driver "+e.toString());
this.connecte = false;
}
}
and the error message:
Try again com.microsoft.sqlserver.jdbc.SQLServerException: Failed to connect to host MJRLGUE, named instance swing. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server name and the instance name, and make sure that no firewalls block UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser service is running Execution on the host.
Exception in thread "main" java.lang.NullPointerException
at Model.Test.remplirDirecteur(Test.java:79)
at Main.main(Main.java:17)
C:\Users\Ghassane\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 29 seconds)
my instance name in SqlServer: MJRLGUE\SWING
code to connect the database: db= new Db("sa","ensak","SuiviMarche");
Check that you can ping the sql server from the machine running the code.
If you can you may need to to configure the sql server to allow remote connections, as you have said you already checked the firewall and I assumed the instance is running on the standard port of 1433
Also, check that you can you use SMSS to connect to the sql server from the machine running the code.
Edit:
Looking at your error message you are not using the standard port of 1433 as the message says you are trying to connect to 1434.
make sure that no firewalls block UDP traffic to port 1434
try using jdbc:sqlserver://localhost:1433 as your host.
check your url and make sure server is running properly and port number and service free from firewall.

Connect to sql server from java with jdbc(windows authentication mode)

I need to connect to Sql Server 2008 from java using jdbc 4.0.
I have a very simple code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost;" +
"integratedSecurity=true;";
Connection con = DriverManager.getConnection(connectionUrl);
But i have this error:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
...
I follow this answer:https://stackoverflow.com/a/12524566/1554397
I added jdbc4.jar in Libraries/Compile
SQL Server Browser windows service is running.
In SQL Server Network Configuration i selected Enebled on TCP/IP properties.
I Set TCP Address to 1433.
On Run,VM Options i put -Djava.library.path=my path to sqljdbc_auth.dll
And copy in JDk ,in bin sqljdbc_auth.dll.
What should I do?
EDIT:
When write in cmd telnet localhost 1433 i get 'Could not open connection to the host,on port 1433'
If using Window authentication you can do something like:
String url = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url);
and then add the path to sqljdbc_auth.dll as a VM argument (you need sqljdbc4.jar in the build path).
Take a look here for a short step-by-step guide showing how to connect to SQL Server from Java should you need more details. Hope it helps!

How to fix it: com.microsoft.sqlserver.jdbc.SQLServerException?

I want to connect to sql server express.
I downloaded this driver.
I read the help file and this is my code:
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost;database=ActorsDb;integratedSecurity=true;";
Connection con = DriverManager.getConnection(connectionUrl);
PreparedStatement st = con.prepareStatement("INSERT INTO Actors(FirstName,LastName,Age) VALUES(?,?,?)");
st.setString(1, "Robert");
st.setString(2, "de Niro");
st.setInt(3,45);
st.executeUpdate();
con.close();
}
catch (SQLException | ClassNotFoundException e)
{
e.printStackTrace();
}
I get this exception:com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
I turned off firewall but nothing changed.
I went to SQL Server Configuration Manager and i enabled TCP/IP
I went to IP Adresses, IP1 and set the properties
Active:Yes ; Enabled:Yes ; TCP Dynamic Ports:[empty] ; TCP Port:1433
Any tips of what i am missing?
Thanks.
Check if the SQL Server service is running and check if any programs like anti-virus is blocking the connections. If so, disable them or add exception to that connection.
You're using localhost in your JDBC URL, try instead using the IP address for "IP1" that you describe in SSCM. Or, verify SSCM is configured to listen on localhost:1433.
Problem solved. I had to copy sqljdbc_auth.dll in the working directory.

can't connect to mysql database in servlet

I keep getting this error when i'm trying to connect to my MySQL database:
exception
javax.servlet.ServletException: JDBC Problem:
root cause
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
root cause
java.net.ConnectException: Connection timed out: connect
here's some code:
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection
("jdbc:mysql://mysql11.000webhost.com/database_name","user_name","password");
stmt = con.createStatement();
rs = stmt.executeQuery("INSERT into emailadresses('email') values ('"+email+"')");
} catch (ClassNotFoundException ex) {
Logger.getLogger(EmailServlet.class.getName()).log(Level.SEVERE, null, ex);
}catch(SQLException e){
throw new ServletException("JDBC Problem: ", e);
}
edit: I have the mysql-connector-java jar file in my libraries
EDIT: FOUND THE PROBLEM
I was looking for the answer on the site where I host my database and found this:
JDBC/ODBC is not supported here
that s*cks...
"Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the connection was refused remotely (e.g., no process is listening on the remote address/port)". - From Oracle Docs
Take a look into your connection string, be sure that the database, username and the password are correctly typed.
Another cause may be that your database server is not up.
Yes its not a classpath problem, its a networking problem. For some reason no network connection can be established to the given host from your client machine. So yeah, could be lots of things.
server is actually not running!
host/IP wrong
firewall (your side)
firewall (other side)
whatever else networking problem there might be
Definitely a networking problem. Note that your tcp connection is NOT refused, instead it incurs in a timeout error. In my experience that behaviour could be caused by a TCP handshaking not correctly finalized (eg. a firewall dropping SYN packets).
Try connecting that database from any database client(like sqlyog) from machine you are running your application. If connection fails from client also, then surely it's a network issue.

Categories