一尘不染

如何检查mysql连接的默认端口号?连接不起作用!jdbc连接

java

我正在尝试使用java文件中的jdbc与数据库连接。它根本没有连接,并且不断给我错误。我想这是因为端口号,因为所有其他数据(例如用户名,密码和其他代码)似乎都是正确的。

我想检查默认端口号,以便可以正确尝试。我确实尝试过使用这8080、80和3306的全部三个,但这显示了错误。

这里的端口8080用于HTTP服务器,研究中默认端口3306为默认端口,默认为80。

try {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Driver found");
    } catch (ClassNotFoundException e) {
        System.out.println("Driver not found");
    }

    String url="jdbc:mysql://localhost:8080 or 80 or 3306 or without port number/test";
    String user="user";
    String password="";

    Connection con=null;

    try {
        con=DriverManager.getConnection(url, user, password);
    System.out.println("Success");
    } catch (SQLException e){
         e.printStackTrace();
    }
}

使用String url =“ jdbc:mysql:// localhost:3306 / test”时,错误在下面给出;

  Driver found
  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:377)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1036)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2232)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
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:377)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:395)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at com.town.connect.Dbconnection.main(Dbconnection.java:26)
  Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:382)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:241)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:228)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:431)
at java.net.Socket.connect(Socket.java:527)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:213)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297)
... 15 more

阅读 348

收藏
2020-12-03

共1个答案

一尘不染

您可以通过以下方式访问这些设置

mysql> show variables;
2020-12-03