首先下载一个jdbc驱动包~
http://pan.baidu.com/s/1jHnDkcI(我的云盘里有资源)
再然后~
http://jingyan.baidu.com/article/fb48e8be386b7c6e622e1488.html(这条经验里说得很清楚,如何一步步把驱动包导入)
最后的测试代码:
import java.sql.*;
public class conn {
public static void main(String[] args)throws Exception{
try{
//调用Class.forName()方法加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
System.out.println("成功加载MySQL驱动!");
String url="jdbc:mysql://127.0.0.1:3306/random"; //JDBC的URL
Connection conn;
conn = DriverManager.getConnection(url,"root","");
Statement stmt = conn.createStatement(); //创建Statement对象
System.out.println("成功连接到数据库!");
//查询数据的代码
String sql = "select * from student_info"; //要执行的SQL
ResultSet rs = stmt.executeQuery(sql);//创建数据对象
System.out.println("学号"+"\t"+"姓名"+"\t"+"电话");
while (rs.next()){
System.out.print(rs.getInt(1) + "\t");
System.out.print(rs.getString(2) + "\t");
System.out.print(rs.getInt(3) + "\t");
System.out.println();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}