0
点赞
收藏
分享

微信扫一扫

mysql+jdbc简单的增删改查的程序


一个实现单表增删查改的小程序.
下面代码用的是Customers表,有id(自动增长,主键),name,age,address字段
程序分为有  提供连接的ConnectionProvider类,用于保存记录的对象Customer类,产生界面的Viewer类,控制器Contral类
属性文件内容:文件名是db.conf

1.数据库


  1. #MySQL
  2. JDBC_DRIVER=com.mysql.jdbc.Driver
  3. URL=jdbc:mysql://localhost:3306/test
  4. USER=root
  5. PASSWORD=1234


2.提供连接的类 DAO


  1. package jdbc;
  2. import java.io.*;
  3. import java.sql.*;
  4. import java.util.*;
  5. public class
  6. private static Connection con = null;
  7. private static Properties ps = new
  8. static{
  9. try
  10. in = new FileInputStream("db.conf");
  11. in);
  12. catch
  13.       e.printStackTrace();
  14.     }
  15.   }
  16. public static
  17. "JDBC_DRIVER");
  18. "URL");
  19. "USER");
  20. "PASSWORD");
  21.     
  22. try
  23. "com.mysql.jdbc.Driver");
  24. new
  25.       con = DriverManager.getConnection(URL,USER,PASSWORD);
  26. catch
  27.       e.printStackTrace();
  28.     }
  29.     
  30. return
  31.   }
  32. }


 

3.视图类 (表示层)



  1. package jdbc;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. /**视图类,用来展示记录,和将信息反馈给控制器,并接受和显示控制器返回的结果*/
  6. public class
  7. private
  8. private
  9. private
  10. private
  11. private
  12. private
  13. private
  14.   
  15. private
  16. private
  17. public
  18.     buildViewer();
  19. new
  20.     customer = contral.first();
  21.     show(customer,contral.getSize());
  22.   }
  23. public void
  24. new
  25. new JLabel("                                                        第0条记录,一共有0条记录");
  26.     jf.add(topLabel,BorderLayout.NORTH);
  27. new JPanel(new
  28. new JLabel("id");
  29. new JLabel("name");
  30. new JLabel("age");
  31. new JLabel("address");
  32. new
  33. new
  34. new
  35. new
  36.     
  37.     jpCenter.add(idLabel);
  38.     jpCenter.add(idField);
  39.     jpCenter.add(nameLabel);
  40.     jpCenter.add(nameField);
  41.     jpCenter.add(ageLabel);
  42.     jpCenter.add(ageField);
  43.     jpCenter.add(addressLabel);
  44.     jpCenter.add(addressField);
  45.     
  46.     jf.add(jpCenter);
  47.     
  48. new
  49. new JButton("first");
  50. new JButton("previous");
  51. new JButton("next");
  52. new JButton("last");
  53. new JButton("insert");
  54. new JButton("update");
  55. new JButton("dalete");
  56. new JButton("refresh");
  57. this);
  58. this);
  59. this);
  60. this);
  61. this);
  62. this);
  63. this);
  64. this);
  65.     
  66.     jpSouth.add(first);
  67.     jpSouth.add(previous);
  68.     jpSouth.add(next);
  69.     jpSouth.add(last);
  70.     jpSouth.add(insert);
  71.     jpSouth.add(update);
  72.     jpSouth.add(dalete);
  73.     jpSouth.add(refresh);
  74.     
  75.     jf.add(jpSouth,BorderLayout.SOUTH);
  76.     
  77.     jf.setSize(600,200);
  78.     jf.setLocation(200, 200);
  79. true);
  80.     jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  81.   }
  82. /**展示记录*/
  83. public void
  84. "");
  85.     nameField.setText(customer.getName());
  86. "");
  87.     addressField.setText(customer.getAddress());
  88. "                                                        第"
  89. ":")[0]
  90. "条记录,一共有"+size.split(":")[1]+"条记录");
  91.   }
  92. /**事件监听*/
  93. public void
  94.     String comm = e.getActionCommand();
  95. if("first".equals(comm)){
  96.       customer = contral.first();
  97.       show(customer,contral.getSize());
  98. else if("previous".equals(comm)){
  99.       customer = contral.previous();
  100.       show(customer,contral.getSize());
  101. else if("next".equals(comm)){
  102.       customer = contral.next();
  103.       show(customer,contral.getSize());
  104. else if("last".equals(comm)){
  105.       customer = contral.last();
  106.       show(customer,contral.getSize());
  107. else if("insert".equals(comm)){
  108.       String name = nameField.getText();
  109. int
  110.       String address = addressField.getText();
  111.       customer = contral.insert(name,age,address);
  112.       show(customer,contral.getSize());
  113. else if("update".equals(comm)){
  114.       String name = nameField.getText();
  115. int
  116.       String address = addressField.getText();
  117.       contral.update(name,age,address);
  118. else if("dalete".equals(comm)){
  119.       customer = contral.dalete();
  120.       show(customer,contral.getSize());
  121. else if("refresh".equals(comm)){
  122.       customer = contral.refresh();
  123.       show(customer,contral.getSize());
  124.     }
  125.   }
  126. public static void
  127. new
  128.   }
  129. }


 

4.控制器类(逻辑层)



  1. package jdbc;
  2. import java.sql.*;
  3. import java.util.*;
  4. public class
  5. private int
  6. private Connection con = null;
  7. private Statement stmt = null;
  8. private ResultSet rs = null;
  9. private
  10. /**构造,产生对象结果集合*/
  11. public
  12.     buildCustomersList();
  13.   }
  14. /**执行查询语句,将结果集转换成对象集*/
  15. private void
  16. new
  17.     con = ConnectionProvider.getConnection();
  18. try
  19.       stmt = con.createStatement();
  20. "select * from customers");
  21. new
  22. while(rs.next()){
  23. int
  24.         String name = toGB(rs.getString(2));
  25. int
  26.         String address = toGB(rs.getString(4));
  27. new
  28.         customers.add(customer);
  29.       }
  30. catch
  31.       e.printStackTrace();
  32. finally{
  33.       closeConnection(con);
  34.       closeStatement(stmt);
  35.       closeResultSet(rs);
  36.     }
  37.   }
  38. /**显示第一个元素*/
  39. public
  40.     cur = 0;
  41. return customers.get(cur);
  42.   }
  43. /**显示前一个元素*/
  44. public
  45. if(cur>0)
  46.       cur--;
  47. return customers.get(cur);
  48.   }
  49. /**显示下一个*/
  50. public
  51. if(cur<customers.size()-1)
  52.       cur++;
  53. return customers.get(cur);
  54.   }
  55. /**显示最后一个*/
  56. public
  57.     cur = customers.size()-1;
  58. return customers.get(cur);
  59.   }
  60. /**插入记录*/
  61. public Customer insert(String name,int
  62.     Customer customer;
  63. "insert into customers (name,age,address)value("
  64. "'" + toISO(name) + "'" + ","
  65. ","
  66. "'" + toISO(address) + "'" + ")";
  67.     boolean isOk = exe(sql);
  68. try{
  69. if(isOk){
  70.         con = ConnectionProvider.getConnection();
  71.         stmt = con.createStatement();
  72. "select id from customers where name="
  73. "'" + toISO(name) + "'";
  74.         rs = stmt.executeQuery(sql);
  75.         rs.next();
  76. new
  77.         customers.add(cur,customer);
  78. return
  79. else{
  80. return customers.get(cur);
  81.       }
  82. catch(Exception e){
  83.       e.printStackTrace();
  84. return customers.get(cur);
  85. finally{
  86.       closeConnection(con);
  87.       closeStatement(stmt);
  88.     }
  89.   }
  90. /**记录更新*/
  91. public void update(String name,int
  92. get(cur);
  93.     customer.setName(name);
  94.     customer.setAge(age);
  95.     customer.setAddress(address);
  96. "update customers set name=" + "'" + toISO(name) + "'" + ","
  97. "age=" + age + ","
  98. "address=" +"'" + toISO(address) + "'"
  99. "where id="
  100.     exe(sql);
  101.   }
  102. /**删除记录*/
  103. public
  104. get(cur);
  105. "delete from customers where id="
  106.     exe(sql);
  107.     customers.remove(cur);
  108. if(cur<customers.size()-1){
  109. get(cur);
  110. else{
  111.       cur = 0;
  112. get(cur);
  113.     }
  114. return
  115.   }
  116. /**刷新结果集合*/
  117. public
  118.     buildCustomersList();
  119. return
  120.   }
  121. /**用于执行SQL语句*/
  122. private
  123.     con = ConnectionProvider.getConnection();
  124. try
  125.       stmt = con.createStatement();
  126.       stmt.execute(sql);
  127. return true;
  128. catch
  129.       e.printStackTrace();
  130. return false;
  131. finally{
  132.       closeConnection(con);
  133.       closeStatement(stmt);
  134.       closeResultSet(rs);
  135.     }
  136.   }
  137. /**获取总记录数和当前记录数的字符串表示*/
  138. public
  139. return (cur+1) + ":"
  140.   }
  141. /**把gb2312转化成ISO-8859-1*/
  142. private
  143. try
  144. return str = new String(str.getBytes("GB2312"),"ISO-8859-1");
  145. catch
  146. return
  147.   }
  148. /**把ISO-8859-1转化成gb2312*/
  149. private
  150. try
  151. return str = new String(str.getBytes("ISO-8859-1"),"GB2312");
  152. catch
  153. return
  154.   }
  155. /**关闭连接*/
  156. private void
  157. try{if(con!=null)con.close();}catch(Exception e){e.printStackTrace();}
  158.   }
  159. /**关闭语句*/
  160. private void
  161. try{if(stmt!=null)stmt.close();}catch(Exception e){e.printStackTrace();}
  162.   }
  163. /**关闭结果集*/
  164. private void
  165. try{if(rs!=null)rs.close();}catch(Exception e){e.printStackTrace();}
  166.   }
  167. }


5.POJO 基本的bean类



  1. package jdbc;

  2. public class
  3. private int
  4. private
  5. private int
  6. private
  7. public Customer(int id,String name,int
  8. this.id = id;
  9. this.name = name;
  10. this.age = age;
  11. this.address = address;
  12.   }
  13. public int
  14. return
  15.   }
  16. public void setId(int
  17. this.id = id;
  18.   }
  19. public
  20. return
  21.   }
  22. public void
  23. this.name = name;
  24.   }
  25. public int
  26. return
  27.   }
  28. public void setAge(int
  29. this.age = age;
  30.   }
  31. public
  32. return
  33.   }
  34. public void
  35. this.address = address;
  36.   }
  37.   
  38. }
举报

相关推荐

0 条评论