Advertisement
Guest User

Untitled

a guest
Aug 1st, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. package practice3;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7. import java.util.Calendar;
  8. import java.util.Date;
  9.  
  10. public class JDBC_CRUD_Test {
  11.  
  12. // 準備連線資料庫所需的字串
  13. private static String url = "jdbc:derby://localhost:1527/EmployeeDB";
  14. private static String user = "test";
  15. private static String pass = "tiger";
  16.  
  17. private static Employee getNewEmployee() {
  18. // 日期格式化
  19. Calendar calendar = Calendar.getInstance();
  20. calendar.set(1980, 4, 1); // 設定日期 1980-4-1
  21. Date birthDate = calendar.getTime(); // 產生 Date 物件
  22. // 返回員工物件
  23. return new Employee(901, "小明", "王", birthDate, 50_000);
  24. }
  25.  
  26. public static void insert() {
  27. String query = "insert into employee values(?,?,?,?,?)"; // 使用問號代表參數,之後設定
  28.  
  29. Employee e = getNewEmployee();
  30. int count = 0; // 有幾筆資料完成新增
  31. try (
  32. Connection con = DriverManager.getConnection(url, user, pass);
  33. PreparedStatement pstmt = con.prepareStatement(query); // PreparedStatement
  34. ) {
  35. // 設定各項 ? 參數值
  36. pstmt.setInt(1, e.getId()); // 設定第1個 ? 參數
  37. pstmt.setString(2, e.getFirstName()); // 設定第2個 ? 參數
  38. pstmt.setString(3, e.getLastName()); // 設定第3個 ? 參數
  39.  
  40. // 資料庫所使用的日期是 java.sql.Date , 所以必須先將 java.util.Date 轉成 java.sql.Date
  41. Date date = e.getBirthDate();
  42. long time = date.getTime();
  43. java.sql.Date sqlDate = new java.sql.Date(time); // 產生 java.sql.Date 物件
  44.  
  45. pstmt.setDate(4, sqlDate); // 設定第4個 ? 參數
  46. pstmt.setFloat(5, e.getSalary()); // 設定第5個 ? 參數
  47.  
  48. // executeUpdate() // 回傳有幾筆資料完成更新
  49. count = pstmt.executeUpdate();
  50.  
  51. } catch (SQLException ex) {
  52. System.err.println(ex);
  53. }
  54. if (count > 0) {
  55. System.out.println(e.getFirstName() + " 新增 成功");
  56. } else {
  57. System.out.println(e.getFirstName() + " 新增 失敗");
  58. }
  59. }
  60.  
  61. public static void update() {
  62. String query = "update employee set firstname=? , salary=? where id=?"; // 使用問號代表參數,之後設定
  63. // 取得 Employee 物件
  64. Employee e = getNewEmployee();
  65. // 修改 firstname 與 salary
  66. e.setFirstName("大明");
  67. e.setSalary(100_000);
  68.  
  69. int count = 0; // 有幾筆資料完成修改
  70. try (
  71. Connection con = DriverManager.getConnection(url, user, pass);
  72. PreparedStatement pstmt = con.prepareStatement(query); // PreparedStatement
  73. ) {
  74. // 設定各項 ? 參數值
  75. pstmt.setString(1, e.getFirstName()); // 設定第1個 ? 參數
  76. pstmt.setFloat(2, e.getSalary()); // 設定第2個 ? 參數
  77. pstmt.setInt(3, e.getId()); // 設定第3個 ? 參數
  78.  
  79. // executeUpdate() // 回傳有幾筆資料完成更新
  80. count = pstmt.executeUpdate();
  81.  
  82. } catch (SQLException ex) {
  83. System.err.println(ex);
  84. }
  85. if (count > 0) {
  86. System.out.println("修改 成功");
  87. System.out.println(e);
  88. } else {
  89. System.out.println("修改 失敗");
  90. }
  91. }
  92.  
  93. public static void delete() {
  94. String query = "delete from employee where id=?"; // 使用問號代表參數,之後設定
  95. // 取得 Employee 物件
  96. Employee e = getNewEmployee();
  97. int count = 0; // 有幾筆資料完成修改
  98. try (
  99. Connection con = DriverManager.getConnection(url, user, pass);
  100. PreparedStatement pstmt = con.prepareStatement(query); // PreparedStatement
  101. ) {
  102. // 設定 ? 參數值
  103. pstmt.setInt(1, e.getId()); // 設定第1個 ? 參數
  104.  
  105. // executeUpdate() // 回傳有幾筆資料完成更新
  106. count = pstmt.executeUpdate();
  107.  
  108. } catch (SQLException ex) {
  109. System.err.println(ex);
  110. }
  111. System.out.println(e);
  112. if (count > 0) {
  113. System.out.println("刪除 成功");
  114. } else {
  115. System.out.println("刪除 失敗");
  116. }
  117. }
  118.  
  119. public static void main(String[] args) {
  120. //insert();
  121. //update();
  122. delete();
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement