2013年9月9日 星期一

java-mysql-table2array


java mysql table to array



static double[][] dRaws=null;

static void getSQLData(){
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection
("jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF8","userID", "Password");
        stmt = con.createStatement();
        rs = stmt.executeQuery("select Tcode,UNIX_TIMESTAMP(date)*1000,field1,
field2, field3, field4,
field5,field6 from TableName where Tcode="+code+" ;");
        while (rs.next()){String id=rs.getString(1);}rs.last(); //go bottom
int rowCount=rs.getRow(); //go recno
        dRaws = new double[rowCount][];
        rs.beforeFirst(); //go top
        int iRecno=0;
        while (rs.next()){
            dRaws[iRecno] = new double [6+10];  //append blank
            for (int j=2; j<=7; j++){
             try{dRaws[iRecno][j-2]=(double) rs.getDouble(j);} //replace field with value
catch (NullPointerException e){};
            }
            ++iRecno;
        }
        con.close();
    } catch (Exception e) {e.printStackTrace();}
}

沒有留言:

張貼留言