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
Related
My java program is working fine when I use localhot instead of IP address, but getting error when I connect using IP address. (I have to use IP address to connect, because it is a client server application).
I have also changed this /etc/mysql/mysql.conf.d/mysqld.cnf file bind address from 127.0.0.1 to 0.0.0.0, But again I get the error message like -
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.
My code is:-
try{
Connection con;
Statement st;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(
"jdbc:mysql://172.16.16.185:3306/itcentre?
verifyServerCertificate=false&useSSL=true","root","");
st=con.createStatement();
ResultSet rs=st.executeQuery("select * from teacher");
while(rs.next())
{
System.out.println(rs.getString(2));
}
rs.close();
st.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
Java can definitely establish an SSL connection without a client validating the certificate chain of the server.
The classes that are establishing the connection (javax.net.ssl classes) would normally treat the unverified server certificate with suspicion and would fail the handshake.
But they provide a way for the user's of those classes to in effect say "It's ok if the server's certificate doesn't validate, go ahead and establish the connection".
That is what's happening when you say verifyServerCertificate=false.
Thanks to everyone to help me. After struggling to many other issues, finally I have the luck with this solution. I logged into my mysql database using phpmyadmin and then
User Accounts ---> login information ---> and changed Host name from localhost to any host.
See the screen shot -
I know that the code below works for a Microsoft Access Database but I need to switch it to my remote SQL server.
try {
String driver = "net.ucanaccess.jdbc.UcanaccessDriver";
Class.forName(driver);
conn = DriverManager.getConnection("jdbc:ucanaccess://???SQLSERVERPATH???");
} catch (Exception e) {
System.out.println(e);
}
So where do I figure out what the server path is ?
The UCanAccess JDBC driver is only designed to work with data stored in Microsoft Access datbase files (.accdb, .mdb). It cannot be used to work with data in a SQL Server database. You will need to use a SQL Server JDBC driver for that (e.g., this one).
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);
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.
I have a remote mysql database server setup on a machine myuniversity.edu and server is named 'localhost'. On it I have the database named 'MyDatabase'.
I want to connect it through Java.
The connection urls that I have tried are:
jdbc:mysql://myuniversity.edu/localhost
jdbc:mysql://myuniversity.edu/localhost/MyDatabase
jdbc:mysql://myuniversity.edu:3306/MyDatabase
but I get the `Connection refused: connect` exception.
Could someone please tell what the connection url should be in this case?
Not really sure if your machine name is myuniversity.edu, you can instead try the IP Address with the connection string, Localhost is the name for loopback network interface and accessible on that machine only. Also make sure if your default port for mysql (may be 3306) is open. With IP address your connection string would look like:
jdbc:mysql://192.168.0.123/MyDatabase
With IP and port it would be:
jdbc:mysql://192.168.0.123:3306/MyDatabase
(You need to replace your IP in the above string)
I'ts impossible to connect remotely without (IP) address
try this approach
if you want to connect it via internet :
OPEN CMD on your computer
in CMD write ping myuniversity.edu (for example ping google.com)
then you will get an ip address of the website and you can copy the ip
then try this approach :
Connection con;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://THE IP ADDRESS :3306/DatabaseName");
System.out.println("CONNECTED");
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
Ok so here's what I did to fix the issue:
In my.cnf file, I changed the bind-address from '127.0.0.1' to the
'host ipaddress'. This allows connecting to the remote mysql server
but would not allow access for any remote host trying to connect to
it.
To fix that, I added an entry in user table with host '%'. This allows remote hosts to connect to the database.
Now I can connect to the database with jdbc:mysql://serverIpAddress:3306/MyDatabase