开发者

Connecting to MySql on Mac: No suitable driver error

开发者 https://www.devze.com 2023-01-09 07:58 出处:网络
I am getting the error: java.sql.SQLException: No suitable driver I have imported the .jar file that is available here http://dev.mysql.com/downloads/mirror.php?id=13598

I am getting the error: java.sql.SQLException: No suitable driver

I have imported the .jar file that is available here http://dev.mysql.com/downloads/mirror.php?id=13598

I am using Eclipse.

I am connecting to the DB with this code:

package dao;

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

public class DBCon {

    private static final String host = "jdbc:mysql://localhost";
    private static final String port = "3306";
    private static final String db = "mydb";
//      private static final String user = "root";
//      private static final String pwd = "";
    private static final String user = "myusername";
    private static final String pwd = "mypwd";

    public Connection getCon() {
        Connection con = null;
        try {
            String url = host + ":" + port + "/" + db;

            con 开发者_StackOverflow中文版= DriverManager.getConnection(url, user, pwd);

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return con;
    }
}

I then query the DB with this code:

package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.sql.Connection;
import model.ClassPojo;

public class ARCSDao {

public ArrayList<ClassPojo> viewClasses() throws SQLException{
    ArrayList<ClassPojo> classList = new ArrayList<ClassPojo>();

    DBCon db = new DBCon();
    Connection con = db.getCon();
    Statement stmt;
    ResultSet rs;
    stmt = con.createStatement();
    rs = stmt.executeQuery("SELECT * FROM classes");
    while(rs.next()){
           ClassPojo aClass = new ClassPojo();
           rs.getString("class_name");
           rs.getString("class_uri");
           classList.add(aClass);
    }           
    return classList;
    }
}


Add

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

before

con = DriverManager.getConnection(url, user, pwd);

if this doesn't work double check that the jdbc driver (jar file) is included in your classpath

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号