I am having issues connecting to mySQL server using ConnectJ driver for JDBC.
I wrote a simple program just to establish connectivity as below:
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://[host]:1521/OBIEE11G";
private static final String DB_USER = "OBADW";
private static final String DB_PASSWORD = "OBADW";
public static Connection getConnection() {
Connection dbConnection = null;
try {
// DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
try {
// DriverManager.registerDriver(new com.mysql.jdbc.Driver());
System.out.println("before get connection ***********");
System.out.println("Java version: " + System.getProperty("java.version"));
Driver driver = (Driver) Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("JDBC driver: " + driver.getMajorVersion() + "." + driver.getMinorVersion());
dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
System.out.println("Connection: " + dbConnection.getClass().getName());
System.out.println("after get connection ***********");
return dbConnection;
} catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
return dbConnection;
}
public static void main(String[] args) {
Connection dbConnection = getConnection();
if (dbConnection != null) {
System.out.println("Connected");
} else {
System.out.println("Failed");
}
I am getting the following error:
before get connection ***********
Java version: 1.7.0
JDBC driver: 5.1
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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:85)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57)
at java.lang.reflect.Constructor.newInstance(Constructor.java:541)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:983)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:628)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1014)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2254)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2285)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:85)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57)
at java.lang.reflect.Constructor.newInstance(Constructor.java:541)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:226)
at JDBCConnection.getConnection(JDBCConnection.java:32)
at JDBCConnection.main(JDBCConnection.java:45)
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2949)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:560)
... 17 more
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.
Failed
I am using connectJ : mysql-connector-java-5.1.37-bin.jar
Java 1.7, JDBC driver 5.1
I am able to ping the host, and connect it using mysql developer.
Please suggest.
From your log its clear about "Communications link failure"
ensure your mysql server running
ensure you have the right hostname/ip/port
ensure your network is working
Sounds stupid to ask but have you configured your MySQL Connector.jar file correctly in your project properties?
Related
ERROR MESSAGE in SERVER
[2019-01-07 09:44:02] ERROR
:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications
link failure
[2019-01-07 09:44:02]
[2019-01-07 09:44:02] The last packet sent successfully to the server was 0
milliseconds ago. The driver has not received any packets from the server.
But testing in local.. No problem.. No Error message..
Commit libraries..
mysql-connector-java-5.1.47-bin.jar
mysql-connector-java-5.1.47.jar
JDBC URL ALTER
jdbc:mysql://IP:3306/ID -> jdbc:mysql://IP:3306/ID?autoReconnect=true
Changed Error Message..
[2019-01-07 11:04:32] Error :
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could
not create connection to database server. Attempted reconnect 3 times.
Giving up.
ControlDAO.java
private final static String DRIVER = "com.mysql.jdbc.Driver";
private final static String URL =
"jdbc:mysql://IP:3306/ID?autoReconnect=true;
public static void CsNumberCheck(String s) {
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, "ID", "PW");
System.out.println(conn);
stat = conn.createStatement();
int result = 0;
sql = Query;
rs = stat.executeQuery(sql);
while ( rs.next() ) {
result = rs.getInt(1);
}
rs.close();
stat.close();
System.out.println(result);
CsNumberResult(result, OrangeEmail);
}
catch (ClassNotFoundException e) {
System.out.println("Driver Loading Failed..");
}
catch (SQLException e) {
System.out.println("Error : " + e);
}
}
Socket.java
ControlDAO.CsNumberCheck(s);
But nothing result about "Sysout", Only Error Message....
Check your windows services to see if your MySQL server is running. That might be a probable cause. If it's not, start it and try to reconnect.
I want to connect MySQL with Java in a local network (between 3 computers) using XAMPP. But I can't do that with Java and tried to connect the database with PHP application, then connected. I think this is not XAMPP error. It may be in Java app.
My code:
Connection getcon() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String URL = "jdbc:mysql://192.168.8.101/checker";
String username = "root";
String password = "";
//Connection
con = DriverManager.getConnection(URL, username, password);
return con;
}
Exception:
run: com.mysql.jdbc.CommunicationsException: Communications link
failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.SocketException MESSAGE: java.net.ConnectException:
Connection timed out: connect
STACKTRACE:
java.net.SocketException: java.net.ConnectException: Connection timed
out: connect at
com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:276) at
com.mysql.jdbc.Connection.createNewIO(Connection.java:2666) at
com.mysql.jdbc.Connection.(Connection.java:1531) at
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(DriverManager.java:661) at
java.sql.DriverManager.getConnection(DriverManager.java:247) at
connectionchecker.JDBC.getcon(JDBC.java:45) at
connectionchecker.JDBC.getdata(JDBC.java:68) at
connectionchecker.Check.incremntno(Check.java:28) at
connectionchecker.Check.jButton1ActionPerformed(Check.java:84) at
connectionchecker.Check.access$000(Check.java:15) at
connectionchecker.Check$1.actionPerformed(Check.java:50) at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6522) at
javax.swing.JComponent.processMouseEvent(JComponent.java:3322) at
java.awt.Component.processEvent(Component.java:6287) at
java.awt.Container.processEvent(Container.java:2229) at
java.awt.Component.dispatchEventImpl(Component.java:4878) at
java.awt.Container.dispatchEventImpl(Container.java:2287) at
java.awt.Component.dispatchEvent(Component.java:4700) at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4872)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:4528)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4457)
at java.awt.Container.dispatchEventImpl(Container.java:2273) at
java.awt.Window.dispatchEventImpl(Window.java:2724) at
java.awt.Component.dispatchEvent(Component.java:4700) at
java.awt.EventQueue.dispatchEventImpl(EventQueue.java:743) at
java.awt.EventQueue.access$400(EventQueue.java:97) at
java.awt.EventQueue$3.run(EventQueue.java:694) at
java.awt.EventQueue$3.run(EventQueue.java:691) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at
java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:716) at
java.awt.EventQueue$4.run(EventQueue.java:714) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:713) at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:220)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:135)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:123)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:119)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:111)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
** END NESTED EXCEPTION **
last packet sent to the server was 43 ms ago. my msg is conn ok
java.lang.NullPointerException
JDBC class:
public class JDBC {
private static JDBC j;
Connection con = null;
private JDBC() {
}
public static JDBC getJDBC() {
if(j == null){
j= new JDBC();
}
return j;
}
Connection getcon() throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
//String URL = "jdbc:mysql://127.0.0.1/checker";
String URL = "jdbc:mysql://192.168.8.101:80/checker";
String username = "root";
String password = "";
//Connection
con = DriverManager.getConnection(URL, username, password);
} catch (ClassNotFoundException | SQLException e) {
System.out.println(e);
}
return con;
}
void setdata(String sql) {
try {
if (con == null) {
getcon();
}
con.createStatement().executeUpdate(sql);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
ResultSet getdata(String sql) throws Exception {
if (con == null) {
getcon();
System.out.println("my msg is conn ok");
}else{
System.out.println("my msg is conn not ok");
}
ResultSet rset = con.createStatement().executeQuery(sql);
return rset;
}
}
You need to pass the port number of the XAMPP after the ip address.
The default port number is 80 for XAMPP.
String URL = "jdbc:mysql://192.168.8.101:80/checker";
Or try this one below
String URL = "jdbc:mysql://192.168.8.101:8080/checker";
Problem lies here
String URL = "jdbc:mysql://192.168.8.101:80/checker";
Note, this should represent your db server and port where your db is running. Above url is wrong as, port 80 is default port number for Apache server. But, you need to use mysql port, which is 3306 (default).
Instead, you should try
String URL = "jdbc:mysql://192.168.8.101:3306/checker";
I'm assuming checker is database name.
This question already has answers here:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
(51 answers)
Closed 4 years ago.
I have settled MAMP on my machine and last time when I used that code on my old computer and with another database that works pretty well. Now I cannot understand where I am wrong.
public class JDBC {
private static String DBURL = "jdbc:mysql://localhost:3306/BookReservationDatabase";
private static String DBLOGIN = "root";
private static String DBPASSWORD = "root";
public static void main(String[] args) {
// Step1: register JDBC driver
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// Step 2: Establish the connection to the database
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
try {
conn = DriverManager.getConnection(DBURL, DBLOGIN, DBPASSWORD);
// Step 3: Create statements (SQL queries)
stmt = conn.createStatement();
// Step 4: Execute statements and retrieve results
rset = stmt.executeQuery("SELECT * FROM Users");
// Step 5: Analyze results
while (rset.next()) {
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// Step 6: Release resources
try {
if (rset != null)
rset.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
My database name is BookReservationDatabase, I use root as a login and password on PhpMyAdmin.
I have got the following errors :
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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:342)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2221)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2016)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at features.JDBC.main(JDBC.java:26)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:301)
... 15 more
The error must be among DBURL,DBLOGIN and DBPASSWORD. Because the code generate error at the connection
conn = DriverManager.getConnection(DBURL, DBLOGIN, DBPASSWORD);
java.net.ConnectException: Connection refused (Connection refused)
clearly signals a communication error on the machine on which you run this code. This could be caused by many reasons, thus I merely list the most likely ones:
MySQL is not running at all.
Did you start it? Verify by inspecting running process lists (depends on OS).
MySQL is not listening on Port 3306 which is the default port.
Did you change it? If so, change your code/config accordingly.
In case a firewall is active, check the rules that could prevent connections on port 3306.
Hope it helps.
My code :
package java_connect;
import java.sql.*;
public class JdbcOracleXe {
public static void main(String[] args) {
System.out.println("-------- Oracle JDBC Connection Testing ------");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("Oracle JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:oracle:thin:#localhost:1521/xe", "username",
"password");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}
Error message :
-------- Oracle JDBC Connection Testing ------
Oracle JDBC Driver Registered!
Connection Failed! Check output console
java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at java_connect.JdbcOracleXe.main(JdbcOracleXe.java:28)
Check if you have the database up. Seems like the database may be down and hence you are not able to connect
I am creating a simple example of jdbc .I am getting this error .I run my sql server on my mac machine .can you please tell me how to make connection with my sql and remove this error .I already include connector jar file .
Exception in thread "main" 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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1129)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:358)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2489)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2526)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2311)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at HelloWorldExample.main(HelloWorldExample.java:17)
Caused by: java.net.UnknownHostException: local
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:877)
at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1230)
at java.net.InetAddress.getAllByName0(InetAddress.java:1181)
at java.net.InetAddress.getAllByName(InetAddress.java:1111)
at java.net.InetAddress.getAllByName(InetAddress.java:1047)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:248)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:308)
... 15 more
Here is my code ..I am using mac
import java.sql.*;
/**
* HelloWorldExample : JDBC Tutorial that creates a simple database, stores in a single row
* and retrieves the data to then be displayed on standard out. The database is cleaned up
* to reduce clutter for this example.
* Author: Kevin Hooks
* Date: April 18th, 2012
**/
public class HelloWorldExample {
public static void main(String[] args) throws SQLException{
// Note that you can use TCP/IP by using this URL
// "jdbc:raima:rdm://localhost"
// The rdmsqlserver must be running to use TCP/IP
Connection Conn = DriverManager.getConnection("jdbc:mysql://local");
try {
Statement Stmt = Conn.createStatement();
try {
try { //Since the database is created here, it cannot be created twice
Stmt.execute("DROP DATABASE hello_db");
} catch (SQLException exception) {}
Stmt.execute("CREATE DATABASE hello_Db");
Stmt.execute("CREATE TABLE hello_table (f00 char(31))");
Conn.commit();
PreparedStatement PrepStmt = Conn.prepareStatement("INSERT INTO hello_table (f00) VALUES (?)");
try { //Sets parameter value to a string
PrepStmt.setString(1, "Hello World!");
PrepStmt.execute();
Conn.commit();
ResultSet RS = Stmt.executeQuery("SELECT * FROM hello_table");
try {
while(RS.next() != false) {
System.out.println(RS.getString(1));
}
} finally {
RS.close();
}
} finally { //Cleans up by dropping database in this case
Stmt.execute("DROP DATABASE hello_db");
PrepStmt.close();
}
} finally {
Stmt.close();
}
} catch (SQLException exception) {
System.err.println("SQLException: " + exception.toString());
}finally {
Conn.close();
}
}
}
The hostname of the loopback address is localhost, not local.
Exception in thread "main" 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.
at HelloWorldExample.main(HelloWorldExample.java:17)
Line 17:
Connection Conn = DriverManager.getConnection("jdbc:mysql://local");
Your program can not establish a connection to the database, be sure that you have your connection URL correct.
local, should be replaced by localhost, or 127.0.0.1
You should also note, that you never actually select a database to use.
you have declared wrong connection string. please modified it according to below rule
Connection Conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name");
your URL should be localhost then add the database name
have a look at this
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class JDBCExample {
public static void main(String[] argv) {
System.out.println("-------- MySQL JDBC Connection Testing ------------");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/mkyongcom","root", "password");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}