我需要在表格中插入一些数据。当程序执行插入时,可能会出现错误,因为我还没有创建表。我想在 catch 块中创建该表并返回到 try 块。因此,每当表被删除或碰巧删除时,try catch 都会构建表。
我已经为它编写了一些代码,但它失败了。
import java.sql.*;
/**
 *
 * @author JOJO
 */
public class ConnectDB {
  static Connection conn;
   static String driver = "org.apache.derby.jdbc.ClientDriver";
   static String connectionURL = "jdbc:derby://localhost:1527/Libraryprj;user=scott;password=tiger";
  public void AddBookDB(String bookName) throws SQLException {
    String createString = "CREATE TABLE BOOKLEDGER (BOOKNAME VARCHAR(50) NOT NULL)";
    try {
      Class.forName(driver);
    } catch (java.lang.ClassNotFoundException e) {
      e.printStackTrace();
    }
    try
        {
            conn = DriverManager.getConnection(connectionURL);
            Statement stmt = conn.createStatement();
            boolean execute = stmt.execute("insert into BOOKLEDGER values ('" + bookName + "')");
            stmt.close();
        }
        catch (SQLException sqlExcept)
        {
            Statement stmt = (Statement) conn.createStatement();
            stmt.executeUpdate(createString);
            sqlExcept.printStackTrace();
        }
  }
}