2016-11-30 53 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

好吧,它看起来像现在正在工作:)谢谢! –