0

I have this code in my Database controller

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

It was perfectly working last 2 months but now when I reopened it, it showed me this error

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')

It says, my connection is failed and that no suitable driver was found for my localhost. HELP PLS! I'm not sure how to fix this problem...

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
zelo
  • 15
  • 1
  • 4

3 Answers3

1

First, make sure you have an appropriate connector for mysql on your classpath. With JDBC 4.0 and up you don't need

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.

From the DriverManager javadoc,

Applications no longer need to explictly load JDBC drivers using Class.forName().

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 1
    Wondering to know why you are so sure that your answer can solve the issue? do not you think your answer is just a comment ? did you check the whole code so may be there are other issues ? – Kick Buttowski Jun 25 '14 at 18:12
  • 1
    @KickButtowski Because I've used Java and MySQL and this just reeks of connector/j issues. And no, I'm really addressing the execption in the question: "No suitable driver found for jdbc:mysql://localhost/nyp_heritage" – Elliott Frisch Jun 25 '14 at 18:13
  • I do not want to criticize anybody just I want to understand your train of thoughts cuz it seems you are professinal – Kick Buttowski Jun 25 '14 at 18:14
1

change

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

to

Class.forName("com.mysql.jdbc.Driver");
SparkOn
  • 8,806
  • 4
  • 29
  • 34
0

Based on what I have found the

First,

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

so you should use this instead in Class.forName section

 com.mysql.jdbc.Driver

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

Second,

you should check whether you have appropriate J connector jar file added in your project you can check this in here http://dev.mysql.com/downloads/connector/j/

Third, you can use try catch block with resources to be sure that whatever you have opened is closed if it is close-able

For example

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

Fourth,

JDBC 4.0 Features

Thanks to the Java SE Service Provider mechanism included in Mustang, Java developers no longer need to explicitly load JDBC drivers using code like Class.forName() to register a JDBC driver. The DriverManager class takes care of this by automatically locating a suitable driver when the DriverManager.getConnection() method is called. This feature is backward-compatible, so no changes are needed to the existing JDBC code.

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

Community
  • 1
  • 1
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58