加载驱动问题,真的不想麻烦大家,可是我还是做不出来
package com.imooc.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class jdbc_test {
private static final String url="jdbc:mysql://192.168.0.101:3306/imooc";
private static final String user="root";
private static final String password="root";
public static void main(String[] args) throws ClassNotFoundException {
//加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//Class.forName("com.sun.java.util.jar.pack.Driver");
//创建连接
try {
Connection conn=DriverManager.getConnection(url, user, password);
Statement stmt=conn.createStatement();
//访问数据库进行查询
ResultSet reset=stmt.executeQuery("select user_name,age from imooc_goddness");
while(reset.next()){
System.out.println(reset.getString("user_name")+","+reset.getInt("age"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
异常:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
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 com.imooc.db.jdbc_test.main(jdbc_test.java:16)