首页 > 解决方案 > java项目无法通过JDBC连接MYSQL,找不到com.mysql.cj.jdbc.Driver

问题描述

我正在尝试使用 MYSQL Connector/j8.0 将我的项目连接到 MYSQL 数据库。我尝试了许多不同的技术,但仍然无法连接。在我遇到错误之前,但我能够使用 try and catch 对它们进行排序,而且我认为我已经正确安装了连接器。请帮忙。

这是我的代码(我遵循 MYSQL 文档:https ://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-connect-drivermanager.html#connector-j-examples-connection- drivermanager ): 导入 java.sql.*;

public class GSM_Mobile_main {
    // this is start of the application
    static Connection conn = null;
    public static void main(String[] args) {
        try {
            // The newInstance() call is a work around for some
            // broken Java implementations
            Class.forName("com.mysql.cj.jdbc.Driver").newInstance();

            try {
                conn =
 DriverManager.getConnection("jdbc:mysql://localhost:3306/GSM_Mobile?  useSSL=false","root","Capris55");

                // Do something with the Connection
                Statement stmt = null;
                ResultSet rs = null;

                try {
                    stmt = conn.createStatement();
                    rs = stmt.executeQuery("SELECT * FROM receipts");
                    // Now do something with the ResultSet ....
                    while(rs.next()){
                        System.out.println("ref ID="+rs.getInt("referenceNumber")+", Name="+rs.getString("customerName"));
                    }
                }
                catch (SQLException ex){
                    // handle any errors
                    System.out.println("SQLException: " + ex.getMessage());
                    System.out.println("SQLState: " + ex.getSQLState());
                    System.out.println("VendorError: " + ex.getErrorCode());
                }
                finally {
                    // it is a good idea to release
                    // resources in a finally{} block
                    // in reverse-order of their creation
                    // if they are no-longer needed

                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException sqlEx) { } // ignore

                    rs = null;
                }

                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException sqlEx) { } // ignore

                    stmt = null;
                }
            }
        } catch (SQLException ex) {
            // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }
    } catch (Exception ex) {
        // handle the error
    }

}


}

标签: javamysqljdbcmysql-connector

解决方案


推荐阅读