java.sql.SQLException:没有为jdbc找到合适的驱动程序:derby // localhost:1527 / database

时间:2017-11-21 04:21:13

标签: java database jdbc derby

使用的库

java db driver - derby.jar

java db driver - derbyclient.jar

java db driver - derbynet.jar

jdk 1.8(default)

SQL语句

SELECT * FROM NAME.TABLE;

代码:

package database;    
import java.sql.Connection;    
import java.sql.Date;    
import java.sql.DriverManager;    
import java.sql.ResultSet;    
import java.sql.SQLException;    
import java.sql.Statement;

public class DataBase {

    public static void main(String[] args) {    
        //TODO code application logic here    
        Connection myconObj = null;    
        Statement mystatObj = null;    
        ResultSet myresObj = null;    
        String query = "Select * from name.table";    
        try {    
            myconObj = DriverManager.getConnection("jdbc:derby//localhost:1527/database", "username", "paasword");    
            mystatObj = myconObj.createStatement();    
            myresObj = mystatObj.executeQuery(query);  

            while (myresObj.next()) {    
                int id = myresObj.getInt("ID");    
                String name = myresObj.getString("Name");    
                Date date = myresObj.getDate("DateOfBirth");    
                String phone=myresObj.getString("Phone");    
                System.out.println(id +  "\t   "+ name   +"\t   " + date+ "\t   "+ phone);    
            }
        }
        catch (SQLException e) {    
            e.printStackTrace();    
        }    
    }    
}

1 个答案:

答案 0 :(得分:0)

未加载JDBC驱动程序或JDBC URL格式错误。由于连接URL看起来很好,问题应该是加载驱动程序。对于Derby数据库,驱动程序类名称为org.apache.derby.jdbc.ClientDriver。 在连接到DB之前使用以下行。

Class.forName("org.apache.derby.jdbc.ClientDriver");