0

我添加了 sqlite-jdbc-3.7.2.jar 来构建路径。我使用了插入查询。当我用记事本打开 wellec.db 时,我可以看到记录。但是我的选择不起作用。

HERE WellInfo 类属性

public Integer wellID;
public Integer measurement;
public String wellname;
public Integer wellstatus;
public String licenseno;
public String gl;
public String kb;
public String spuddate;
public String drillingenddate;
public String totaldepth;
public String notes;
public String easting;
public String northing;
public String coordinatesystem;
public Integer islogadd;

这是我的可创建 sql

          Class.forName("org.sqlite.JDBC");
          c = DriverManager.getConnection("jdbc:sqlite:wellec.db");
          c.setAutoCommit(false);
          System.out.println("Opened database successfully");

          stmt = c.createStatement();

          String sql = "CREATE TABLE IF NOT EXISTS WELLINFO " +
                    "(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ," +
                    " WELLNAME           CHAR(90)    NOT NULL, " +
                    " WELLSTATUS   INT NOT NULL," +
                    " MEASUREMENT INT NOT NULL," +
                    " ISLOGADD INT NOT NULL," +
                    " GL           CHAR(50)    NOT NULL, " +
                    " KB           CHAR(50)    NOT NULL, " +
                    " SPUDDATE           CHAR(50)    NOT NULL, " +
                    " TOTALDEPTH           CHAR(50)    NOT NULL, " +
                    " LICENSENO           CHAR(50)    NOT NULL, " +
                    " DRILLINGENDDATE           CHAR(50)    NOT NULL, " +
                    " NOTES           CHAR(150)    NOT NULL, " +
                    " EASTING           CHAR(50)    NOT NULL, " +
                    " NORTHING           CHAR(50)    NOT NULL, " +
                    " COORDINATESYSTEM CHAR(50)    NOT NULL)";
          stmt.executeUpdate(sql);
...//other tables sql and stmt.executeUpdate(sql)
 stmt.close();
 c.commit();

这是我的插入查询,它正在工作。WellInfo wi 作为参数

        Connection c = null;
        Statement stmt = null;
        try {
          Class.forName("org.sqlite.JDBC");
          c = DriverManager.getConnection("jdbc:sqlite:wellec.db");
          c.setAutoCommit(false);
          System.out.println("Opened database successfully");

          stmt = c.createStatement();
          String sql = "INSERT INTO WELLINFO " +
                    "(ID, WELLNAME, WELLSTATUS , MEASUREMENT, ISLOGADD , GL, KB, SPUDDATE, TOTALDEPTH, " +
                    "LICENSENO , DRILLINGENDDATE, NOTES, EASTING, NORTHING, COORDINATESYSTEM) " +
                    "VALUES (NULL,'" + wi.wellname + "'," + wi.wellstatus + "," + wi.measurement + "," +
                            "" + wi.islogadd +",'" + wi.gl + "','" + wi.kb + "','" + wi.spuddate + "'," +
                                    "'" + wi.totaldepth + "','" + wi.licenseno + "','" + wi.drillingenddate + "', " +
                                            "'" + wi.notes + "','" + wi.easting + "','" + wi.northing + "','" + wi.coordinatesystem + "');";
          stmt.executeUpdate(sql);

          stmt.close();
          c.commit();

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

        return 0;

这是我的选择

    Connection c = null;
    Statement stmt = null;
    WellInfo[] wi = new WellInfo[60000];
    WellInfo[] wi2 = new WellInfo[0];

    int i = 0;
    try {
      Class.forName("org.sqlite.JDBC");
      c = DriverManager.getConnection("jdbc:sqlite:wellec.db");
      //c.setAutoCommit(false);
      System.out.println("Opened database successfully");


      stmt = c.createStatement();
      ResultSet rs = stmt.executeQuery("select * from WELLINFO"); 
      //c.commit();

      //if(rs.getRow() > 0)
          while ( rs.next() ) { //HERE IS THE PROBLEM
                 wi[i].wellID = rs.getInt("ID");
                 i++;
          }

      wi2 = new WellInfo[i];
      for(int j = 0; j < i - 1; j++){
          wi2[j] = wi[j];
      }

      rs.close();
      stmt.close();

      return wi2;
    } catch ( Exception e ) {
      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
      System.exit(0);
    }

    System.out.println("Operation done successfully");
    return wi2;

抱歉没有具体说明问题。这里是:

当我调用 rs.next() 时出现 SELECT QUERY 问题,我收到错误 java.lang.NullPointerException。

4

1 回答 1

1

wi[i]is null=> 你不能wi[i].wellID在实例化之前调用wi[i],你应该做类似的事情

wi[i] = new WellInfo()然后打电话wi[i].wellID

于 2013-09-13T07:17:12.263 回答