jdbc学习总结3------javab

发布时间:2019-09-14 09:49:50编辑:auto阅读(1810)

     1.测试类的内容:

    在包:com.hanchao.test中

     

    1. package com.hanchao.test; 
    2.  
    3. import com.hanchao.dao.UserDao; 
    4. import com.hanchao.entity.User; 
    5.  
    6. /** 
    7.  * 测试jdbc的类(javaBean+DAO) 
    8.  * @author hanlw 
    9.  * 2012-07-09 
    10.  */ 
    11. public class Test { 
    12.  
    13.     /** 
    14.      * 1.什么是JavaBean? 
    15.      *   它要满足两个条件:<1>.私有化属性;<2>.公开访问方法(setter and getter) 
    16.      *    
    17.      * 2.javabean又名:entity实体类、pojo、vo 
    18.      *  
    19.      * 3.DAO = data access object (封装了对一个表的所有CRUD操作!!) 
    20.      *  DAO中方法的本质是将对象(entity)类和数据库进行交互。 
    21.      */ 
    22.  
    23.     public static void main(String[] args) { 
    24.          
    25.         /** 
    26.          * 1.insert()操作测试 
    27.          */ 
    28. /*      UserDao userDao = new UserDao(); 
    29.         User user = new User(); 
    30.          
    31.         user.setUsername("chenchen"); 
    32.         user.setAddress("jiangsu"); 
    33.         userDao.insert(user); 
    34. */ 
    35.      
    36.         /** 
    37.          * 2.update()操作 
    38.          */ 
    39. /*      UserDao userDao = new UserDao(); 
    40.         User user = new User(); 
    41.          
    42.         user.setId(20); 
    43.         user.setAddress("yancheng1"); 
    44.         user.setUsername("iloveyou"); 
    45.         userDao.update(user); 
    46. */ 
    47.          
    48.         /** 
    49.          * delete()操作 
    50.          */ 
    51. //      UserDao userDao = new UserDao(); 
    52. //      userDao.delete(21); 
    53.          
    54.         /** 
    55.          * retrieve()操作 
    56.          */ 
    57.         UserDao userDao = new UserDao(); 
    58.         userDao.retrieve(22); 
    59.          
    60.     } 
    61.      

    2.实体类的写法:com.hanchao.entity

     

    1. package com.hanchao.entity; 
    2. /** 
    3.  * 实体类 
    4.  * @author hanlw 
    5.  * 2012-07-09 
    6.  */ 
    7. public class User { 
    8.  
    9.     /** 
    10.      * 1.实体类的类名一般和数据库中的相应的表名相同:如t_user对应的实体类为User 
    11.      *  
    12.      * 2.实体类中的属性一般与表中的列名相同:如下 
    13.      *  
    14.      * 3.写属性的getter 和 setter方法 
    15.      */ 
    16.      
    17.     private int id; 
    18.     private String username; 
    19.     private String address; 
    20.      
    21.     //下面是setter...getter.. 
    22.     public int getId() { 
    23.         return id; 
    24.     } 
    25.     public void setId(int id) { 
    26.         this.id = id; 
    27.     } 
    28.     public String getUsername() { 
    29.         return username; 
    30.     } 
    31.     public void setUsername(String username) { 
    32.         this.username = username; 
    33.     } 
    34.     public String getAddress() { 
    35.         return address; 
    36.     } 
    37.     public void setAddress(String address) { 
    38.         this.address = address; 
    39.     } 
    40.      
    41.      

    3.dao的写法:com.hanchao.dao

     

    1. package com.hanchao.dao; 
    2.  
    3. import java.sql.Connection; 
    4. import java.sql.DriverManager; 
    5. import java.sql.PreparedStatement; 
    6. import java.sql.ResultSet; 
    7. import java.sql.SQLException; 
    8.  
    9. import com.hanchao.entity.User; 
    10.  
    11. /** 
    12.  * User类对应的Dao 
    13.  * @author hanlw 
    14.  * 2012-07-09 
    15.  */ 
    16. public class UserDao { 
    17.  
    18.     /** 
    19.      * 说明: 
    20.      * 1.某个实体类对应的Dao一般写成(类名+Dao),如User类对应Dao类为:UserDao 
    21.      *  
    22.      * 2.Dao中封装了对对应实体类的所有的CRUD的操作!!如下: 
    23.      */ 
    24.      
    25.     /** 
    26.      * 1.对mysql的insert操作 
    27.      */ 
    28.     public int insert(User user) { 
    29.         Connection con = null
    30.         PreparedStatement sta = null
    31.         int rows = 0
    32.          
    33.         try { 
    34.              
    35.             Class.forName("com.mysql.jdbc.Driver"); 
    36.             con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root"); 
    37.              
    38.             String sql = "insert into t_user(username,address) value(?,?)"
    39.             sta = con.prepareStatement(sql); 
    40.             sta.setString(1, user.getUsername()); 
    41.             sta.setString(2, user.getAddress()); 
    42.              
    43.             rows = sta.executeUpdate(); 
    44.             if(rows > 0) { 
    45.                 System.out.println("operate successfully!!"); 
    46.             } 
    47.              
    48.         } catch (ClassNotFoundException e) { 
    49.             e.printStackTrace(); 
    50.         } catch (SQLException e) { 
    51.             e.printStackTrace(); 
    52.         } finally { 
    53.             if(sta != null) { 
    54.                 try { 
    55.                     sta.close(); 
    56.                 } catch (SQLException e) { 
    57.                     e.printStackTrace(); 
    58.                 } finally { 
    59.                     if(con != null) { 
    60.                         try { 
    61.                             con.close(); 
    62.                         } catch (SQLException e) { 
    63.                             e.printStackTrace(); 
    64.                         } 
    65.                     } 
    66.                 } 
    67.             } 
    68.         } 
    69.         return rows; 
    70.     } 
    71.      
    72.     /** 
    73.      * 2.对mysql的update操作 
    74.      */ 
    75.     public int update(User user) { 
    76.         Connection con = null
    77.         PreparedStatement sta = null
    78.         int rows = 0
    79.          
    80.         try { 
    81.             Class.forName("com.mysql.jdbc.Driver"); 
    82.             con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root"); 
    83.              
    84.             String sql = "update t_user set address=?,username=? where id=?"
    85.             sta = con.prepareStatement(sql); 
    86.             sta.setString(1, user.getAddress()); 
    87.             sta.setString(2, user.getUsername()); 
    88.             sta.setInt(3, user.getId()); 
    89.              
    90.             rows = sta.executeUpdate(); 
    91.             if(rows > 0) { 
    92.                 System.out.println("ok"); 
    93.             } 
    94.         } catch (ClassNotFoundException e) { 
    95.             e.printStackTrace(); 
    96.         } catch (SQLException e) { 
    97.             e.printStackTrace(); 
    98.         } finally { 
    99.             if(sta != null) { 
    100.                 try { 
    101.                     sta.close(); 
    102.                 } catch (SQLException e) { 
    103.                     e.printStackTrace(); 
    104.                 } finally { 
    105.                     if(con != null) { 
    106.                         try { 
    107.                             con.close(); 
    108.                         } catch (SQLException e) { 
    109.                             e.printStackTrace(); 
    110.                         } 
    111.                     } 
    112.                 } 
    113.             } 
    114.         } 
    115.         return rows; 
    116.     } 
    117.      
    118.     /** 
    119.      * 3.对mysql的delete()操作 
    120.      */ 
    121.     public int delete(int id) { 
    122.         Connection con = null
    123.         PreparedStatement sta = null
    124.         int rows = 0
    125.          
    126.         try { 
    127.             Class.forName("com.mysql.jdbc.Driver"); 
    128.             con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root"); 
    129.              
    130.             String sql = "delete from t_user where id=?"
    131.             sta = con.prepareStatement(sql); 
    132.             sta.setInt(1, id); 
    133.              
    134.             rows = sta.executeUpdate(); 
    135.             if(rows > 0) { 
    136.                 System.out.println("ok,ok,ok"); 
    137.             } 
    138.              
    139.         } catch (ClassNotFoundException e) { 
    140.             e.printStackTrace(); 
    141.         } catch (SQLException e) { 
    142.             e.printStackTrace(); 
    143.         } finally { 
    144.             if(sta != null) { 
    145.                 try { 
    146.                     sta.close(); 
    147.                 } catch (SQLException e) { 
    148.                     e.printStackTrace(); 
    149.                 } finally { 
    150.                     if(con != null) { 
    151.                         try { 
    152.                             con.close(); 
    153.                         } catch (SQLException e) { 
    154.                             e.printStackTrace(); 
    155.                         } 
    156.                     } 
    157.                 } 
    158.             } 
    159.         } 
    160.         return rows; 
    161.     } 
    162.      
    163.      
    164.     /** 
    165.      * 4.retrieve()操作 
    166.      */ 
    167.     public void retrieve(int id) { 
    168.         Connection con = null
    169.         PreparedStatement sta = null
    170.         ResultSet rs = null
    171.          
    172.         try { 
    173.             Class.forName("com.mysql.jdbc.Driver"); 
    174.             con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root"); 
    175.              
    176.             String sql = "select id,username,address from t_user where id=?"
    177.             sta = con.prepareStatement(sql); 
    178.             sta.setInt(1, id); 
    179.              
    180.             rs = sta.executeQuery(); 
    181.             if(rs.next()) { 
    182.                 int idd = rs.getInt("id"); 
    183.                 String username = rs.getString("username"); 
    184.                 String adrress = rs.getString("address"); 
    185.                 System.out.println(idd+"\t"+username+"\t"+adrress); 
    186.             } 
    187.              
    188.         } catch (ClassNotFoundException e) { 
    189.             e.printStackTrace(); 
    190.         } catch (SQLException e) { 
    191.             e.printStackTrace(); 
    192.         } finally { 
    193.             if(rs != null) { 
    194.                 try { 
    195.                     rs.close(); 
    196.                 } catch (SQLException e) { 
    197.                     e.printStackTrace(); 
    198.                 } finally { 
    199.                     if(sta != null) { 
    200.                         try { 
    201.                             sta.close(); 
    202.                         } catch (SQLException e) { 
    203.                             e.printStackTrace(); 
    204.                         } finally { 
    205.                             if(con != null) { 
    206.                                 try { 
    207.                                     con.close(); 
    208.                                 } catch (SQLException e) { 
    209.                                     e.printStackTrace(); 
    210.                                 } 
    211.                             } 
    212.                         } 
    213.                     } 
    214.                 } 
    215.             } 
    216.         } 
    217.     } 
    218.      

     看dao中的内容,我们发现几个方法中的代码有很多重复的!!所以在下一篇文章中,我们要对代码进行优化!

     

    我把前三篇文章的例子,包括数据库,放在一个压缩包里!名称为:jdbc学习例子.rar

    需要的同学可以去下载!!

关键字