Android

2013年4月10日 星期三

ArrayList 和ResultSet

Connection con = null; // Database objects
// 連接object
Statement stat = null;
// 執行,傳入之sql為完整字串
ResultSet result = null;

// 結果集

String selectSQL = "select * from hash_value ";
// 查詢資料
// 可以看看回傳結果集及取得資料方式
Class.forName("com.mysql.jdbc.Driver");
// 註冊driver
con = DriverManager.getConnection("jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=Big5","帳號", "密碼");
// 取得connection

// jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=Big5
// localhost是主機名,test是database名
// useUnicode=true&characterEncoding=Big5使用的編碼
stat = con.createStatement();
result = stat.executeQuery(selectSQL);
try{

ArrayList<String> alResult = new ArrayList<String>();
while (result.next()) {
  System.out.println("ID\t\tValue\t");
  System.out.println(result.getInt("ID") + "\t\t"+ result.getString("Value"));

  alResult.add(result.getString("Value"));
for (int i = 0; i < alResult.size(); i++) {
tbs = alResult.get(i).getBytes();
}
}catch (Exception ex) {
ex.printStackTrace();
}

沒有留言:

張貼留言