滿滿一週的課終於結束,獎勵自己一杯奶茶吧
學習內容:安卓
package com.example.app_02.utils;
import com.example.app_02.R;
import com.example.app_02.database.MySQLConnection;
import com.example.app_02.entity.Record;
import com.example.app_02.entity.User;
import java.util.ArrayList;
public class RecordDao extends MySQLConnection {
public int insertRecord(Record record) {
int value = 0;
connect();
try {
String sql = "insert into record(starttime, endtime, record) values(?,?,?)";
preparedStatement = connection.prepareStatement(sql);
//將資料插入資料庫中
preparedStatement.setString(1, record.getStartTime());
preparedStatement.setString(2, record.getEndTime());
preparedStatement.setString(3, record.getRecord());
value = preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
}
return value;
}
public ArrayList<Record> findRecord() {
connect();
ArrayList<Record> arrayList = new ArrayList<>();
try {
//sql語句。我這裡是根據我自己的users表的username欄位來查詢記錄
String sql = "select * from record where 1=1";
//獲取用於向資料庫傳送sql語句的preparedStatement
preparedStatement = connection.prepareStatement(sql);
//根據賬號進行查詢
//執行sql查詢語句並返回結果集
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
//.next()表示指標先下一行,若有資料則返回true
Record record = new Record(resultSet.getString("starttime"), resultSet.getString("endtime"), resultSet.getString("record"));
arrayList.add(record);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
}
return arrayList;//若傳入成功返回賬號,失敗則為null
}
}