Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 312|回复: 0

[JDBC学习]jdbc学习:javabean+dao

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-11 02:03:58 | 显示全部楼层 |阅读模式
    1.测试类的内容:
        在包:com.hanchao.test中
       
    1. [/code]
    2.    
    3.      package com.hanchao.test;
    4.       
    5.      import com.hanchao.dao.UserDao;
    6.      import com.hanchao.entity.User;
    7.       
    8.      /**
    9.       * 测试jdbc的类(javaBean+DAO)
    10.       * @author hanlw
    11.       * 2012-07-09
    12.       */
    13.      public class Test {
    14.       
    15.          /**
    16.           * 1.什么是JavaBean?
    17.           *   它要满足两个条件:<1>.私有化属性;<2>.公开访问方法(setter and getter)
    18.           *   
    19.           * 2.javabean又名:entity实体类、pojo、vo
    20.           *  
    21.           * 3.DAO = data access object (封装了对一个表的所有CRUD操作!!)
    22.           *  DAO中方法的本质是将对象(entity)类和数据库进行交互。
    23.           */
    24.       
    25.          public static void main(String[] args) {
    26.               
    27.              /**
    28.               * 1.insert()操作测试
    29.               */
    30.      /*      UserDao userDao = new UserDao();
    31.              User user = new User();
    32.               
    33.              user.setUsername("chenchen");
    34.              user.setAddress("jiangsu");
    35.              userDao.insert(user);
    36.      */
    37.          
    38.              /**
    39.               * 2.update()操作
    40.               */
    41.      /*      UserDao userDao = new UserDao();
    42.              User user = new User();
    43.               
    44.              user.setId(20);
    45.              user.setAddress("yancheng1");
    46.              user.setUsername("iloveyou");
    47.              userDao.update(user);
    48.      */
    49.               
    50.              /**
    51.               * delete()操作
    52.               */
    53.      //      UserDao userDao = new UserDao();
    54.      //      userDao.delete(21);
    55.               
    56.              /**
    57.               * retrieve()操作
    58.               */
    59.              UserDao userDao = new UserDao();
    60.              userDao.retrieve(22);
    61.               
    62.          }
    63.          
    64.      }
    65.       
    66.     2.实体类的写法:com.hanchao.entity
    67.       
    68.     [code]
    复制代码
       
         package com.hanchao.entity;
         /**
          * 实体类
          * @author hanlw
          * 2012-07-09
          */
         public class User {
          
             /**
              * 1.实体类的类名一般和数据库中的相应的表名相同:如t_user对应的实体类为User
              *  
              * 2.实体类中的属性一般与表中的列名相同:如下
              *  
              * 3.写属性的getter 和 setter方法
              */
             
             private int id;
             private String username;
             private String address;
             
             //下面是setter...getter..
             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;
             }
             
             
         }
          
        3.dao的写法:com.hanchao.dao
          
        [code][/code]
       
         package com.hanchao.dao;
          
         import java.sql.Connection;
         import java.sql.DriverManager;
         import java.sql.PreparedStatement;
         import java.sql.ResultSet;
         import java.sql.SQLException;
          
         import com.hanchao.entity.User;
          
         /**
          * User类对应的Dao
          * @author hanlw
          * 2012-07-09
          */
         public class UserDao {
          
             /**
              * 说明:
              * 1.某个实体类对应的Dao一般写成(类名+Dao),如User类对应Dao类为:UserDao
              *  
              * 2.Dao中封装了对对应实体类的所有的CRUD的操作!!如下:
              */
             
             /**
              * 1.对mysql的insert操作
              */
             public int insert(User user) {
                 Connection con = null;
                 PreparedStatement sta = null;
                 int rows = 0;
                  
                 try {
                      
                     Class.forName("com.mysql.jdbc.Driver");
                     con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");
                      
                     String sql = "insert into t_user(username,address) value(?,?)";
                     sta = con.prepareStatement(sql);
                     sta.setString(1, user.getUsername());
                     sta.setString(2, user.getAddress());
                      
                     rows = sta.executeUpdate();
                     if(rows > 0) {
                         System.out.println("operate successfully!!");
                     }
                      
                 } catch (ClassNotFoundException e) {
                     e.printStackTrace();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 } finally {
                     if(sta != null) {
                         try {
                             sta.close();
                         } catch (SQLException e) {
                             e.printStackTrace();
                         } finally {
                             if(con != null) {
                                 try {
                                     con.close();
                                 } catch (SQLException e) {
                                     e.printStackTrace();
                                 }
                             }
                         }
                     }
                 }
                 return rows;
             }
             
             /**
              * 2.对mysql的update操作
              */
             public int update(User user) {
                 Connection con = null;
                 PreparedStatement sta = null;
                 int rows = 0;
                  
                 try {
                     Class.forName("com.mysql.jdbc.Driver");
                     con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");
                      
                     String sql = "update t_user set address=?,username=? where id=?";
                     sta = con.prepareStatement(sql);
                     sta.setString(1, user.getAddress());
                     sta.setString(2, user.getUsername());
                     sta.setInt(3, user.getId());
                      
                     rows = sta.executeUpdate();
                     if(rows > 0) {
                         System.out.println("ok");
                     }
                 } catch (ClassNotFoundException e) {
                     e.printStackTrace();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 } finally {
                     if(sta != null) {
                         try {
                             sta.close();
                         } catch (SQLException e) {
                             e.printStackTrace();
                         } finally {
                             if(con != null) {
                                 try {
                                     con.close();
                                 } catch (SQLException e) {
                                     e.printStackTrace();
                                 }
                             }
                         }
                     }
                 }
                 return rows;
             }
             
             /**
              * 3.对mysql的delete()操作
              */
             public int delete(int id) {
                 Connection con = null;
                 PreparedStatement sta = null;
                 int rows = 0;
                  
                 try {
                     Class.forName("com.mysql.jdbc.Driver");
                     con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");
                      
                     String sql = "delete from t_user where id=?";
                     sta = con.prepareStatement(sql);
                     sta.setInt(1, id);
                      
                     rows = sta.executeUpdate();
                     if(rows > 0) {
                         System.out.println("ok,ok,ok");
                     }
                      
                 } catch (ClassNotFoundException e) {
                     e.printStackTrace();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 } finally {
                     if(sta != null) {
                         try {
                             sta.close();
                         } catch (SQLException e) {
                             e.printStackTrace();
                         } finally {
                             if(con != null) {
                                 try {
                                     con.close();
                                 } catch (SQLException e) {
                                     e.printStackTrace();
                                 }
                             }
                         }
                     }
                 }
                 return rows;
             }
             
             
             /**
              * 4.retrieve()操作
              */
             public void retrieve(int id) {
                 Connection con = null;
                 PreparedStatement sta = null;
                 ResultSet rs = null;
                  
                 try {
                     Class.forName("com.mysql.jdbc.Driver");
                     con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");
                      
                     String sql = "select id,username,address from t_user where id=?";
                     sta = con.prepareStatement(sql);
                     sta.setInt(1, id);
                      
                     rs = sta.executeQuery();
                     if(rs.next()) {
                         int idd = rs.getInt("id");
                         String username = rs.getString("username");
                         String adrress = rs.getString("address");
                         System.out.println(idd+"        "+username+"        "+adrress);
                     }
                      
                 } catch (ClassNotFoundException e) {
                     e.printStackTrace();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 } finally {
                     if(rs != null) {
                         try {
                             rs.close();
                         } catch (SQLException e) {
                             e.printStackTrace();
                         } finally {
                             if(sta != null) {
                                 try {
                                     sta.close();
                                 } catch (SQLException e) {
                                     e.printStackTrace();
                                 } finally {
                                     if(con != null) {
                                         try {
                                             con.close();
                                         } catch (SQLException e) {
                                             e.printStackTrace();
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             
         }
          
         看dao中的内容,我们发现几个方法中的代码有很多重复的!!所以在下一篇文章中,我们要对代码进行优化!
        出处http://hanchaohan.blog.51cto.com/2996417/925573
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 13:23 , Processed in 0.297303 second(s), 36 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表