网上的资料真烂,千篇一律的拷贝的,根本不能用,鄙视! 正题: 到MYSQL网站下载mysql-connector-java-5.0.4.zip文件,解压; 解压后有一个文件:mysql-connector-java-5.0.4-bin.jar 把这个文件放到应用的lib包里或者写一个classpath确定绝对地址。 package org.sytuyuan; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; public class MySqlTest { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost/test", "root", "123456"); Statement stmt = con.createStatement(); // stmt.execute("insert into user values('123',null,null,null) "); ResultSet rs = stmt.executeQuery("select * from user"); while (rs.next()) { System.out.println(rs.getString("userid")); } rs.close(); con.close(); } catch (ClassNotFoundException e) { System.out.println(e.getMessage()); } catch (SQLException ex) { System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } } }