My code:
import java.sql.Connection;
import java.sql.DriverManager;
public class sqlConnection {
private static String url = "jdbc:mysql://localhost:3306/hosapi";
private static String uname = "root";
private static String pword = "1016";
public static void main(String[] arg) {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, uname, pword);
System.out.println("connection created");
} catch (Exception e) {
System.out.println("error");
e.printStackTrace();
}
}
}
error
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 322 milliseconds ago. The last packet sent successfully to the server was 297 milliseconds ago.
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:981)
at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:164)
at com.mysql.jdbc.MysqlIO.negotiateSSLConnection(MysqlIO.java:4801)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1643)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1215)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2255)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2286)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2085)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
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/java.sql.DriverManager.getConnection(DriverManager.java:681)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
at com.project.Hospital.sqlConnection.main(sqlConnection.java:19)
Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at java.base/sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:172)
at java.base/sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:103)
at java.base/sun.security.ssl.TransportContext.kickstart(TransportContext.java:240)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:448)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:426)
at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:149)
... 19 more
Related
This question already has answers here:
ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long on connect to MySQL
(6 answers)
Closed 2 years ago.
I was tryng to learn how to connect to mysql from an desktop java application, but there is something wrong with the connection method Class.forName("com.mysql.jdbc.Driver");
When I call it returns this expection :
java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
I reinstalled the J/connection (I couldn't download it alone, it comes with the other drivers in the installer, I added the driver as a library and i Tried using the com.mysql.jdbc.Connection, and the java.sql.Connection class. The two of them throw the same error. I looked for other similar questions and none of those solved the problem, what else can I do? where can I download an old J/Connection driver ? should I reinstall the whole netbeans?
I wonder...
these are my methods with the unnecessary code commented:
public static final String URL = "jdbc:mysql://localhost:3306/escuela";
public static final String USERNAME = "root";
public static final String PASSWORD = "123";
public static Connection getConnection(){
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(URL, USERNAME, PASSWORD); //here
fails.
JOptionPane.showMessageDialog(null, "Conexion exitosa.");
}
catch(Exception e){
e.printStackTrace();
}
return con;
}
private void btnConectarActionPerformed(java.awt.event.ActionEvent evt) {
Connection con = getConnection(); //I call the connection here
// Statement st;
// ResultSet rs;
//
// try{
// st = con.createStatement();
// rs = st.executeQuery("SELECT * FROM personas");
//
// while(rs.next()){
// JOptionPane.showMessageDialog(null, rs.getString("nombre")
// + " " + rs.getString("telefono"));
// }
// con.close();
// }
// catch(Exception e){
// System.out.println(e);
// }
}
The whole e.printStackTrace:
java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1062)
at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3556)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2513)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
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:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at App.conexion.getConnection(conexion.java:93)
at App.conexion.btnConectarActionPerformed(conexion.java:70)
at App.conexion.access$000(conexion.java:19)
at App.conexion$1.actionPerformed(conexion.java:45)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
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:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ClassCastException: java.math.BigInteger cannot be cast
to java.lang.Long
at
com.mysql.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1007)
... 54 more
I finally made it thanks to Cirille Con Morales,thanks a lot for the link and the download instruction. My Netbeans has an old installation of MySql connector driver (5.xxx) I downloaded the (8.0.17) version, added to the global libraries and I hed other issues but digging in google I resolve them, this pats must be changed:
I used the driver has an string url :
public static final String DRIVER = "com.mysql.cj.jdbc.Driver";
Then an error abour timezine appeared, so to fix this the URL should be modified whith this:
public static final String URL =
"jdbc:mysql://localhost:3306/escuela?
useUnicode=true&useJDBCCompliantTimezoneShift
=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
Then I used it like this:
public static Connection getConnection(){
Connection con = null;
try{
Class.forName(DRIVER);
con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
JOptionPane.showMessageDialog(null, "Conexion exitosa.");
}
catch(Exception e){
e.printStackTrace();
}
return con;
}
Regards!
This question already has answers here:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
(51 answers)
Closed 4 years ago.
I am creating an application and I need to connect to a remote MySql DB. I am using JSch to connect via SSH. I am able to connect via JSch, however when I try to connect to my database I keep getting errors.
public void connectSSH() {
int lport = 5656;
String rhost = "host"; //not actual host
String host = "host"; //not actual host
int rport = 3306;
String user = "user"; //not actual user
String password = "*********";
String dbuserName = "user"; //not actual user
String dbpassword = "********";
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:" + rport + "?user=" + dbuserName + "&password=" + dbpassword + "&useSSL=true";
Connection conn = null;
Session session = null;
try {
//Set StrictHostKeyChecking property to no to avoid UnknownHostKey issue
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");
int assinged_port = session.setPortForwardingL(lport, rhost, rport);
System.out.println("localhost:" + assinged_port + " -> " + rhost + ":" + rport);
System.out.println("Port Forwarded");
//mysql database connectivity
Class.forName(driverName).newInstance();
conn = DriverManager.getConnection(url);
System.out.println("Database connection established");
System.out.println("DONE");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null && !conn.isClosed()) {
System.out.println("Closing Database Connection");
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
if (session != null && session.isConnected()) {
System.out.println("Closing SSH Connection");
session.disconnect();
}
}
}
Output:
Connected
localhost:5656 -> rhost:3306
Port Forwarded
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:270)
at com.ej.ozyazgan.classes.SqlHelper.connectSSH(SqlHelper.java:176)
at com.ej.ozyazgan.classes.MainController.initialize(MainController.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at com.ej.ozyazgan.classes.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
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.PlainSocketImpl.connect(PlainSocketImpl.java:172)
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)
... 43 more
Closing SSH Connection
The url cannot be null
Caused by: java.net.ConnectException: Connection refused: connect
You didn't read the stack trace. The actual error here is 'connection refused'. The port in the JDBC URL should be the local SSH forwarding port, not the remote port.
I have a remotely hosted database. I had been working on an FX application for which I connected to localhost for reading and writing data from database. But now I want to connect my desktop application to the remotely hosted database and read and write data from there. But it shows me the following error.
SEVERE: null
java.sql.SQLException: null, message from server: "Host '103.253.39.4' is not allowed to connect to this MySQL server"
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1037)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2239)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2270)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2069)
at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:794)
at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:44)
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:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at com.garciaPlumbing.model.DatabaseManager.connect(DatabaseManager.java:38)
at com.garciaPlumbing.model.FittingDetails.getAllSuppliers(FittingDetails.java:155)
at com.garciaPlumbing.view.Layout_AddFittingType.(Layout_AddFittingType.java:52)
at com.garciaPlumbing.view.Layout_InsertFittingDetails.(Layout_InsertFittingDetails.java:37)
at com.garciaPlumbing.view.MenuItemTask.(MenuItemTask.java:23)
at com.garciaPlumbing.view.BasicLayout.(BasicLayout.java:40)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplicationWithArgs$148(LauncherImpl.java:327)
at com.sun.javafx.application.LauncherImpl$$Lambda$43/445051633.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$44/1775282465.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/2028529302.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/2040495657.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1558600329.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.NullPointerException
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:358)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
... 5 more
java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplicationWithArgs$148(LauncherImpl.java:327)
at com.sun.javafx.application.LauncherImpl$$Lambda$43/445051633.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$44/1775282465.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/2028529302.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/2040495657.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1558600329.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at com.garciaPlumbing.model.FittingDetails.getAllSuppliers(FittingDetails.java:171)
at com.garciaPlumbing.view.Layout_AddFittingType.(Layout_AddFittingType.java:52)
at com.garciaPlumbing.view.Layout_InsertFittingDetails.(Layout_InsertFittingDetails.java:37)
at com.garciaPlumbing.view.MenuItemTask.(MenuItemTask.java:23)
at com.garciaPlumbing.view.BasicLayout.(BasicLayout.java:40)
... 16 more
This what I have done to establish the connection.
public class DatabaseManager {
private static final String CLASS_FORNAME = "com.mysql.jdbc.Driver";
private static final String DATABASE_LOCATION = "jdbc:mysql://mysql.2freehosting.com/";//"jdbc:mysql://localhost:3306/";
private static final String DATABASE_NAME = "u821323799_grc?";//"GarciaPlumbing2";
private static final String USERNAME = "u821323799_grc";//"root";
protected static Connection connection = null;
protected static PreparedStatement statement = null;
/**
*
* Connect() method is responsible for establishing any connection to database.
*
*/
public static void connect(){
try {
Class.forName(CLASS_FORNAME);
connection = (Connection) DriverManager.getConnection(
"jdbc:mysql://mysql.2freehosting.com/u821323799_grc?"
+ "user=u821323799_grc&password=celloviii1");
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(DatabaseManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Please someone help me out with a better solution.
Thanks in advance.
java.sql.SQLException: null, message from server: "Host
'103.253.39.4' is not allowed to connect to this MySQL server"
It means that the MySQL Server at the remote system is not configured to accept remote connection from 103.253.39.4 with the provided credentials. Please configure the remote MySQL Server to accept remote connection and try again.
You may try: How to allow remote connection to mysql
=================Code===================
package com.name.file;
import java.sql.*;
public class JDBC {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://162.219.26.220:3306/waseianc_db";
// Database credentials
static final String USER = "waseianc_vinit";
static final String PASS = "pass";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, first, last, age FROM Employees";
ResultSet rs = stmt.executeQuery(sql);
// STEP 5: Extract data from result set
while (rs.next()) {
// Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
// Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
// STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
}
======================Errors========================
Connecting to database...
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 2 ms ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2104)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:729)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.name.file.JDBC.main(JDBC.java:24)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2027)
... 12 more
=============================== Cpanel Remote Access ====================
=========================== Cpanel MySQL DataBase =========================
Yes, Please contact your hosting provider and ask them to enable mysql port 3306 in firewall and try to login again.
I'm trying execute a java application in a client computer from a server. But the application, when i try open this, not exetecute.
Here my class Conexion:
package Conexion;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class Conexion {
private static final String database = "recursos_humanos";
private static final String login = "root";
private static final String password = "1234";
private static final String url = "jdbc:mysql://localhost:3306/"+database;
public static Connection connection;
public static Statement stm;
public static ResultSet rs;
public static PreparedStatement ps;
public static Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url,login,password);
if(connection != null)
System.out.println("Base de Datos " +database +" lista.");
} catch (ClassNotFoundException ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(null, "No se pudo coenctar: "+ex);
}catch(SQLException ex){System.out.println(ex);}
return connection;
}
}
Help me please and thank's.
EDIT
I made the change, i erase "String url = "jdbc:mysql://localhost:3306/"+database;"
and put the ip address of server; "String url = "jdbc:mysql://10.6.12.247:3306/"+database;" but the RESULT was as follows:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection timed out: connect
STACKTRACE:
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Ventanas.VentanaPrincipal.listar(VentanaPrincipal.java:387)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at Ventanas.VentanaPrincipal.<init>(VentanaPrincipal.java:76)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at Ventanas.VentanaPrincipal$11.run(VentanaPrincipal.java:356)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.net.Socket.connect(Socket.java:538)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.net.Socket.<init>(Socket.java:434)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.net.Socket.<init>(Socket.java:244)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at java.security.AccessController.doPrivileged(Native Method)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at Conexion.Conexion.getConnection(Conexion.java:34)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at Ventanas.VentanaPrincipal.listar(VentanaPrincipal.java:385)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
at Ventanas.VentanaPrincipal.<init>(VentanaPrincipal.java:76)
at Ventanas.VentanaPrincipal$11.run(VentanaPrincipal.java:356)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
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.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
** END NESTED EXCEPTION **
Last packet sent to the server was 1 ms ago.
Tell me about that please. Thank's.
Your error lies here.
private static final String url = "jdbc:mysql://localhost:3306/"+database;
Localhost is the computer in which the application is running, in this case, the client computer.
In order to actually connect to the server, the IP of the server must be in place of "localhost".
The correct implementation would be as represented below:
String ServerAddress = "192.168.1.4";
private static final String url = "jdbc:mysql://" + ServerAddress + ":3306/"+database;
Regards
Your application is trying to establish a jdbc connection to localhost which am sure does not have mysql running
Just change your code as follows:
url = "jdbc:mysql://IPofYourServer:3306/"+database;
● Replace localhost to the ip address of your server in your code.
Just change that localhost with your server name and it will work.
But make sure that your database must reside on that server.