【问题标题】:Java SQLite database, insert entry giving nullPointerExceptionJava SQLite 数据库,插入条目给出 nullPointerException
【发布时间】:2014-09-15 12:08:50
【问题描述】:

我正在尝试使用 Eclipse 和 Java 创建一个可以将条目添加到 SQLite 数据库中的通用方法。

当表名被硬编码时,它工作正常,但是当我尝试将表名作为字符串传递时,它给了我一个 nullPointerException。

下面是创建该表的方法:

public static void Table()
      {
        Connection c = null;
        Statement stmt = null;
        try {
          Class.forName("org.sqlite.JDBC");
          c = DriverManager.getConnection("jdbc:sqlite:test.db");
          System.out.println("Opened database successfully");

          stmt = c.createStatement();
          String sql = "CREATE TABLE IF NOT EXISTS COMPANY " +
                       "(ID INT PRIMARY KEY     NOT NULL," +
                       " NAME           TEXT    NOT NULL, " + 
                       " AGE            INT     NOT NULL, " + 
                       " ADDRESS        TEXT, " + 
                       " SALARY         REAL)"; 
          stmt.executeUpdate(sql);
          stmt.close();
          c.close();
        } catch ( Exception e ) {
          System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          System.exit(0);
        }
        System.out.println("Table created successfully");
      }

这是在创建的表中插入条目的方法。我想通过方法传入表名而不是硬编码:

 public static void Insert(String table, int id, String name, int age, String address, String salary)
      {
        Connection c = null;
        PreparedStatement pstmt = null;
        try {
          Class.forName("org.sqlite.JDBC");
          c = DriverManager.getConnection("jdbc:sqlite:test.db");
          c.setAutoCommit(false);
          System.out.println("Opened database successfully");

          String query="INSERT INTO "+table+" (ID,NAME,AGE,ADDRESS,SALARY) VALUES (?,?,?,?,?)";

          PreparedStatement stmt = c.prepareStatement(query);

            pstmt.setInt(1,id);
            pstmt.setString(2,name);
            pstmt.setInt(3, age);
            pstmt.setString(4, address);
            pstmt.setString(5, salary);

            pstmt.executeUpdate();

            pstmt.close();
          c.commit();
          c.close();
        } catch ( Exception e ) {
          System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          System.exit(0);
        }
        System.out.println("Records created successfully");
      }

【问题讨论】:

  • 尝试打印“查询”。并检查“表”的值是否到来。
  • 这是个坏主意。表名和列名不受 PreparedStatement 约束。这些列在您的 SQL 中是固定的;对表名也进行硬编码。您没有正确关闭资源。您的错误处理也不正确。你没有遵循 Java 编码标准。
  • @duffymo 错误处理有什么不正确的地方?
  • 打印完整的堆栈跟踪总是比单个消息更多的信息。硬连线写入 System.err 会阻止记录到 log4j。系统退出? INSERT 失败不一定是致命的。您不会在 catch 块中回滚事务 - 为什么要为自动提交错误而烦恼?您应该在单个 try/catch 的 finally 块中关闭您的资源。您不应该在该方法中连接。传递连接。

标签: java eclipse sqlite jdbc methods


【解决方案1】:

你有一个小错误 只是你创建了两个不同的 PreparedStatement 对象 所以改变

PreparedStatement stmt = c.prepareStatement(query);

pstmt = c.prepareStatement(query);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多