【问题标题】:I can't connect to my sql and my driver is not found [duplicate]我无法连接到我的 sql 并且找不到我的驱动程序 [重复]
【发布时间】:2014-08-16 10:06:00
【问题描述】:

我的数据库控制器中有这段代码

package nypHeritage.database;
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement;

public class DBController {
    //Declaration of attributes
    private Connection con; 

    /********************************************************
     * Method Name : testDriver
     * Input Parameter : nil 
     * Purpose : To test if the driver is properly installed
     * Return :nil
     *******************************************************/
    public void testDriver() throws Exception { 
        System.out.println("Initializing Server... "); 
    try { 
        Class.forName("org.gjt.mm.mysql.Driver"); 
        System.out.println(" Driver Found."); 
    } 
    catch (ClassNotFoundException e) { 
        System.out.println(" Driver Not Found, exiting.."); 
        throw (e); 
        } 
    } 
    public void getConnection(){ 
        String url = ""; 
        try { 
            url = "jdbc:mysql://localhost/nyp_heritage"; 
            con = DriverManager.getConnection(url, "IT1639User", "user"); 
            System.out.println("Successfully connected to " + url+ "."); 
        } 
        catch (java.sql.SQLException e) { 
            System.out.println("Connection failed ->"+ url); 
            System.out.println(e); 
        } 
    } 
    /************************************************************
     * Method Name : readRequest 
     * Input Parameter : String (database query) 
     * Purpose : Obtain the result set from the db query 
     * Return : resultSet (records from the query)
     ************************************************************/
    public ResultSet readRequest(String dbQuery) {
        ResultSet rs = null;
        System.out.println("DB Query: " + dbQuery);
        try {
            // create a statement object
            Statement stmt = con.createStatement();
            // execute an SQL query and get the result
            rs = stmt.executeQuery(dbQuery);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }
    /***********************************************************
     * Method Name : updateRequest 
     * Input Parameter : String (database query) 
     * Purpose : Execute update using the db query 
     * Return : int (count is 1 if successful)
     ***********************************************************/
    public int updateRequest(String dbQuery) {
        int count = 0;
        System.out.println("DB Query: " + dbQuery);
        try {
            // create a statement object
            Statement stmt = con.createStatement();
            // execute an SQL query and get the result
            count = stmt.executeUpdate(dbQuery);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }
    /***********************************************************
     * Method Name : terminate 
     * Input Parameter : nil 
     * Purpose : Close db conection 
     * Return :nil
     **********************************************************/
    public void terminate() {
        // close connection
        try {
            con.close();
            System.out.println("Connection is closed");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] arg)throws Exception{
        DBController db = new DBController();
        db.testDriver();
    }
}

过去 2 个月运行良好,但现在当我重新打开它时,它显示了这个错误

Connection failed ->jdbc:mysql://localhost/nyp_heritage
java.lang.NullPointerException
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/nyp_heritage
DB Query: INSERT INTO PARTICIPANT(name, mobile, monthOfEvent, eventName, dateBirth, address, email, gender) VALUES ('Samantha Jones','+6590287645', 'december', 'Heritage Centres Tour', '19/05/1990', 'Woodlands', 'jones@gmail.com', 'Female')

它说,我的连接失败,并且没有为我的本地主机找到合适的驱动程序。请帮助!我不确定如何解决这个问题...

【问题讨论】:

标签: java mysql jdbc


【解决方案1】:

首先,确保您的类路径中有一个适当的connector 用于mysql。使用 JDBC 4.0 及更高版本您不需要

Class.forName("org.gjt.mm.mysql.Driver");  // which should be "com.mysql.jdbc.Driver"
                                           // since the one you appear to be trying
                                           // is truly ancient.

来自DriverManagerjavadoc,

应用程序不再需要使用 Class.forName() 显式加载 JDBC 驱动程序。

【讨论】:

  • 想知道为什么您如此确定您的答案可以解决问题吗?你不认为你的回答只是一个评论吗?你检查了整个代码,所以可能还有其他问题?
  • @KickButtowski 因为我使用过 Java 和 MySQL,这只是连接器/j 问题的恶臭。不,我真的在解决问题中的执行:“没有找到适合 jdbc:mysql://localhost/nyp_heritage 的驱动程序”
  • 我不想批评任何人只是想了解你的思路因为你看起来很专业
【解决方案2】:

根据我的发现

首先,

org.gjt.mm.mysql.Driver is out dated. 

所以你应该在Class.forName 部分使用它

 com.mysql.jdbc.Driver

资源: What is the jdbc driver "org.gjt.mm.mysql.Driver" for?

第二,

您应该检查您的项目中是否添加了适当的 J 连接器 jar 文件 你可以在这里查看http://dev.mysql.com/downloads/connector/j/

第三, 您可以将 try catch 块与资源一起使用,以确保您打开的任何内容都已关闭(如果可以关闭)

例如

public void update(String name, String tel, String id) throws SQLException {
        System.out.println("in update part tel is " + tel);
        String sql = "UPDATE APP.DBNAME "
                + "SET NAME=? , TEL=? WHERE NAME=?";
        try (PreparedStatement ps = getConn().prepareCall(sql)) {
            ps.setString(1, name);
            ps.setString(2, tel);
            ps.setString(3, id);
            ps.executeUpdate();
        }
    }

第四,

JDBC 4.0 功能

感谢 Mustang 中包含的 Java SE Service Provider 机制, Java 开发人员不再需要使用显式加载 JDBC 驱动程序 像 Class.forName() 这样的代码来注册一个 JDBC 驱动程序。 DriverManager 类通过自动定位合适的驱动程序来解决这个问题 当调用 DriverManager.getConnection() 方法时。这项特征 向后兼容,因此无需对现有 JDBC 进行任何更改 代码。

资源: http://www.onjava.com/2006/08/02/jjdbc-4-enhancements-in-java-se-6.html

【讨论】:

    【解决方案3】:

    改变

    Class.forName("org.gjt.mm.mysql.Driver");
    

    Class.forName("com.mysql.jdbc.Driver");
    

    【讨论】:

      猜你喜欢
      • 2018-02-10
      • 2016-02-18
      • 2019-07-15
      • 1970-01-01
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多