我想在java和mysql之间创建数据库连接。这是我的代码:
import java.sql.*;
public class Java2MySql {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/";
String dbName = "sh";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "shakti";
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
Statement st = conn.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM shakti");
while (res.next()) {
int id = res.getInt("id");
String msg = res.getString("name");
System.out.println(id + "\t" + msg);
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我收到以下错误:
java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at Java2MySql.main(Java2MySql.java:13)
任何人都可以帮我解决问题吗?