无法使用jdbc驱动程序连接到本地主机上的mysql

lrpiutwd  于 2021-06-18  发布在  Mysql
关注(0)|答案(3)|浏览(331)

这个问题在这里已经有答案了

无法从java连接到mysql:mysql驱动程序连接逻辑中的nullpointerexception(3个答案)
两年前关门了。
我无法连接到在我自己的机器上运行的mysql示例,但我对情况了解不够,无法诊断问题。

public void readDataBase() throws Exception {
    Connection connection = null;
    Class.forName("com.mysql.jdbc.Driver");

    String url = "jdbc:mysql://localhost:3306/tmlschedule";

    try {

        connection = DriverManager.getConnection(url, "user", "pass");

    } catch (Exception e) {
        throw e;
    } finally {
        close();
    }
}

呼叫 .getConnection() 具有以下堆栈跟踪的抛出:

Exception in thread "main"
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
Could not create connection to database 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:411)     at
com.mysql.jdbc.Util.getInstance(Util.java:386)  at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)  at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)   at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)   at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)   at
com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2570)
    at
com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2306)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:839)    at
com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)  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:411)     at
com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:421)  at
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:350)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)     at
java.sql.DriverManager.getConnection(DriverManager.java:247)    at
tmlSchedule.MySQLAccess.readDataBase(MySQLAccess.java:22)   at
tmlSchedule.TmlSchedule.main(TmlSchedule.java:9) Caused by:
java.lang.NullPointerException  at
com.mysql.jdbc.ConnectionImpl.getServerCharacterEncoding(ConnectionImpl.java:3281)
    at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1940)
    at
com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1866)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1252)    at
com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2488)
    at
com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2521)
    ... 14 more

到目前为止我所做的:
确认表名、用户名和密码。我可以使用所有这些变量登录mysql shell( mysql -u user -p )并运行选择和插入命令。
用mysql示例上根用户的登录详细信息替换用户名和密码。
在eclipse上的java构建路径菜单中引用mysql-connector-java.jar
添加 mysql 通过firewalld的gui作为一个可信任的服务。
在禁用防火墙的情况下运行代码( sudo systemctl stop firewalld ). 这没什么区别。
检查mysql是否正在侦听端口3306。 sudo netstat -tuplen 包括以下条目: tcp6 0 0 :::3306 :::* LISTEN 27 113879 7224/mysqld 任何帮助都将不胜感激。
编辑:
我对mysql和驱动程序的版本控制感到非常困惑。 $ zipgrep 'Bundle-Version' /usr/share/java/mysql-connector-java.jar 退货:
meta inf/manifest.mf:捆绑包版本:5.1.25$ mysql --version` 退货:
mysql ver 8.0.12 for linux on x86\ U 64(mysql社区服务器-gpl)
这表明驱动程序已经过时了,但是我通过这里列出的yum repo安装了mysql,我可以想象它会为我提供一个与数据库匹配的驱动程序。我还尝试通过此处列出的rpm软件包安装connector/j 8.0,但收到以下消息,这对我来说毫无意义:
包mysql-connector-java-1:5.1.25-3.el7.noarch(比mysql-connector-java-8.0.12-1.el7.noarch更新)已经安装

bmvo0sr5

bmvo0sr51#

如果你的mysql版本是8.x,驱动程序类,那么你的驱动程序版本很可能太旧了 com.mysql.jdbc.Driver 已弃用。新的驱动程序类为 com.mysql.cj.jdbc.Driver . 驱动程序是通过spi自动注册的,通常不需要手动加载驱动程序类。以下代码来自官方文档:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

Connection conn = null;
...
try {
    conn =
       DriverManager.getConnection("jdbc:mysql://localhost/test?" +
                                   "user=minty&password=greatsqldb");

    // 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());
}
gdrx4gfi

gdrx4gfi2#

我不清楚所提供的代码到底发生了什么,但是,当您构建一个数据访问对象时,您可能需要考虑以这样的方式构建它:您的连接是您的类的一个变量,而不是类的方法。
此外,默认情况下,mysql数据库通常需要安全连接。要删除此默认值,请查看url变量。

public class DataAccessExample {

private Connection connection;
private final String driverClass = "com.mysql.jdbc.Driver";
private final String url = "jdbc:mysql://localhost:3306/tmlschedule?autoReconnect=true&useSSL=false";
private final String userName = "YOUR USERNAME";
private final String password = "YOUR PASSWORD";

//Consider calling this openConnection();
public void readDataBase() throws ClassNotFoundException, SQLException {
    Class.forName(driverClass);
    connection = DriverManager.getConnection(url, userName, password);
}

//Then have a close connection, this will make your life easier in the future
public void closeConnection() throws SQLException {
    if (connection != null) {
        connection.close();
    }
}

}
要使用此代码:

public static void main(String[] args) {
    DataAccessExample d = new DataAccessExample();

    try {
        d.readDataBase();
        d.closeConnection();
    } catch (ClassNotFoundException | SQLException ex) {
        Logger.getLogger(DataAccessExample.class.getName()).log(Level.SEVERE, null, ex);
    }
}

最后,确保数据库名称拼写正确,用户名和密码正确。
我希望这有帮助。

mspsb9vt

mspsb9vt3#

由于多种原因,应用程序可能无法连接到数据库。其中之一是没有使用正确的驱动程序软件(又称jdbc驱动程序)。
在本例中,验证与数据库服务器本身兼容的jdbc驱动程序版本的使用情况解决了问题。
你可以从这里得到一个正确的驱动程序软件。
可以通过命令提示符验证mysql服务器的版本:首先,启动一个mysql客户机,然后使用命令 mysql> select version();

相关问题