Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从代码创建JAVA数据库。语句不会执行并创建表?_Java_Sql_Database_Database Connection_Derby - Fatal编程技术网

从代码创建JAVA数据库。语句不会执行并创建表?

从代码创建JAVA数据库。语句不会执行并创建表?,java,sql,database,database-connection,derby,Java,Sql,Database,Database Connection,Derby,我不知道为什么我的程序没有创建表,但我还需要一些关于如何在创建表后用这样的代码填充表的想法?我还需要向这个数据库添加两个表 这就是我得到的错误: java.sql.SQLSyntaxErrorException: Table/View 'PIZZASIZE' does not exist. Caused by: ERROR 42X05: Table/View 'PIZZASIZE' does not exist. Caused by: java.lang.RuntimeException: Ex

我不知道为什么我的程序没有创建表,但我还需要一些关于如何在创建表后用这样的代码填充表的想法?我还需要向这个数据库添加两个表

这就是我得到的错误:

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); 

根据您使用的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类的外观: 主包装

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;
        }
    }
}

批准这个答案如果这对你有帮助,如果没有意义,我很乐意解释

请尝试
stmt.executeUpdate(sqlCreate)
与stmt.executeUpdate(sqlCreate)相同的错误:(
System.out.println(“数据库已创建”);
这是执行的吗?是的,确实是打印谢谢,这是很好的信息!准备好的语句正在创建一个表,但我似乎无法填充它,我对“setString”有点困惑(x,xxx)“方法。我只需要ID和“大小”列,并且表中只有4个选项。若要填充表,必须像使用常规数据库编程语言一样使用insert into查询。您可以根据表的名称插入查询,然后在parenthasis中列出列。然后指定要插入的值。另外,很抱歉关于我程序中的setString代码,我忘了删除lol