0

I'm using Java and SQLite and I have the following situation:

I have a database connected and created a table say ubs

Now I have arrays int[] d and double[] o in Java, what would be the most efficient way to store it in the database? I tried piecewise insertion and read... the insertion works but it doesnt read out right.

Here is my code:

    int size = 20;
    int[] d = new int[size];
    double[] o = new double[size];
    double[] c = new double[size];

    int[] dtest = new int[size];
    double[] otest = new double[size];
    double[] ctest = new double[size];


    for(int a = 0; a< size; ++a) {
        d[a] = a+1;
        o[a] = Math.random();
        c[a] = Math.random();
    }

    Connection conn = DriverManager.getConnection(sDbUrl);


     Statement stat = conn.createStatement();
     stat.executeUpdate("CREATE  TABLE ubs (date INTEGER, open DOUBLE, close DOUBLE)");
     PreparedStatement prep = conn.prepareStatement("insert into ubs values (?,?,?);");
     for(int a = 0; a < size; ++a) {
         prep.setInt(1, d[a]);
         prep.setDouble(2,o[a]);
         prep.setDouble(3,c[a]);
         prep.addBatch();
     }

     conn.setAutoCommit(false);
     prep.executeBatch();
     conn.commit();
     conn.setAutoCommit(true);
     ResultSet rs = stat.executeQuery("select * from ubs;");
     int a = 0;
     while (rs.next())
     {
         dtest[a] = rs.getInt("date");
         otest[a] = rs.getDouble("open");
         ctest[a] = rs.getDouble("close");
         ++a;
     }
    rs.close();
    conn.close();


     //test wether contain the same:
    for(a = 0; a< size; ++a) {
         System.out.println("original: " + o[a] +" saved: "+otest[a]);

    }

here is what it returns:

    original: 0.707119601198528 saved: 0.22184821183493642
    original: 0.616040801231279 saved: 0.0
    original: 0.32427329912284675 saved: 0.0
    original: 0.570393637345201 saved: 0.0
    original: 0.05334583924906977 saved: 0.0
    original: 0.5920142191693711 saved: 0.0
    original: 0.5440058264918218 saved: 0.0
    original: 0.9792739762035961 saved: 0.0
    original: 0.7288365270388483 saved: 0.0
    original: 0.862852415217843 saved: 0.0
    original: 0.6427868320117643 saved: 0.0
    original: 0.4895153310585434 saved: 0.0
    original: 0.9163102294291119 saved: 0.0
    original: 0.7991642836297964 saved: 0.0
    original: 0.35640672534480244 saved: 0.0
    original: 0.047751063499014146 saved: 0.0
    original: 0.4956032547461785 saved: 0.0
    original: 0.8975964852843482 saved: 0.0
    original: 0.11097820888267451 saved: 0.0
    original: 0.22184821183493642 saved: 0.0

which doesnt make sense. SO what am I doing wrong? And how can I improve it? By the way this code might be completely senseless because I pieced it together from different tutorials so sorry for that.

4

1 回答 1

1

在您的以下循环中

int a = 0;
while (rs.next())
{
         dtest[a] = rs.getInt("date");
         otest[a] = rs.getDouble("open");
         ctest[a] = rs.getDouble("close");
}

最后添加a++

基本上,您的数组索引在之后不会增加rs.next()

于 2013-02-12T15:53:18.947 回答