首先要在数据库中建好表,表的属性要跟代码中的一致

使用jdbk连接数据库,并且进行增删改查的操作(curd).

 1 package com.beiwi;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.util.ResourceBundle;
 6 
 7 import java.sql.ResultSet;
 8 import java.sql.Statement;
 9 
10 /**
11  *    数据库的帮助类
12  * @author Lrs
13  *
14  */
15 public class DBUtils {
16 
17     private static String driverClass;
18     private static String url;
19     private static String user;
20     private static String password;
21     
22     
23     static{
24         ResourceBundle rb = ResourceBundle.getBundle("jdbc");
25         driverClass = rb.getString("driverClass");
26         url = rb.getString("url");
27         user = rb.getString("user");
28         password = rb.getString("password");
29         
30         try {
31             Class.forName(driverClass);
32         } catch (ClassNotFoundException e) {
33             // TODO Auto-generated catch block
34             e.printStackTrace();
35         }
36     }
37     
38     public static Connection getConn() throws Exception{
39         return DriverManager.getConnection(url, user, password);
40     }
41     
42     public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
43         
44         if(null!=rs){
45             try {
46                 rs.close();
47             } catch (Exception e) {
48                 e.printStackTrace();
49             }
50         }
51         if(null!=stmt){
52             try {
53                 stmt.close();
54             } catch (Exception e) {
55                 e.printStackTrace();
56             }
57         }
58         
59         if(null!=conn){
60             try {
61                 conn.close();
62             } catch (Exception e) {
63                 e.printStackTrace();
64             }
65         }
66     }
67 
68     
69 }

 

  1 package com.beiwi;
  2 
  3 import java.sql.Connection;
  4 
  5 import java.sql.ResultSet;
  6 import java.sql.Statement;
  7 
  8 
  9 /**
 10  * 增删改查操作的方法
 11  *  create:新增
 12  *  refer:查询
 13  *  update:更新
 14  *  delete:删除
 15  * @author Lrs
 16  *
 17  */
 18 public class MasterCurd {
 19 
 20     
 21     /**
 22      * 查询所有主人的信息
 23      */
 24     public void findAll(){
 25         String sql = "SELECT * FROM master";
 26         Connection conn = null;
 27         Statement stmt = null;
 28         ResultSet rs = null;
 29         
 30         try {
 31             conn = DBUtils.getConn();
 32             stmt = conn.createStatement();
 33             rs = stmt.executeQuery(sql);
 34             
 35             while (rs.next()) {
 36                 System.out.println(rs.getObject(1));
 37                 System.out.println(rs.getObject(2));
 38                 System.out.println(rs.getObject(3));
 39                 System.out.println(rs.getObject(4));
 40                 System.out.println("**************************");
 41             }
 42         } catch (Exception e) {
 43             e.printStackTrace();
 44         }finally{
 45             DBUtils.closeAll(rs, stmt, conn);
 46         }
 47         
 48     }
 49     /**
 50      *  insert一条记录
 51      */
 52     
 53     public void insert(){
 54         String sql = "INSERT INTO master (id,user,password,money) VALUES ('4','haha','456','100');";
 55         
 56         Connection conn = null;
 57         Statement stmt = null;
 58         
 59         try {
 60             conn = DBUtils.getConn();
 61             stmt = conn.createStatement();
 62             stmt.executeUpdate(sql);
 63         } catch (Exception e) {
 64             e.printStackTrace();
 65         } finally {
 66             DBUtils.closeAll(null, stmt, conn);
 67         }
 68     }
 69     
 70     /**
 71      * 更新一条记录
 72      */
 73     public void update(){
 74         String sql = "UPDATE master SET user='beiwo1',password='456',money=888 WHERE id=1";
 75         Connection conn = null;
 76         Statement stmt = null;
 77         
 78         try {
 79             conn = DBUtils.getConn();
 80             stmt = conn.createStatement();
 81             int num = stmt.executeUpdate(sql);
 82             
 83             if (num>0) {
 84                 System.out.println("更新成功");
 85             }
 86             
 87         } catch (Exception e) {
 88             e.printStackTrace();
 89         } finally {
 90             DBUtils.closeAll(null, stmt, conn);
 91         }
 92     }
 93     /**
 94      * 删除一条记录
 95      */
 96     public void delete(){
 97         String sql = "DELETE FROM master WHERE id=3";
 98         Connection  conn = null;
 99         Statement stmt = null;
100         
101         try {
102             conn = DBUtils.getConn();
103             stmt = conn.createStatement();
104             int num = stmt.executeUpdate(sql);
105             
106             if (num > 0) {
107                 System.out.println("删除成功");
108             }
109         } catch (Exception e) {
110             e.printStackTrace();
111         } finally {
112             DBUtils.closeAll(null, stmt, conn);
113         }
114     }
115 }
 1 package com.beiwi;
 2 
 3 import org.junit.Before;
 4 import org.junit.Test;
 5 
 6 /**
 7  *  测试类
 8  */
 9 
10 public class TestMasterCrud {
11 
12     MasterCurd curd = null;
13     
14     @Before
15     public void before(){
16         curd = new MasterCurd();
17     }
18     @Test
19     public void testFindAll(){
20         curd.findAll();
21     }
22     @Test
23     public void testInsert(){
24         curd.insert();
25     }
26     @Test
27     public void testUpdate(){
28         curd.update();
29     }
30     @Test
31     public void testDelete(){
32         curd.delete();
33     }
34     @Test
35     public void after(){
36         curd=null;
37     }
38     
39 }

 

posted on 2016-12-14 10:05  栗子先生i  阅读(921)  评论(0编辑  收藏  举报