如果您写好JDBC连接MySQL的Java文件,之后敲java命令运行代码,报异常:
- Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
原因是找不到驱动,ok?! 这篇文章适合您。
1. 下载MySQL的驱动
mysql-connector-java-5.1.16.tar.gz
到官方网站下载吧!
2. 解压
- tar -zxvf mysql-connector-java-5.1.16.tar.gz
解压之后文件夹中存在文件mysql-connector-java-5.1.16-bin.jar
名字太长,可以修改文件名:
- mv mysql-connector-java-5.1.16-bin.jar mysql-connector.jar
3. jdk与该驱动
如果你不使用eclipse的话,使用一般Java编辑器可以copy该mysql-connector.jar文件到:
- /home/mark/android/Java/jdk1.6.0_24/jre/lib/ext
就是你的jdk安装目录下面的/Java/jdk1.6.0_24/jre/lib/ext
4. Eclipse与该驱动
如果使用eclips的话,在新建工程中右键选择Build Path / Add External Archives选择mysql-connector.jar文件即可。
5. 测试源代码
- package net.mark;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class ShowInfo {
- private static final String SQLEXCEPTION =
- "jdbc:mysql://127.0.0.1:3306/mydb?mytable=root&password=";
- private static final String SQL = "jdbc:mysql://127.0.0.1/mydb?";
- private static final String USER = "root";
- private static final String PASSWORD = "";
- public static void main(String[] args) throws Exception {
- System.out.println("userId info:");
- getInfoFromDatabase(SQL, USER, PASSWORD, 1);
- System.out.println("==================================");
- System.out.println("userName info:");
- getInfoFromDatabase(SQL, USER, PASSWORD, 2);
- // 报异常信息:Access denied for user ''@'localhost' to database 'mydb'
- getInfoFromDatabase(SQLEXCEPTION, 1);
- }
- /**
- * 获取数据库中表的数据信息
- *
- * @param sql
- * sql语句
- * @param user
- * 用户名
- * @param password
- * 用户密码
- * @param columnIndex
- * 行、列索引.1 代表获得userId,2 代表获得userName
- */
- public static void getInfoFromDatabase(String sql, String user, String password, int columnIndex) {
- Connection conn = null;
- Statement statement = null;
- ResultSet result = null;
- try {
- //Class.forName("com.mysql.jdbc.Driver").newInstance();
- Class.forName("com.mysql.jdbc.Driver");//加载类Driver
- conn = DriverManager.getConnection(sql, user, password);
- statement = conn.createStatement();
- result = statement.executeQuery("select * from mytable");
- while (result.next()) {
- System.out.println("userName= " + result.getString(columnIndex));
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (result != null) {
- result.close();
- result = null;
- }
- if (statement != null) {
- statement.close();
- statement = null;
- }
- if (conn != null) {
- conn.close();
- conn = null;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 获取数据库中表的数据信息,重载方法
- *
- * @param sql
- * sql语句
- * @param columnIndex
- * 行、列索引.1 代表获得userId,2 代表获得userName
- */
- public static void getInfoFromDatabase(String sql, int columnIndex) {
- Connection conn = null;
- Statement statement = null;
- ResultSet result = null;
- try {
- //Class.forName("com.mysql.jdbc.Driver").newInstance();
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(sql);
- statement = conn.createStatement();
- result = statement.executeQuery("select * from mytable");
- while (result.next()) {
- System.out.println("userName= " + result.getString(columnIndex));
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (result != null) {
- result.close();
- result = null;
- }
- if (statement != null) {
- statement.close();
- statement = null;
- }
- if (conn != null) {
- conn.close();
- conn = null;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
6. 使用Eclipse看数据库
<1> 显示视图
window -- show view -- other
选择 Data Source Explorer和SQL Results
<2>在Data Source Explorer下面的Database Connections右键选择New...
<3> 选择MySQL数据库,Name任意取
<4> Next,填写数据库名称、用户名以及密码
这里数据库名称为mydb,用户root,密码为空。如果你的密码不为空的话,必须写!
<5> 上面图中,选择下载的MySQL的JDBC驱动。连接成功之后,显示如下:
<6> 显示表中数据。在表mytable右键Data选择Sample Contents
在SQL Results视图可以看到上述内容。
- Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user ''@'localhost' to database 'mydb'