Java SQL Server connection string - java

How can i create a java connection to an sql server located on a different server and in a different location (remote).
I am new to SQL Server, the server holding the database is 172.16.18.145 and the machine where am running the application from is 192.168.17.5

Your JDBC url would include the server name and port. Could you please elaborate the problem you are facing
String url="jdbc:db2://172.16.18.145:50007/exampledb";
String user="db2inst1";
String password="password";
connection = DriverManager.getConnection(url, user, password);

Related

JDBC connectivity issues while connecting to data base in a network

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.

Unable to connect to mysql jdbc database

I am frustrated seeing this error and not knowing the solution. I am trying to connect to a mysql db with a server url but it is giving my mysqlException(stacktrace below). The code works fine till here:
String dbUrl = "jdbc:mysql://server_url/db_name";
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
String user = "user";
String password = "password";
conn = DriverManager.getConnection(dbUrl,user,password);
This is the error I'm getting
java.sql.SQLException: null, message from server: "Host '172.23.251.154' is not allowed to connect to this MySQL server"
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1070)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
Is this because I'm using a different version of mysql-connector jar?
Please help me.
From Chapter 6. SQL Questions: How Do I Enable TCP Connections to MySQL?,
By default, MySQL won't allow ANY users access to any of the databases if they connect over a TCP connection. In order to permit the connection, you must create a entry in the user table of the mysql database (make sure you select the PASSWORD function to encrypt your password). In particular, the Host field needs to indicate which host(s) are permitted to connect. If you specify % (which I would not recommend), then a user would be able to connect from any host.
What about firewall on port 3306?
May be the user you are trying to access doesn't have enough privileges to access the database from the given server.
So try providing GRANTS to the user.
GRANT ALL PRIVILEGES ON database_name TO 'user'#'hostname' IDENTIFIED BY PASSWORD <password>;
FLUSH PRIVILEGES;
here hostname can be your host address "172.23.251.154". Dont forget to flush privileges and then try connecting the server.
If your MySql server is located on your host provider there is option in your control panel where you can set ip addresses from which you can connect to mysql server in your case 172.23.251.154 if this is your ip address

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

Unable to connect to sql server 2008 with jdbc

I'm trying to connect to a sql server with JDBC in java.
My server uses a windows authentication.
I use this code:
String url = "jdbc:sqlserver://MYPC\\MYSERVER;databaseName=MYDB;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url);
In my classpath i have the file:
jtds-1.3.1.jar and sqljdbc4.jar
And i have a VM argument:
-Djava.library.path="SQLPATH\jtds\x64\SSO"
When i run the code, i have this exception:
com.microsoft.sqlserver.jdbc.SQLServerException: Unable to connect to the host MYPC
Do you know what is wrong?
The URL syntax is jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
Did you try to ping your host using ping command ? Or try with IP address.
If //MYPC\\MYSERVER is a network path, I'd imagine it needs to look like this:
\\MYPC\\MYSERVER
Of course, if the SQL Server 2008 instance is setup as the default instance, the \\MYSERVER portion isn't necessary.

Java connection string for sqlserver for localdb

I have done Java application using Netbeans and Sqlserver 2012.
So for developing the application i ran Sqlserver in particular port and used the below string for connecting.
public static Connection connectDB(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost\\SQLEXPRESS:1433;databaseName=DB1;user=zubair;password=zubair1234");
//JOptionPane.showMessageDialog(null,"Connection established");
return conn;
}catch (Exception ex){
JOptionPane.showMessageDialog(null,ex);
return null;
}
}
Now for delivering the java application , I need to make the sqlserver db to be run locally within the application. Iam not an expert in java so little help i require to change the connection string.
For access db i know we can use "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+accessFileName+".accdb;";" this string and give the filename of the Db.
Is there any similar string for sqlserver db. I have copied the DB to my project folder.?
Connect to the database on the local computer by using a username and password:
jdbc:sqlserver://localhost;databaseName=DbName;user=MyUserName;password=*****;
Please read Building the Connection URL and follow the guidelines.
I don't have any running example with me.
But will surely post after trying it myself.
Secondly, I strongly recommend using properties files to store connection details.
Prefer XML over prop files.

Categories