Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 连接到Mac上的MySql:没有合适的驱动程序错误_Java_Mysql - Fatal编程技术网

Java 连接到Mac上的MySql:没有合适的驱动程序错误

Java 连接到Mac上的MySql:没有合适的驱动程序错误,java,mysql,Java,Mysql,我得到一个错误:java.sql.SQLException:没有合适的驱动程序 我已经导入了这里可用的.jar文件 我正在使用Eclipse 我使用以下代码连接到数据库: package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBCon { private static final String host =

我得到一个错误:java.sql.SQLException:没有合适的驱动程序

我已经导入了这里可用的.jar文件

我正在使用Eclipse

我使用以下代码连接到数据库:

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 = DriverManager.getConnection(url, user, pwd);

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return con;
    }
}
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;
    }
}
然后,我用以下代码查询数据库:

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 = DriverManager.getConnection(url, user, pwd);

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return con;
    }
}
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;
    }
}
包dao;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.sql.Statement;
导入java.util.ArrayList;
导入java.sql.Connection;
导入model.ClassPojo;
公共类ARCSDao{
公共ArrayList viewClasses()引发SQLException{
ArrayList类列表=新的ArrayList();
DBCon db=new DBCon();
连接con=db.getCon();
报表stmt;
结果集rs;
stmt=con.createStatement();
rs=stmt.executeQuery(“从类中选择*);
while(rs.next()){
ClassPojo aClass=newclasspojo();
rs.getString(“类别名称”);
rs.getString(“class_uri”);
添加(aClass);
}           
返回类列表;
}
}
添加

以前

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

如果这不起作用,请仔细检查jdbc驱动程序(jar文件)是否包含在您的类路径中

我过去在windows设备上进行开发时没有遇到任何问题,现在我在mac上进行此操作-不确定这是否会导致任何问题。我看不出这是怎么回事,因为这是用Java编写的,我在这台机器上做过非db编码。mysql服务器在同一个机器上吗?那么,检查一下您使用的mysql版本怎么样?不,它不在同一个框中,我实际上使用的是一个使用端口转发的远程mysql服务器,但我将尝试检查mysql的版本。如果您不想连接到远程计算机,则需要在jdbc中更改localhost:mysql://localhost 你的数据库主机名。@stacker-我知道这通常是有道理的。设置远程数据库的人说他们已经设置了端口转发,所以我只需添加localhost;com.mysql.jdbc.CommunicationsException:通信链路故障-我将处理这个问题。