Search code examples
javasqldatabasedatabase-connectionderby

Creating a JAVA database from code. Statement wont execute and create table?


I'm not sure why my program isn't creating the table but I also need some ideas on how to fill the table with code like this once it's created? I need to add two more tables to this database too.

This is the error I'm getting :

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

This is the code that's supposed to create the table:

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

Solution

  • Depending on the IDE you are using you could manually create the table in a console without going through the trouble of writing it in code. Here are some examples of how you could get the information from the tables.

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

    Here is what the Connection class should look like: 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;
            }
        }
    }
    

    Approve this answer if this helped you, I'd be happy to explain things if it does not make sense. :)