I know that this issue has been discussed a lot of times but I cannot establish a connection between my Java app and MySQL server.
I'm using code form tutorials:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCTest{
private Connection conn = null;
public void connect(){
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("test");
conn = DriverManager.getConnection("jdbc:mysql://mysql.***.***.**:3306/******","*******","top_secret_password");
System.out.println("test2");
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " ex.getMessage());
System.out.println("SQLState: " ex.getSQLState());
System.out.println("VendorError: " ex.getErrorCode());
}catch(Exception e){e.printStackTrace();}
}
}
Error that occured is:
test
SQLException: 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.
SQLState: 08S01
VendorError: 0
Have you got any ideas how fix it?
Try to provide the IP-address of the database instead of its name.
Say
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/*******","*******","top_secret_password");
and make sure that your firewall doesn' block 3306 port
Related
I'm trying to connect from SSMS(SQL Server Management Studio) to Eclipse(Java), but I keep getting this error message:
com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host localhost, 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.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:234)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:6132)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:2609)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2346)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:2213)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1276)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:861)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at net.codejava.sql.JavaConnect2SQL.main(JavaConnect2SQL.java:16)
The code I do have is:
import java.sql.DriverManager;
import java.sql.SQLException;
//import com.sun.jdi.connect.spi.Connection;
public class JavaConnect2SQL {
public static void main(String[] args) {
// TODO Auto-generated method stub
String url = "jdbc:sqlserver://localhost\\SQLEXPRESS;databaseName=students"; // This is where I think the error is
String user = "sa";
String password = "123";
try {
java.sql.Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("Succesfully connected to Microsoft SQL Server");
}catch (SQLException e) {
System.out.println("Oops! There was an error: ");
e.printStackTrace();
}
}
}
I think the problem is in the connection URL, but I don't know the host name/instance name. If you can tell me how to get my host name/instance name, that would be appreciated. But if the problem is something else, please tell me!
Edit: I have changed the code, and I'm now getting a different. Here is the new code:
package net.codejava.sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JavaConnect2SQL {
public static void main(String[] args) {
// Create a variable for the connection string.
String url = " jdbc:sqlserver://LAPTOP-5697KK36:1433;databaseName=students";
String user= "sa";
String password = "123";
try {
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("Connection succesful!");
}
// Handle any errors that may have occurred.
catch (SQLException e) {
System.out.println("Here is your error message: ");
e.printStackTrace();
}
}
}
The new error message is this:
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://LAPTOP-5697KK36:1433;databaseName=students
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at net.codejava.sql.JavaConnect2SQL.main(JavaConnect2SQL.java:40)
I think the problem is that I installed the wrong version of JDBC Driver. My JRE version is 12.0.1. What is the correct JDBC Driver to install.
You can check in SQL server configuration manager and Microsoft SQL server management studio. The state must be running in server configuration manager and you can try to connect your database in Microsoft SQL server management with your password.
if you connect successfully then there is another problem but it looks like the database is stopped.
I am using the below code to connect to MySQL
import java.sql.DriverManager;
import com.mysql.jdbc.Connection;
public class connectMysql {
public static void main(String[] args){
Connection conn = null;
try{
conn = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306//test","root","admin");
if(conn!=null)
{
System.out.println("Connected successfully");
}
}catch(Exception e)
{
System.out.println("Not connected");
e.printStackTrace();
}
}
}
The username: root, password: admin, Hostname: localhost, Port: 3306.
I get the output as "Not connected".
[Edits] Now I Stack Trace and see that error is 'Unknown database '/test'. But I do have a schema named test. Is the schema same as the database?
Everything is same as this. But I don't seem to get connected to the DB.
Thanks for the help!!!
Why you have double slash // before your database name in your connection string
jdbc:mysql://localhost:3306//test","root","admin"
Change it to
jdbc:mysql://localhost:3306/test","root","admin"
Check all the drivers are available in the Library
Check for the port number, Username and password(case Sensitive)
if every thing is correct then
use the below code
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "username", "password");
statement = con.createStatement();
I need to access database with remote mysql server, but when i try it, i get an error like this :
SQLException: Access denied for user 'u121252012_iya'#'114.79.63.156' (using password: YES)
SQLState: 28000
VendorError: 1045
and this is my connection code :
package Include;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* #author Ruslan
*/
public class Koneksi {
private static Connection koneksi;
public static void main (String []args){
Connection conn = null;
try {
conn =
DriverManager.getConnection("jdbc:mysql://my_ip_server/u121252012_coba?" +
"user=u121252012_iya&password=qwertyu");
// Do something with the Connection
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
}
please, i need your assistance to solve it.
CREATE USER '<username>'#'%' IDENTIFIED BY '<pwd>';
Here % indicates user is allowed access from all IP.
Also, in addition you might need to grant permissions to the user
GRANT ALL PRIVILEGES ON *.* TO '<username>'#'%' WITH GRANT OPTION;
And also, the connection mechanism should be like.
conn = DriverManager.getConnection(<DB_URL>,<USER>,<PASS>);
So change
conn =
DriverManager.getConnection("jdbc:mysql://my_ip_server/u121252012_coba?" +
"user=u121252012_iya&password=qwertyu");
To
conn =
DriverManager.getConnection("jdbc:mysql://my_ip_server/u121252012_coba?",
"u121252012_iya","qwertyu");
I use PostgreSQL to create my database and save my users list to it, when i try to connect db by java jdbc, i get error that say:
"java.sql.SQLException: invalid database address:
jdbc:postgresql://localhost:5432/users".
i use "JDBC41 Postgresql Driver, Version 9.3-1102" from PostgreSQL website.
and this is my code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class javaconnect {
private static Connection c = null;
public static Connection connectDb() {
try {
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/users", "postgres", "12345");
return c;
} catch (ClassNotFoundException | SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
return null;
}
}
}
Thanks.
As the error,
"java.sql.SQLException: invalid database address:
says your database name is incorrect . check for the database name if you have something like sql developer installed.
Valid database name should be specified here "jdbc:postgresql://localhost:5432/users" after the /localhost:5432/
Read JDBC using postgresql to connect to PostgreSQL database using jdbc
I am trying to do a simple connection test to a postgresql database, the code is working well but I am getting the following message:
PostgreSQL 9.0 JDBC4 (build 802)
Found in: jar:file:/C:/thales/Dropbox/study/java/jars/postgresql-9.0-802.jdbc4.jar!/org/postgresql/Driver.class
I am using the jdbc file provided by the address http://jdbc.postgresql.org/download.html
Following, the code:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class connector {
public static void main(String[] argv) {
System.out.println("Checking if Driver is registered with DriverManager.");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe) {
System.out.println("Couldn't find the driver!");
System.out.println("Let's print a stack trace, and exit.");
cnfe.printStackTrace();
System.exit(1);
}
System.out.println("Registered the driver ok, so let's make a connection.");
Connection c = null;
try {
// The second and third arguments are the username and password,
// respectively. They should be whatever is necessary to connect
// to the database.
c = DriverManager.getConnection("jdbc:postgresql://localserver/test","ping", "pong");
} catch (SQLException se) {
System.out.println("Couldn't connect: print out a stack trace and exit.");
se.printStackTrace();
System.exit(1);
}
if (c != null)
System.out.println("Hooray! We connected to the database!");
else
System.out.println("We should never get here.");
}
}
thanks for all feedback but i manage to solve the problem reinstallign eclipse (?? )
using the same code, and the same jdbc driver, the code described in the question worked.