我想創建一個表,插入到表中並使用Derby打印表格的內容(如下所示)。在eclipse項目中實現嵌入式Derby數據庫的問題?
TestProject類:
package com.user.DerbyTest;
public class TestProject {
public static void main(String[] args) {
DBConnection db = new DBConnection();
db.createTable();
db.insertIntoTable("todd", 23, 'M');
db.insertIntoTable("wayne", 54, 'M');
db.printAll();
}
}
DBConnection的類:
package com.user.DerbyTest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBConnection {
private static final String DRIVER = "org.apache.derby.jdbc.*";
private static final String JDBC_URL = "jdbc:derby:derbytest;create=true";
Connection conn;
public DBConnection(){
try {
this.conn = DriverManager.getConnection(JDBC_URL);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (this.conn != null){
System.out.println("Connected to database.");
}
}
public void createTable(){
try {
conn.createStatement().execute("Create TABLE MyDerbytable(Name varchar(50), Age INT, Gender char(1))");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insertIntoTable(String name, int age, char gender){
try {
conn.createStatement().execute("INSERT INTO MyDerbytable Values ("+name+","+age+","+gender+")");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void printAll(){
try {
Statement statement = this.conn.createStatement();
ResultSet res = statement.executeQuery("Select * FROM MyDerbytable");
while(res.next()){
System.out.println(res.getString("Name") + res.getString("Age") + res.getString("Gender"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
試圖運行這個時,我收到錯誤的整體plethra:
有什麼建議嗎?
編輯:更改爲DROP錯誤:
謝謝!但仍然出現錯誤..請參閱我的編輯 – vkapustin
顯示完整的錯誤消息。 – Tassu
@vkapustin你可以顯示你的drop table語句嗎? – Tassu