JDBC学习总结5-------使用了内部类后的改造

1.com.hanchao.entity包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package  com.hanchao.entity; 
/** 
  * 实体类 
  * @author hanlw 
  * 2012-07-09 
  */ 
public  class  User { 
  
     private  int  id; 
     private  String username; 
     private  String address; 
      
     //get...set...方法 
     public  int  getId() { 
         return  id; 
    
     public  void  setId( int  id) { 
         this .id = id; 
    
     public  String getUsername() { 
         return  username; 
    
     public  void  setUsername(String username) { 
         this .username = username; 
    
     public  String getAddress() { 
         return  address; 
    
     public  void  setAddress(String address) { 
         this .address = address; 
    
      
}

2.com.hanchao.util包

《1》.一个接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package  com.hanchao.util; 
  
import  java.sql.ResultSet; 
import  java.sql.SQLException; 
  
/** 
  * 顶一个泛型的RowMapper接口 
  * @author hanlw 
  * 2012-07-09 
  */ 
public  interface  RowMapper<T> { 
     /** 
      * 注意:T表示一个泛型的实体类。 
      */ 
     public  T mapperRow(ResultSet rs)  throws  SQLException; 
}

《2》.工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package  com.hanchao.util; 
  
import  java.sql.Connection; 
import  java.sql.DriverManager; 
import  java.sql.PreparedStatement; 
import  java.sql.ResultSet; 
import  java.sql.SQLException; 
import  java.util.ArrayList; 
import  java.util.List; 
  
/** 
  * 顶一个泛型的工具类 
  * @author hanlw 
  * 2012-07-09 
  */ 
public  class  DBHelp<T> { 
     //定义几个常量 
     private  static  final  String DRIVER =  "com.mysql.jdbc.Driver"
     private  static  final  String URL =  "jdbc:mysql://127.0.0.1:3306/mydb"
     private  static  final  String DB_NAME =  "root"
     private  static  final  String DB_PASSWORD =  "root"
      
     /** 
      * 连接数据库 
      * @return 
      */ 
     public  Connection getConnection() { 
         Connection con =  null
         try 
             Class.forName(DRIVER); 
             con = DriverManager.getConnection(URL,DB_NAME,DB_PASSWORD); 
         catch  (Exception e) { 
             e.printStackTrace(); 
        
         return  con; 
    
      
     /** 
      * 返回一个对象的查询 
      * @param sql SQL语句 
      * @param rm  该接口的所有实现类对象 
      * @param args 不定项参数 
      * @return 
      */ 
     public  T queryForObject(String sql, RowMapper<T> rm, Object...args) { 
         Connection con = getConnection(); 
         PreparedStatement sta =  null
         ResultSet rs =  null
          
         T obj =  null
          
         try 
             sta = con.prepareStatement(sql); 
             for  ( int  i =  0 ; i < args.length; i++) { 
                 sta.setObject(i+ 1 , args[i]); // 
            
             rs = sta.executeQuery(); 
             if (rs.next()) { 
                 obj = rm.mapperRow(rs); //实际上运行是其实现类中的mapperRow()方法,即实现类重现后的方法 
            
              
         catch  (SQLException e) { 
             e.printStackTrace(); 
         finally 
             close(rs,sta,con); 
        
          
         return  obj; 
    
      
     /** 
      * 返回一个集合的查询 
      * @param sql → SQL语句 
      * @param rm → RowMapper<T>接口的实现类对象 
      * @param args → 不定项参数 
      * @return 
      */ 
     public  List<T> queryForList(String sql,RowMapper<T> rm, Object...args) { 
         Connection con = getConnection(); 
         PreparedStatement sta =  null
         ResultSet rs =  null
          
         List<T> list =  new  ArrayList<T>(); 
          
         try 
             sta = con.prepareStatement(sql); 
             for  ( int  i =  0 ; i < args.length; i++) { 
                 sta.setObject(i+ 1 , args[i]); 
            
              
             rs = sta.executeQuery(); 
             while (rs.next()) { 
                 T obj = rm.mapperRow(rs); 
                 list.add(obj); 
            
              
         catch  (SQLException e) { 
             e.printStackTrace(); 
         finally 
             close(rs,sta,con); 
        
         return  list; 
    
      
     /** 
      * 用来执行insert、update、delete方法 
      * @param sql → SQL语句 
      * @param args → 不定项参数 
      * @return 
      */ 
     public  int  executeSQL(String sql, Object...args) { 
         Connection con = getConnection(); 
         PreparedStatement sta =  null
         int  rows =  0
          
         try 
             sta = con.prepareStatement(sql); 
             for  ( int  i =  0 ; i < args.length; i++) { 
                 sta.setObject(i+ 1 , args[i]); 
            
              
             rows = sta.executeUpdate(); 
             if (rows >  0 ) { 
                 System.out.println( "ok" ); 
             else 
                 System.out.println( "no" ); 
            
         catch  (Exception e) { 
             e.printStackTrace(); 
         finally 
             close(sta,con); 
        
         return  rows; 
    
      
     /** 
      * 关闭 → 注意关闭的顺序 rs,sta,con.   
      * @param rs 
      * @param sta 
      * @param con 
      */ 
     public  void  close(ResultSet rs, PreparedStatement sta, Connection con) { 
         try 
             if (rs !=  null ) { 
                 rs.close(); 
            
         catch  (SQLException e) { 
             e.printStackTrace(); 
         finally 
             try 
                 if (sta !=  null ) { 
                     sta.close(); 
                
             catch  (SQLException e) { 
                 e.printStackTrace(); 
             finally 
                 try 
                     if (con !=  null ) { 
                         con.close();     
                    
                 catch  (SQLException e) { 
                     e.printStackTrace(); 
                
            
        
    
     /** 
      * sta,con的关闭 
      * @param sta 
      * @param con 
      */ 
     public  void  close(PreparedStatement sta, Connection con) { 
         this .close( null , sta, con); 
    
     //************************************************ 
}

3.com.hanchao.dao包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package  com.hanchao.dao; 
  
import  java.sql.ResultSet; 
import  java.sql.SQLException; 
import  java.util.List; 
  
import  com.hanchao.entity.User; 
import  com.hanchao.util.DBHelp; 
import  com.hanchao.util.RowMapper; 
  
/** 
  * 对User对象操作的Dao类 
  * @author hanlw 
  * 2012-07-09 
  */ 
public  class  UserDao { 
  
     private  DBHelp<User> db =  new  DBHelp<User>(); 
      
     /** 
      * 添加操作 
      * @param name 
      * @param address 
      * @return 
      */ 
     public  int  save(String name, String address) { 
         String sql =  "insert into t_user(username,address) values(?,?)"
         return  db.executeSQL(sql, name,address); 
    
      
     /** 
      * 更新操作 
      * @param user 
      * @return 
      */ 
     public  int  update(User user) { 
         String sql =  "update t_user set username=?,address=? where id=?"
         return  db.executeSQL(sql, user.getUsername(),user.getAddress(),user.getId()); 
    
      
     /** 
      * 删除操作 
      * @param id 
      * @return 
      */ 
     public  int  delete( int  id) { 
         String sql =  "delete from t_user where id=?"
         return  db.executeSQL(sql, id); 
    
      
     /** 
      * 查找单独的一个用户 
      * @param name 
      * @return 
      */ 
     public  User findByName(String name) { 
         String sql =  "select id,username,address from t_user where username=?"
         return  db.queryForObject(sql,  new  UserRowMpper(), name); 
    
      
     /** 
      * 查询所有 
      * @return 
      */ 
     public  List<User> findAll() { 
         String sql =  "select id,username,address from t_user"
         return  db.queryForList(sql,  new  UserRowMpper()); 
    
      
     /** 
      * 接口RowMapper的一个实现类  (这是一个内部类哦) 
      * 具体了解内部类的知识:http://hanchaohan.blog.51cto.com/2996417/930721 
      * @author hanlw 
     
      */ 
     private  class  UserRowMpper  implements  RowMapper<User> {  
         /** 
          * 实现该接口的所有的方法 
          */ 
         public  User mapperRow(ResultSet rs)  throws  SQLException { 
             User user =  new  User(); 
             user.setId(rs.getInt( "id" )); 
             user.setUsername(rs.getString( "username" )); 
             user.setAddress(rs.getString( "address" )); 
             return  user; 
        
          
    
}

4.com.hanchao.test包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package  com.hanchao.test; 
  
import  java.util.List; 
  
import  com.hanchao.dao.UserDao; 
import  com.hanchao.entity.User; 
  
/** 
  * 测试 
  * @author hanlw 
  * 2012-07-09 
  */ 
public  class  Test { 
  
     public  static  void  main(String[] args) { 
          
         /** 
          * 1.添加操作 
          */ 
         UserDao dao =  new  UserDao(); 
         dao.save( "tom" "cherry" ); 
          
         /** 
          * 2.更新操作 
          */ 
         User user =  new  User(); 
         user.setId( 32 ); 
         user.setAddress( "" ); 
         user.setUsername( "chenchen" ); 
         dao.update(user); 
          
         /** 
          * 3.删除操作 
          */ 
         dao.delete( 31 ); 
          
         /** 
          * 4.查找一个对象 
          */ 
         User u = dao.findByName( "hanchao" ); 
         System.out.println( "----" +u.getAddress()); 
          
         /** 
          * 查找一个集合 
          */ 
         List<User> list = dao.findAll(); 
         for (User us : list) { 
             System.out.println(us.getUsername()); 
        
    
}

关于泛型,可以了解一下这篇文字:http://www.cnblogs.com/yinhaiming/articles/1749738.html


     本文转自韩立伟 51CTO博客,原文链接:http://blog.51cto.com/hanchaohan/930862,如需转载请自行联系原作者







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值