[英]Creating a database via JDBC API
首先,我是編程方面的新手。我要在幾天后完成一項作業,在那里我應該創建動物數據庫來演示JDBC API的用法。 這是作業的確切要求:
編寫一個Java程序(非GUI首選)來演示JDBC的用法。
該程序應允許用戶執行以下操作:
•使用JDBC將動物及其特征列表寫入數據庫
•選擇動物時,顯示其特征。
現在,我一直在翻閱我的教科書,閱讀數據庫中的所有部分,並嘗試按照列出的示例來完成此作業,因此我陷入了困境。
我不斷收到no suitable driver found error for the database URL
。
我遵循了這些示例,在反映任務所需的領域中對其進行了更改,但仍然無法正常工作。
我輸入的示例與編寫時完全相同,因此無法編譯。 我一直在整個Internet上尋找解決方法,老實說,我不明白。
如果有不同,我正在使用NetBeans IDE 8.0.2。
這是我到目前為止的代碼。 我知道我還沒有顯示方法,也沒有數據庫表中的信息,但是如果我什至無法獲得實現數據庫的功能,那是不重要的。
我還在第15行的IDE中收到“未封閉的字符串文字”消息,正如我的教科書所示,該行寫為ESCATLY。
package animalsweek4;
import java.sql.*;
public class AnimalsWeek4 {
public static void main(String[] args) throws SQLException{
try{
final String DB_URL = "jdbc:derby:Animals; create = true";
Connection conn = DriverManager.getConnection(DB_URL);
Statement s = conn.createStatement();
s.execute(CREATE TABLE Animals ( "+
"Name Char(10)"+
"Type Char(10)"+
"Diet Char (10)"+
"Habitat Char 10)");
s.close();
conn.close();
}
catch(Exception ex){
System.out.println("Error"+ ex.getMessage());
}
}
}
您收到找不到驅動程序的錯誤,因為您可能沒有將derby驅動程序添加到程序庫中。 在程序中,右鍵單擊庫文件夾,然后選擇添加jar /文件夾並添加derby jar文件(本地系統中應該隨身帶有derby jar文件)。 或從互聯網下載或向您的朋友詢問。
進入代碼,您的SQL是錯誤的。 因此,我對您的代碼做了一些修改,並使其在我的系統中正常工作。 添加驅動程序后,您可以運行代碼。
import java.sql.*;
public class AnimalsWeek4 {
public static void main(String[] args) throws SQLException{
Connection conn=null;
PreparedStatement preparedStatement = null;
String createTableSQL="CREATE TABLE Animals (Name VARCHAR(10),Type VARCHAR(10),Diet VARCHAR(10),Habitat VARCHAR(10))";
try{
final String DB_URL = "jdbc:derby:Animals;create = true";
//final String DB_URL = "jdbc:derby://localhost:1527/sample";
conn = DriverManager.getConnection(DB_URL);
Statement s = conn.createStatement();
s.executeUpdate("DROP TABLE IF EXISTS Animals");
s.close();
preparedStatement = conn.prepareStatement(createTableSQL);
preparedStatement.executeUpdate();
System.out.println("Table is created!");
}
catch(Exception ex){
System.out.println("Error"+ ex.getMessage());
}finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (conn != null) {
conn.close();
}
}
}
}
請查看修改后的代碼。
import java.sql.*;
public class AnimalsWeek4 {
public static void main(String[] args) throws SQLException{
Connection conn=null;
PreparedStatement preparedStatement = null;
Statement s =null;
String createTableSQL="CREATE TABLE Animals ( "
+"Name VARCHAR(10),"
+"Type VARCHAR(10),"
+"Diet VARCHAR(10),"
+"Habitat VARCHAR(10))";
try{
//final String DB_URL = "jdbc:derby:Animals; create = true";
final String DB_URL = "jdbc:derby://localhost:1527/TestDB";
conn = DriverManager.getConnection(DB_URL);
System.out.println("Connected to DB successfully");
final String dropTableSQL="DROP TABLE Animals";
s = conn.createStatement();
try{
s.executeUpdate(dropTableSQL);
System.out.println("Table Dropped");
}
catch(Exception e){
System.out.println("Exception is "+e);
// if (!e.getSQLState().equals("proper SQL-state for table does not exist"));
}
preparedStatement = conn.prepareStatement(createTableSQL);
preparedStatement.executeUpdate();
System.out.println("Table is created!");
}
catch(Exception ex){
System.out.println("Error"+ ex.getMessage());
}finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (conn != null) {
conn.close();
}
if (s!=null){
s.close();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.