Java连接MySQL有错误Sorry,can`t find the Driver!,请各位看一下
Java连接MySQL有错误Sorry,can`t find the Driver!,请各位看一下
这是提示的错误
Sorry,can`t find the Driver!
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at JDBCTest.main(JDBCTest.java:11)
下面是Java代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCTest {
public static void main(String[] args){
try {
// 加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
// URL指向要访问的数据库名test
String url = "jdbc:mysql://127.0.0.1:3306/test";
// MySQL配置时的用户名
String user = "root";
// MySQL配置时的密码
String password = "123456";
// 连续数据库
Connection connection = DriverManager.getConnection(url, user, password);
if(!connection.isClosed())
System.out.println("Succeeded connecting to the Database!");
// statement用来执行SQL语句
Statement statement = connection.createStatement();
// 要执行的SQL语句
String sql = "select * from student";
// 执行SQL语句并返回结果集
ResultSet rs = statement.executeQuery(sql);
System.out.println(" 学号" + "\t" + " 姓名");
String name = null;
while(rs.next()) {
// 选择sname这列数据
name = rs.getString("sname");
// 输出结果
System.out.println(rs.getString("sno") + "\t" + name);
}
// 关闭结果集
rs.close();
// 关闭连接
connection.close();
} catch(ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}
将你的驱动jar包拖到WEN_INF/lib里面试试看怎么操作啊,我不会弄,能具体点吗?两种方法:
1 项目上右击--》Build Path--》Configure Build Path,默认应该是在Libraries那个Tab,然后点Add External Jars,找到你的驱动Jar.
2 直接将你的Jar复制粘贴到项目WEN_INF/lib目录下