2016-11-30 51 views
1

我不知道爲什麼我的程序不是在創建表格,但是我也需要關於如何在創建代碼時使用代碼填充表格的一些想法?我還需要再向這個數據庫添加兩個表。從代碼創建JAVA數據庫。陳述不會執行並創建表?

這是我得到的錯誤:

java.sql.SQLSyntaxErrorException: Table/View 'PIZZASIZE' does not exist. 
Caused by: ERROR 42X05: Table/View 'PIZZASIZE' does not exist. 
Caused by: java.lang.RuntimeException: Exception in Application start method 
Caused by: javafx.fxml.LoadException: file:/C:/Users/Allie/Documents/NetBeansProjects/Pizzeria_AllieBeckman/dist/run1674141987/Pizzeria_AllieBeckman.jar!/pizzeria_alliebeckman/FXMLDocument.fxml 

這是一個本應創建表的代碼:

 // connect to the derby URL using the given username and password 
     connect = DriverManager.getConnection("jdbc:derby://localhost:1527/pizzeria;create=true", connectProps); 
     // current url for pre created database "jdbc:derby://localhost:1527/pizza" 
     // if connection is successful print that it succeeded. 
     System.out.println("database created"); 

     stmt = connect.createStatement(); 

     String sqlCreate = "CREATE TABLE PIZZASIZE " 
      + "(id int NOT NULL, " 
      + "size char(20) NOT NULL, " 
      + "PRIMARY KEY (id))"; 

     stmt.execute(sqlCreate); 
+0

嘗試'stmt.executeUpdate(sqlCreate)與stmt.executeUpdate(sqlCreate)' –

+0

同樣的錯誤:( –

+0

'的System.out.println( 「數據庫中創建」);?'這是執行 –

回答

1

根據IDE你使用,你可以手動創建在控制檯中的表格,而不需要經過編寫代碼的麻煩。以下是一些如何從表格中獲取信息的示例。

Connection conn = CreatingDerbyDJB.dbConnection(); 

      try{ 
       String query = "INSERT INTO Items (Name,Color,ItemName,SchoolName, Description) VALUES(?,?,?,?, ?)"; 
       PreparedStatement pstmt = conn.prepareStatement(query); 


       pstmt.execute(); 
       conn.close(); 
       }catch(Exception e) 
      { 
        e.printStackTrace(); 
      }  } 

以下是Connection類的外觀: package main;

import java.sql.Connection; 
import java.sql.DriverManager; 

import javax.swing.JOptionPane; 

public class CreatingDerbyDJB 
{ 
    public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver"; 
    public static final String JDBC_URL = "jdbc:derby:LostAndFoundDB"; 
    public static Connection dbConnection() 
    { 
     try 
     { 
      Class.forName(DRIVER).newInstance(); 
      Connection c = DriverManager.getConnection(JDBC_URL); 
      return c; 
     }catch(Exception e) 
     { 
      JOptionPane.showMessageDialog(null, e); 
      return null; 
     } 
    } 
} 

贊同這個答案如果這對你有幫助,我會很高興解釋的東西,如果它沒有意義。 :)

+0

謝謝,這是很好的信息!準備好的聲明正在創建一個表,但我似乎無法填充它,我有點困惑於「setString(x,xxx)」方法,我只需要ID和「size」列,它只會有4個選項 –

+1

要填充表格,您必須使用插入查詢,就好像您使用的是常規數據庫編程語言一樣,您可以根據你的表的名字然後在parenthasis中列出這些列,然後你指定你想要插入的值。另外,對於來自代碼的setString我的程序我忘了刪除大聲笑 – Aaron

+0

好吧,它看起來像現在正在工作:)謝謝! –