前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jdbc学习总结3------javab

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

作者头像
py3study
发布2020-01-07 15:03:07
3340
发布2020-01-07 15:03:07
举报
文章被收录于专栏:python3

 1.测试类的内容:

在包:com.hanchao.test中

代码语言:javascript
复制
package com.hanchao.test;  import com.hanchao.dao.UserDao; import com.hanchao.entity.User;  /**  * 测试jdbc的类(javaBean+DAO)  * @author hanlw  * 2012-07-09  */ public class Test {      /**      * 1.什么是JavaBean?      *   它要满足两个条件:<1>.私有化属性;<2>.公开访问方法(setter and getter)      *         * 2.javabean又名:entity实体类、pojo、vo      *       * 3.DAO = data access object (封装了对一个表的所有CRUD操作!!)      *  DAO中方法的本质是将对象(entity)类和数据库进行交互。      */      public static void main(String[] args) {                  /**          * 1.insert()操作测试          */ /*      UserDao userDao = new UserDao();         User user = new User();                  user.setUsername("chenchen");         user.setAddress("jiangsu");         userDao.insert(user); */              /**          * 2.update()操作          */ /*      UserDao userDao = new UserDao();         User user = new User();                  user.setId(20);         user.setAddress("yancheng1");         user.setUsername("iloveyou");         userDao.update(user); */                  /**          * delete()操作          */ //      UserDao userDao = new UserDao(); //      userDao.delete(21);                  /**          * retrieve()操作          */         UserDao userDao = new UserDao();         userDao.retrieve(22);              }      } 

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

代码语言:javascript
复制
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

代码语言:javascript
复制
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释 
* 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+"\t"+username+"\t"+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中的内容,我们发现几个方法中的代码有很多重复的!!所以在下一篇文章中,我们要对代码进行优化!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档