JDBC准备语句从表中选择

时间:2014-11-01 02:58:53

标签: java database sqlite jdbc

我正在尝试编写一个JDBC SQLite preparedstatement,它将返回customerID与传入的行匹配的行。我的方法返回一个空指针异常。

我对JDBC,SQLite或预备语句没有很好的理解,但从我所看到的,我拥有我需要的一切,并且无法解决为什么它不起作用。我的代码如下:

public static void CustomersSelectWhere(JPanel customers, int CustID)
      {              
         CustomersTable();
          String [] entries = new String[7]; 
        Connection c = null;
        PreparedStatement pstmt = null;
        try {
          Class.forName("org.sqlite.JDBC");
          c = DriverManager.getConnection("jdbc:sqlite:WalkerTechCars.db");
          c.setAutoCommit(false);

          String query = "SELECT * FROM CUSTOMERS WHERE CUSTID=? " ;

          ResultSet rs = pstmt.executeQuery( query );

          pstmt = c.prepareStatement(query);
          pstmt.setInt(1, CustID);
          pstmt.executeUpdate();
          c.commit();



          while ( rs.next() ) {
             int custId = rs.getInt("custID");
             String phone = rs.getString("phone");
             String  surname = rs.getString("surname");
             String  firstname = rs.getString("firstname");
             String home  = rs.getString("home");
             String  address = rs.getString("address");
             String  postcode = rs.getString("postcode");

             customers.add(customersTableSingle(Integer.toString(custId), firstname, surname, phone, home, address, postcode, false, customers ));


          }
          rs.close();
          pstmt.close();
          c.close();
        } catch ( Exception e ) {
          System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          System.exit(0);
        }

      }

}

4 个答案:

答案 0 :(得分:1)

您已定义

PreparedStatement pstmt = null;

准备好的语句应该是从connection连接创建的。因为你的预准备语句为null并且你试图调用ResultSet rs = pstmt.executeQuery(query);这是NPE的原因。

答案 1 :(得分:1)

您有两种不同类型的混合预备语句 - 查询和更新。您在分配之前查询pstmt,这会导致NPE。您还需要删除与提交相关的所有内容:

try {
    Class.forName("org.sqlite.JDBC");
    c = DriverManager.getConnection("jdbc:sqlite:WalkerTechCars.db");
    c.setAutoCommit(false);
    String query = "SELECT * FROM CUSTOMERS WHERE CUSTID=? " ;
    pstmt = c.prepareStatement(query);
    pstmt.setInt(1, CustID);
    ResultSet rs = pstmt.executeQuery( query );
    while ( rs.next() ) {
        String phone = rs.getString("phone");
        String surname = rs.getString("surname");
        String firstname = rs.getString("firstname");
        String home  = rs.getString("home");
        String address = rs.getString("address");
        String postcode = rs.getString("postcode");
        customers.add(customersTableSingle(Integer.toString(CustId), firstname, surname, phone, home, address, postcode, false, customers ));
    }
    rs.close();
    pstmt.close();
    c.close();
} catch ( Exception e ) {
    System.err.println( e.getClass().getName() + ": " + e.getMessage() );
    System.exit(0);
}

请注意,您无需再查询CustId - 它将与您提供的ID相同。在生产代码中传递*,然后按索引检索列也不是一个好主意:这太容易出错。您应该明确列出所有列的名称。

答案 2 :(得分:0)

你的语法稍微偏离,首先preapreStatement然后绑定参数,然后像{/ 1>那样调用executeQuery()

String query = "SELECT * FROM CUSTOMERS WHERE CUSTID=?";
// ResultSet rs = pstmt.executeQuery( query );
pstmt = c.prepareStatement(query);
pstmt.setInt(1, CustID);
rs = pstmt.executeQuery();
// c.commit();

此外,您应该close() finally中的所有内容。所以,像

public static void customersSelectWhere(JPanel customers, int CustID) {
    CustomersTable();
    String[] entries = new String[7];
    Connection c = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        // You should not need Class.forName anymore.
        // Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection("jdbc:sqlite:WalkerTechCars.db");
        c.setAutoCommit(false);

        String query = "SELECT * FROM CUSTOMERS WHERE CUSTID=?";
        // ResultSet rs = pstmt.executeQuery( query );
        pstmt = c.prepareStatement(query);
        pstmt.setInt(1, CustID);
        rs = pstmt.executeQuery();
        // c.commit();
        while (rs.next()) {
            // Make it a String to begin with.
            // String custId = rs.getString("custID");
            String custId = Integer.toString(CustID);
            String phone = rs.getString("phone");
            String surname = rs.getString("surname");
            String firstname = rs.getString("firstname");
            String home = rs.getString("home");
            String address = rs.getString("address");
            String postcode = rs.getString("postcode");
            customers.add(customersTableSingle(custId, firstname, surname,
                    phone, home, address, postcode, false, customers));
        }
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (c != null) {
            try {
                c.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

答案 3 :(得分:0)

您的代码缺少pstmt预处理语句对象的赋值,它被初始化为null并保持原样。

请添加

PreparedStatment pstmt = c.preparedStatement(query); 然后在结果集对象中获取它 RS = pstmt.executeQuery(); 然后迭代结果集就行了。