Eclipse加载MySQL驱动步骤

eclipse下编辑链接MySQL数据库时,若出现Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc. 说明数据库驱动还没加载。加载方式如下:

从http://dev.mysql.com/downloads/connector/j/ 下载驱动压缩包(Window下为.zip) 然后解压出来mysql-connector-java-5.0.8-bin.jar(我的版本为5.0.8),接着在Eclipse下项目-属性-Java构建路径-库-添加外部Jar,添加刚解压的jar就行了。配连接MySQL基本语句如下:


public class MysqlTest {

	public static void main(String[] args) throws Exception {
		// TODO 自动生成的方法存根
		Connection conn = null;
		String sql;
		String url = "jdbc:mysql://localhost:3306/d_studentinfo";
		String user = "root";
		String pwd = "root";

		Class.forName("com.mysql.jdbc.Driver");
		System.out.println("数据库驱动加载成功");
		conn = DriverManager.getConnection(url, user, pwd);
		java.sql.Statement statement = null;
		statement = conn.createStatement();
		sql = "select * from t_stud where age=45";
		ResultSet rs = null;
		rs = statement.executeQuery(sql);
		while (rs.next())
			System.out.println(rs.getString("name"));
		rs.close();
		conn.close();
	}
}



你可能感兴趣的:(Eclipse加载MySQL驱动步骤)