JDBC connectivity issues while connecting to data base in a network - java

I designed a java application using net beans, which connects to a database that is present on port 1527 and retrieves data. I have prepared a executable jar file by using build and compile. After running the jar i am getting the following error.
java.sql.sqlnontransientconnectionexception java.net.connectexception
error connecting to server localhost on port 1527 with message connection refused connect
I have written the following code for connecting to derby database
Class.forName("org.apache.derby.jdbc.ClientDriver");
String host= "jdbc:derby://localhost:1527/database/project.db;create=true";
Connection conn;
String user="user1";
String pass="pass1";
conn = DriverManager.getConnection(host,user,pass);
if (conn!= null) {
JOptionPane.showMessageDialog(null,"Connected to database");
}
Is this error because of un availability of db on server or i have to do anything other than the code?
If i use a local database in my system the application is working fine. That db address i used in the code is given by the person who asks me to develop the application.

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?

access derby database from an executable jar file

I did a java project with netbeans using derby. The project runs fine in the IDE, but when i build it and run from the .jar file without starting server from the netbeans, it displays:
"java.net.ConnectException : Error connecting to server localhost on port 1527
with message Connection refused: connect."
How can i connect to the database on my client`s machine without netbeans on it?
You need to know the IP of your DB server, as localhost or 127.0.0.1 refers to the DB installed on local machine, and if you run the app on client's machine it is pretty sure, that this DB is not there locally. You need to specify the exact IP address, if multiple clients should connect to the same DB.
If you just need a DB per app, then you can use Embedded Derby, that starts and stops with your application, so you don't really need external server (very good solution in case when this is the only app that use this DB):
public String driver = "org.apache.derby.jdbc.EmbeddedDriver";
...
Class.forName(driver).newInstance();
Get an Embedded Connection
public String protocol = "jdbc:derby:";
...
conn = DriverManager.getConnection(protocol + "derbyDB;create=true", props);
That embedded connection URL, fully constructed, looks like this:
jdbc:derby:derbyDB;create=true

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.

Connecting Java application to a database on a remote server

I am developing a Java application that needs a database. I know how to connect my application to a database that is stored in my local hard disk, but I need help to connect to a remote database.
String host="jdbc:derby://localhost:1527/Test";
Connection con=null;
String uname="admins";
String pass="admins";
try {
con=DriverManager.getConnection(host, uname, pass);
} catch (SQLException ex) {
}
The above code allows me to connect to a local database. What changes should I apply to connect to a database that is stored in a remote server?
Syntax for Derby JDBC database connection URL for the network client driver:
jdbc:derby://server[:port]/databaseName[;attribute=value]*
By defaultthe port is 1527 if omitted. suppose the user abc with password xyz wants to connect to the remote database remotedb on the server dbserver, use the following URL:
jdbc:derby://dbserver/remotedb;user=abc;password=xyz
One more thing as Russell Uhl said in his comment first make sure the remote server accepts connection

Connecting to external database using 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.

Categories