我是eclipse中使用DB的新手,我遇到了一些问题。我能够创建一个数据库连接(因此创建一个表),但是当我尝试查询所述表时,我得到以下内容:线程“main”java.sql.SQLException中的异常:没有为jdbc找到合适的驱动程序:德比:
我正在使用我首先使用的JDBC_URL的静态调用,所以我很困惑为什么会发生这种情况。下面显示了两个主要类。第一个工作正常(表的创建),但第二个抛出上述错误。注意我的项目的lib文件夹中有derby.jar。
A类)工作正常
public class CreateDB {
public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
public static final String JDBC_URL = "jdbc:derby:zadb;create=true";
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
Class.forName(DRIVER);
Connection connection = DriverManager.getConnection(JDBC_URL);
connection
.createStatement()
.execute(
"create table channels(channel varchar(20), topic varchar(20), videoclip varchar(20))");
connection.createStatement().execute(
"insert into channels values "
+ "('oodp', 'creational', 'singleton'), "
+ "('oodp', 'creational', 'factory method'), "
+ "('oodp', 'creational', 'abstract factory')");
System.out
.println("Table was created and the records were successfully inserted...");
}
}
B类)(抛出SQL异常)
public class QueryDB {
public static final String SQL_STATEMENT = "Select * from Channels";
public static void main(String[] args) throws SQLException {
Connection connection = DriverManager.getConnection(CreateDB.JDBC_URL);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(SQL_STATEMENT);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int columnCount = resultSetMetaData.getColumnCount();
for(int x = 1; x <= columnCount; x++){
System.out.print(resultSetMetaData.getColumnName(x) + "|");
}
while(resultSet.next()){
System.out.println();
for(int x = 1; x <= columnCount; x++){ System.out.print(resultSet.getString(x) + "|");}
}
if(statement!=null) statement.close();
if(connection!=null) connection.close();
}
}