Just nu i M3-nätverket
Jump to content

Ansluta till Mysql i Java


Nexustwins.com

Recommended Posts

Nexustwins.com

Tjenare, jag har lite problem med med att ansluta till en mysql databas i Java. Felmeddelandet jag får, verkar svårt att tolka.

 

 



import java.sql.*;

public class getmySQLConnection 
{
       public static void main(String[] args) 
       {
               DB db = new DB();
               Connection conn=db.dbConnect(
                 "jdbc:mysql://server.nexustwins.com:3306/cbd", "***", "**");
       }

}

class DB
{
       public DB() {}

       public Connection dbConnect(String db_connect_string,
         String db_userid, String db_password)
       {
               try
               {
                       Class.forName("com.mysql.jdbc.Driver").newInstance();
                       Connection conn = DriverManager.getConnection(
                         db_connect_string, db_userid, db_password);

                       System.out.println("connected");
                       return conn;

               }
               catch (Exception e)
               {
                       e.printStackTrace();
                       return null;
               }
       }
};

 

Och här är felmeddelandet:

 

 

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
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:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2120)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
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:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at DB.dbConnect(getmySQLConnection.java:24)
at getmySQLConnection.main(getmySQLConnection.java:8)
at __SHELL5.run(__SHELL5.java:7)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at bluej.runtime.ExecServer$3.run(ExecServer.java:819)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
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:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:335)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2043)
... 19 more
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:209)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:253)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:284)
... 20 more

 

Det vore mycket vänligt, om någon kunde hjälpa mig att tolka meddelandet.

 

Tack på förhand!

 

Link to comment
Share on other sites

  • 4 months later...

Känner igen felet, men det var länge sedan sist :-)

 

Toppkandidater:

1. MySQL användaren är korrekt och lösenordet likaså, men du har inte definierat att användaren får ansluta från viss IP-adress.

2. En annan del av programmet stänger ner anslutningen till databasen efter avslutad fråga.

3. Ditt program öppnar nya anslutingar hela tiden och stänger dem inte lika fort. Efter ett tag når du MySQL's gränsvärde för anslutningar från din applikation.

 

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.



×
×
  • Create New...