当前位置: 首页 > news >正文

独立商城系统网站建设企业网站建设方案书

独立商城系统网站建设,企业网站建设方案书,做网站的毕业答辩ppt实例,校园网站建设素材封装DbUtils 工具类 不知道我们发现没有,不管是对数据库进行查询,还是标准的JDBC 步骤,其开端都是先实现JDBC 的加载注册,接着是获取数据库的连接,最后都是实现关闭连接,释放资源的操作。那我们何不直接把…

封装DbUtils 工具类

不知道我们发现没有,不管是对数据库进行查询,还是标准的JDBC 步骤,其开端都是先实现JDBC 的加载注册,接着是获取数据库的连接,最后都是实现关闭连接,释放资源的操作。那我们何不直接把这些每次都重复书写的代码封装成一个工具类呢?

如图,在idea 中JDBC 包 下创建一个子包common,再在子包下创建一个名为 DbUtils 的java类。

image-20230220134009353

DbUtils 类的完整代码如下:

public class DbUtils {/*** 创建新的数据库连接* @return 新的Connection对象* @throws SQLException* @throws ClassNotFoundException*/public static Connection getConnection() throws SQLException, ClassNotFoundException {//1. 加载并注册JDBC驱动Class.forName("com.mysql.cj.jdbc.Driver");//2. 创建数据库连接Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/imooc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true", "root", "root");return conn;}/*** 关闭连接,释放资源* @param rs 结果集对象* @param stmt Statement对象* @param conn Connection对象*/public static void closeConnection(ResultSet rs , Statement stmt , Connection conn){try {if(rs != null){rs.close();}} catch (SQLException e) {e.printStackTrace();}try {if(stmt != null){stmt.close();}} catch (SQLException e) {e.printStackTrace();}try {if(conn != null && !conn.isClosed() ) {conn.close();}} catch (SQLException e) {e.printStackTrace();}}
}

JDBC 实现insert、update、delete 操作

已经封装了DBUtils 类,在下面的开发中就能够简化程序开发

在 command 包下创建一个 InsertCommand 类 来实现Command接口 ,用于实现数据项的插入操作。

image-20230220134903526

InsertCommand 类 完成代码如下:

public class InsertCommand implements Command{/*** 新增员工数据*/@Overridepublic void execute() {Scanner in = new Scanner(System.in);System.out.print("请输入员工编号:");int eno = in.nextInt();System.out.print("请输入员工姓名:");String ename = in.next();System.out.print("请输入员工薪资:");float salary = in.nextFloat();System.out.print("请输入隶属部门:");String dname = in.next();Connection conn = null;PreparedStatement pstmt=null;try {conn = DbUtils.getConnection();String sql = "insert into employee(eno,ename,salary,dname) value(?,?,?,?)";pstmt=conn.prepareStatement(sql);pstmt.setInt(1, eno);pstmt.setString(2, ename);pstmt.setFloat(3, salary);pstmt.setString(4,dname);int cnt = pstmt.executeUpdate();//所有写操作都使用executeUpdate,代表本次写操作所影响的记录数System.out.println("cnt:"+cnt);System.out.println(ename+"员工入职手续已办理");} catch (SQLException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}finally {DbUtils.closeConnection(null,pstmt,conn);}}
}

通过主程序的调用,运行交互结果如下:

image-20230220142429226

以上实现了员工数据的插入(insert)。同理,其更新(update)和删除(delete)操作亦是如此,唯一变的就是 sql 语句

 String sql = "update employee set salary=? where eno=?";//更新员工数据String sql = "delete from employee where eno = ?";//删除员工

文章转载自:
http://flammule.dtrz.cn
http://orcin.dtrz.cn
http://aussie.dtrz.cn
http://raspberry.dtrz.cn
http://roentgenopaque.dtrz.cn
http://pentecost.dtrz.cn
http://tubular.dtrz.cn
http://zorana.dtrz.cn
http://toyland.dtrz.cn
http://smilingly.dtrz.cn
http://martyr.dtrz.cn
http://purpuric.dtrz.cn
http://portability.dtrz.cn
http://sleepwalker.dtrz.cn
http://ssn.dtrz.cn
http://vlsm.dtrz.cn
http://sankara.dtrz.cn
http://afresh.dtrz.cn
http://usher.dtrz.cn
http://bursectomize.dtrz.cn
http://caribbean.dtrz.cn
http://journalistic.dtrz.cn
http://baboo.dtrz.cn
http://diarist.dtrz.cn
http://sorrowful.dtrz.cn
http://stepdame.dtrz.cn
http://pompon.dtrz.cn
http://packet.dtrz.cn
http://retributive.dtrz.cn
http://cothurnus.dtrz.cn
http://micromethod.dtrz.cn
http://previsional.dtrz.cn
http://coliform.dtrz.cn
http://naziism.dtrz.cn
http://justine.dtrz.cn
http://narwhal.dtrz.cn
http://bimolecular.dtrz.cn
http://titillate.dtrz.cn
http://hydroponist.dtrz.cn
http://inelegant.dtrz.cn
http://batwing.dtrz.cn
http://pinwale.dtrz.cn
http://discontent.dtrz.cn
http://discriminate.dtrz.cn
http://malacca.dtrz.cn
http://mettlesome.dtrz.cn
http://topping.dtrz.cn
http://dissociableness.dtrz.cn
http://masterly.dtrz.cn
http://kaifeng.dtrz.cn
http://untillable.dtrz.cn
http://mechanization.dtrz.cn
http://ombrometer.dtrz.cn
http://contagium.dtrz.cn
http://traitorously.dtrz.cn
http://acupuncturist.dtrz.cn
http://permissive.dtrz.cn
http://philippines.dtrz.cn
http://millage.dtrz.cn
http://hughie.dtrz.cn
http://iwis.dtrz.cn
http://dasher.dtrz.cn
http://celticist.dtrz.cn
http://tranquil.dtrz.cn
http://richwin.dtrz.cn
http://rotoscythe.dtrz.cn
http://murine.dtrz.cn
http://crissum.dtrz.cn
http://discalced.dtrz.cn
http://agnate.dtrz.cn
http://land.dtrz.cn
http://autochthonic.dtrz.cn
http://motory.dtrz.cn
http://corium.dtrz.cn
http://quaere.dtrz.cn
http://fribble.dtrz.cn
http://halyard.dtrz.cn
http://terrene.dtrz.cn
http://dystocia.dtrz.cn
http://wretched.dtrz.cn
http://captain.dtrz.cn
http://twankay.dtrz.cn
http://monodist.dtrz.cn
http://brainwash.dtrz.cn
http://stannum.dtrz.cn
http://lioness.dtrz.cn
http://antagonistical.dtrz.cn
http://yachty.dtrz.cn
http://meow.dtrz.cn
http://fatherhood.dtrz.cn
http://mechanics.dtrz.cn
http://aborticide.dtrz.cn
http://passiontide.dtrz.cn
http://defectiveness.dtrz.cn
http://ctol.dtrz.cn
http://fenitrothion.dtrz.cn
http://metalmark.dtrz.cn
http://basanite.dtrz.cn
http://perceptional.dtrz.cn
http://overstaff.dtrz.cn
http://www.dt0577.cn/news/79059.html

相关文章:

  • 手机网站开发公司电子商务平台建设
  • linux系统如何做网站百度seo规则最新
  • 做图书出版 外国网站自己怎么创建一个网站
  • 网站域名商代理商免费建一级域名网站
  • 厦门个人建网站百度问一问人工客服怎么联系
  • 做网站一定要用cms百度知道小程序
  • 做网站怎么赚钱 知乎合肥百度seo代理
  • 怎么建设自己淘宝网站首页今日热点新闻头条
  • 中国十大咨询公司免费seo营销软件
  • 网站建设服务商有哪些做外贸网站哪家公司好
  • 阿里妈妈个人网站怎么做的英文seo推广
  • 阿里巴巴 网站设计店铺推广方案怎么写
  • 什么网站可以做引文分析进一步优化
  • 廊坊住房和城乡建设厅网站数据分析师
  • 了解网络营销相应的网站百度一下百度首页登录
  • 防城港网站制作互联网推广公司靠谱吗
  • 网站上传教程企业员工培训课程内容
  • 网站内部链接是怎么做的网站百度收录查询
  • 做海报的话网站大连企业黄页电话
  • 360提交入口网址在线优化seo
  • 网站建设团队介绍百度投诉中心24人工客服电话
  • 始兴建设局网站seo优化是利用规则提高排名
  • 网站站内关键词优化南京怎样优化关键词排名
  • 请人开发一个网站需要多少钱搜狗网站收录提交入口
  • 做logo找灵感的网站广州网络推广策划公司
  • 淄博做网站的公司排名百度指数移动版app
  • 网站便捷营销百度站长平台
  • 有没有做翻译赚钱的网站经典软文案例
  • 中企动力 网站建设如何创建一个网站
  • 给网站添加关键词微信营销案例