java.sql.SQLException:找不到合适的驱动程序

时间:2013-07-06 21:47:29

标签: java jdbc sqlexception ocpjp

我正在尝试使用DbQuery.java类执行简单查询,该类使用DbConnector从DriverManager获取连接。

注意

  1. 我已经在我的网站上加入了“mysql-connector-java-5.1.25-bin.jar” classpath via:export CLASSPATH = $ CLASSPATH:/home/me/ocpjp/chapter-10/mysql-connector-java-5.1.25/mysql-connector-java-5.1.25-bin.jar
  2. 我可以使用“mysql -uroot -ptcial”连接到mysql 地址簿“,如果重要的话。
  3. 还尝试使用'-cp'运行 争论没有用。
  4. 我能够让我的#3 DbConnect.java类说“已建立数据库连接”。
  5. 另外#4 DbQueryWorking.java没有问题并提供预期的输出。
  6. 你能帮我理解这里的问题吗?

    1)DbConnector.java

    package com.me.ocpjp.chapter10;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    public class DbConnector{
    public static Connection connectToDb() throws SQLException{
    String url = "jdbc:mysql//localhost:3306/";
    String db = "addressBook";
    String username = "root";
    String password = "tcial";
    return DriverManager.getConnection(url+db, username, password);
    }
    
    }
    

    2)DbQuery.java

    package com.me.ocpjp.chapter10;
    
    import java.sql.Connection  ;
    import java.sql.Statement  ;
    import java.sql.ResultSet  ;
    import java.sql.SQLException  ;
    import com.me.ocpjp.chapter10.DbConnector;
    
    public class DbQuery{
    public static void main(String[] args){
    try(Connection connection = DbConnector.connectToDb();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from contact")){
    System.out.println("ID \tfName \tlName \temail \t\tphoneNo");
    while(resultSet.next()){
    System.out.println(resultSet.getInt("id") + "\t"
    + resultSet.getString("firstName") + "\t"
    + resultSet.getString("lastName") + "\t"
    + resultSet.getString("email") + "\t"
    + resultSet.getString("phoneNo") );
    }
    
    }catch(SQLException sqle){
    sqle.printStackTrace();
    System.exit(-1);
    }
    
    }
    }
    

    3)DbConnect.java

    package com.me.ocpjp.chapter10;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    
    
    
    public class DbConnect{
    public static void main(String[] args){
    
    String url = "jdbc:mysql://localhost:3306/";
    String database = "addressBook";
    String userName = "root";
    String password = "tcial";
    
    try(Connection connection = DriverManager.getConnection(url+database, userName, password)){
    System.out.println("Database connection established");
    }catch(Exception e){
    System.out.println("Database connectioni NOT  established");
    e.printStackTrace();
    }
    
    }
    
    }
    

    4)DbQueryWorking.java

    package com.me.ocpjp.chapter10;
    
    import java.sql.Connection  ;
    import java.sql.Statement  ;
    import java.sql.ResultSet  ;
    import java.sql.SQLException  ;
    import java.sql.DriverManager;
    
    public class DbQuery{
    public static void main(String[] args){
    String url = "jdbc:mysql://localhost:3306/";
    String database = "addressBook";
    String userName = "root";
    String password = "tcial";
    try(Connection connection = DriverManager.getConnection(url + database, userName, password);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from contact")){
    System.out.println("ID \tfName \tlName \temail \t\tphoneNo");
    while(resultSet.next()){
    System.out.println(resultSet.getInt("id") + "\t"
    + resultSet.getString("firstName") + "\t"
    + resultSet.getString("lastName") + "\t"
    + resultSet.getString("email") + "\t"
    + resultSet.getString("phoneNo") );
    }
    
    }catch(SQLException sqle){
    sqle.printStackTrace();
    System.exit(-1);
    }
    
    }
    }
    

2 个答案:

答案 0 :(得分:7)

看起来DbConnector.java中的网址错误。结肠丢失了。网址必须是:

jdbc:mysql://localhost:3306/

而不是

jdbc:mysql//localhost:3306/

答案 1 :(得分:4)

你的网址错了,你错过了一个冒号,它应该是:

String url = "jdbc:mysql://localhost:3306/";