Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package practice3;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.Calendar;
- import java.util.Date;
- public class JDBC_CRUD_Test {
- // 準備連線資料庫所需的字串
- private static String url = "jdbc:derby://localhost:1527/EmployeeDB";
- private static String user = "test";
- private static String pass = "tiger";
- private static Employee getNewEmployee() {
- // 日期格式化
- Calendar calendar = Calendar.getInstance();
- calendar.set(1980, 4, 1); // 設定日期 1980-4-1
- Date birthDate = calendar.getTime(); // 產生 Date 物件
- // 返回員工物件
- return new Employee(901, "小明", "王", birthDate, 50_000);
- }
- public static void insert() {
- String query = "insert into employee values(?,?,?,?,?)"; // 使用問號代表參數,之後設定
- Employee e = getNewEmployee();
- int count = 0; // 有幾筆資料完成新增
- try (
- Connection con = DriverManager.getConnection(url, user, pass);
- PreparedStatement pstmt = con.prepareStatement(query); // PreparedStatement
- ) {
- // 設定各項 ? 參數值
- pstmt.setInt(1, e.getId()); // 設定第1個 ? 參數
- pstmt.setString(2, e.getFirstName()); // 設定第2個 ? 參數
- pstmt.setString(3, e.getLastName()); // 設定第3個 ? 參數
- // 資料庫所使用的日期是 java.sql.Date , 所以必須先將 java.util.Date 轉成 java.sql.Date
- Date date = e.getBirthDate();
- long time = date.getTime();
- java.sql.Date sqlDate = new java.sql.Date(time); // 產生 java.sql.Date 物件
- pstmt.setDate(4, sqlDate); // 設定第4個 ? 參數
- pstmt.setFloat(5, e.getSalary()); // 設定第5個 ? 參數
- // executeUpdate() // 回傳有幾筆資料完成更新
- count = pstmt.executeUpdate();
- } catch (SQLException ex) {
- System.err.println(ex);
- }
- if (count > 0) {
- System.out.println(e.getFirstName() + " 新增 成功");
- } else {
- System.out.println(e.getFirstName() + " 新增 失敗");
- }
- }
- public static void update() {
- String query = "update employee set firstname=? , salary=? where id=?"; // 使用問號代表參數,之後設定
- // 取得 Employee 物件
- Employee e = getNewEmployee();
- // 修改 firstname 與 salary
- e.setFirstName("大明");
- e.setSalary(100_000);
- int count = 0; // 有幾筆資料完成修改
- try (
- Connection con = DriverManager.getConnection(url, user, pass);
- PreparedStatement pstmt = con.prepareStatement(query); // PreparedStatement
- ) {
- // 設定各項 ? 參數值
- pstmt.setString(1, e.getFirstName()); // 設定第1個 ? 參數
- pstmt.setFloat(2, e.getSalary()); // 設定第2個 ? 參數
- pstmt.setInt(3, e.getId()); // 設定第3個 ? 參數
- // executeUpdate() // 回傳有幾筆資料完成更新
- count = pstmt.executeUpdate();
- } catch (SQLException ex) {
- System.err.println(ex);
- }
- if (count > 0) {
- System.out.println("修改 成功");
- System.out.println(e);
- } else {
- System.out.println("修改 失敗");
- }
- }
- public static void delete() {
- String query = "delete from employee where id=?"; // 使用問號代表參數,之後設定
- // 取得 Employee 物件
- Employee e = getNewEmployee();
- int count = 0; // 有幾筆資料完成修改
- try (
- Connection con = DriverManager.getConnection(url, user, pass);
- PreparedStatement pstmt = con.prepareStatement(query); // PreparedStatement
- ) {
- // 設定 ? 參數值
- pstmt.setInt(1, e.getId()); // 設定第1個 ? 參數
- // executeUpdate() // 回傳有幾筆資料完成更新
- count = pstmt.executeUpdate();
- } catch (SQLException ex) {
- System.err.println(ex);
- }
- System.out.println(e);
- if (count > 0) {
- System.out.println("刪除 成功");
- } else {
- System.out.println("刪除 失敗");
- }
- }
- public static void main(String[] args) {
- //insert();
- //update();
- delete();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement