Thinking in Enterprise Java中编译例程Lookup.java相关问题java.lang.ClassNotFoundException和 Access denied

Thinking in Enterprise Java中:

编译例程c15:jdbc:Lookup.java的时候出现,相关问题

java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver

显示jdbc访问ACCESS的方法已经过时,在新的版本中官方不再提供相应的方法。

遂改用jdbc访问mySQL进行替代。

即Class.forName("com.mysql.cj.jdbc.Driver")已经过时

故笔者在MySQL建库之后,使用

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();替代之。

 

由此处下载 JDBC Connector

https://dev.mysql.com/downloads/connector/j/5.1.html

安装指南位于

https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-installing.html

本次下载,下载后解压缩

得到两个jar文件:

mysql-connector-java-5.1.47.jar

mysql-connector-java-5.1.47-bin.jar

其实原本安装的mysql也有C:\Program Files (x86)\MySQL\Connector J 8.0\mysql-connector-java-8.0.11.jar

复制到

C:\Program Files\Java\jdk-10.0.1\lib\mysql-connector-java-5.1.47-bin.jar;

C:\Program Files\Java\jdk-10.0.1\lib\mysql-connector-java-5.1.47.jar;

本次操作将两个文件都拷到那个目录下,其实可以只放一个。

修改环境变量CLASSPATH(笔者为windows 平台) ,指向要包含到具体文件, 修改后,其值为:

.;C:\Program Files\Java\ClassPathDir;C:\Program Files\Java\jdk-10.0.1\lib\mysql-connector-java-5.1.47-bin.jar;C:\Program Files\Java\jdk-10.0.1\lib\mysql-connector-java-5.1.47.jar;

之后使用javac就可以编译了

在调试过程中,碰到

SQLException: Access denied for user 'dbuser'@'localhost' (using password: YES)

SQLState: 28000

VendorError: 1045

把user更换成root并使用密码,则可以查询了,可能是很久没有更新数据库dbuser的密码过期expired了。

修改之后的代码为:

//: c15:jdbc:Lookup.java
// Looks up email addresses in a
// local database using JDBC.
// {Broken}
import java.sql.*;
 
public class Lookup {
  public static void main(String[] args)
  throws SQLException, ClassNotFoundException {
    //ACCESS is nolonger support by official after newly JDK
    //modified by zyy to use MySql
    //String dbUrl = "jdbc:odbc:people";
    String dbUrl ="jdbc:mysql://localhost:3306/people?useUnicode=true&characterEncoding=GB2312";
    //String user = "";
    String user = "root";
    String password = "zo1_zyyinbh";
    // Load the driver (registers itself)
    try {
      // The newInstance() call is a work around for some
      // broken Java implementations
      Class.forName(
      "sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
      //"com.mysql.cj.jdbc.Driver").newInstance();
    } catch (Exception ex) {
      // handle the error
      System.err.println(ex);
    }
      //"com.mysql.jdbc.Driver");
      //DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      try{
    Connection c = DriverManager.getConnection(
      dbUrl, user, password);
       Statement s = c.createStatement();
    // SQL code:
    ResultSet r =
      s.executeQuery(
        "SELECT FIRST, LAST, EMAIL " +
        //"FROM people.csv people " +
        "FROM people " +
        "WHERE " +
        "(LAST='" + args[0] + "') " +
        " AND (EMAIL Is Not Null) " +
        "ORDER BY FIRST");
    while(r.next()) {
      // Capitalization doesn't matter:
      System.out.println(
        r.getString("Last") + ", "
        + r.getString("fIRST")
        + ": " + r.getString("EMAIL") );
    }
    s.close(); // Also closes ResultSet
    }catch (SQLException ex){
    // handle any errors
      System.out.println("SQLException: " + ex.getMessage());
      System.out.println("SQLState: " + ex.getSQLState());
      System.out.println("VendorError: " + ex.getErrorCode());
}
  }
} ///:~

 

展开阅读全文

没有更多推荐了,返回首页