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

秦时明月的个人网站怎么做台州cms模板建站

秦时明月的个人网站怎么做,台州cms模板建站,网站架构文案,网站开发培训是不是坑目录 封装方法 引出dbutils 案例 当关闭connection后#xff0c;resultset结果集就无法使用了#xff0c;这就使得resultset不利于数据的管理 封装方法 我们可以将结果集先存储在一个集合中#xff0c;当connection关闭后#xff0c;我们可以通过访问集合来访问结果集 …目录 封装方法 引出dbutils 案例 当关闭connection后resultset结果集就无法使用了这就使得resultset不利于数据的管理 封装方法 我们可以将结果集先存储在一个集合中当connection关闭后我们可以通过访问集合来访问结果集 public ArrayListActor testSelectToArrayList() {System.out.println(使用 druid方式完成);//1. 得到连接Connection connection null;//2. 组织一个sqlString sql select * from actor where id ?;PreparedStatement preparedStatement null;ResultSet set null;ArrayListActor list new ArrayList();//创建ArrayList对象,存放actor对象//3. 创建PreparedStatement 对象try {connection JDBCUtilsByDruid.getConnection();System.out.println(connection.getClass());//运行类型 com.alibaba.druid.pool.DruidPooledConnectionpreparedStatement connection.prepareStatement(sql);preparedStatement.setInt(1, 1);//给?号赋值//执行, 得到结果集set preparedStatement.executeQuery();//遍历该结果集while (set.next()) {int id set.getInt(id);String name set.getString(name);//getName()String sex set.getString(sex);//getSex()Date borndate set.getDate(borndate);String phone set.getString(phone);//把得到的resultset 的记录封装到 Actor对象放入到list集合list.add(new Actor(id, name, sex, borndate, phone));}System.out.println(list集合数据 list);for(Actor actor : list) {System.out.println(id actor.getId() \t actor.getName());}} catch (SQLException e) {e.printStackTrace();} finally {//关闭资源JDBCUtilsByDruid.close(set, preparedStatement, connection);}//因为ArrayList 和 connection 没有任何关联所以该集合可以复用.return list;}引出dbutils 是Apache组织提供的一个开源的jdbc工具库类对jdbc进行封装简化工作量 QueryRunner类封装了sql的执行是线程安全的可以实现增删改查 ResuleSetHandler接口该接口处理java.sql.ResultSet将数据按要求转化为另一种格式 案例 使用DBUtils 数据连接池德鲁伊方式完成对表的actor的crud 1.返回结果为多行 public void testQueryMany() throws SQLException { //返回结果是多行的情况//1. 得到 连接 (druid)Connection connection JDBCUtilsByDruid.getConnection();ListActor list queryRunner.query(connection, sql, new BeanListHandler(Actor.class), 1);System.out.println(输出集合的信息);for (Actor actor : list) {System.out.print(actor);}//释放资源JDBCUtilsByDruid.close(null, null, connection); } (1) query 方法就是执行sql 语句得到resultset ---封装到 -- ArrayList 集合中         (2) 返回集合         (3) connection: 连接         /4) sql : 执行的sql语句         (5) new BeanListHandler(Actor.class): 在将resultset - Actor 对象 - 封装到 ArrayList            底层使用反射机制 去获取Actor 类的属性然后进行封装         (6) 1 就是给 sql 语句中的? 赋值可以有多个值因为是可变参数Object... params         (7) 底层得到的resultset ,会在query 关闭, 关闭PreparedStatment 2.返回结果为单行 public void testQuerySingle() throws SQLException {//1. 得到 连接 (druid)Connection connection JDBCUtilsByDruid.getConnection();//2. 使用 DBUtils 类和接口 , 先引入DBUtils 相关的jar , 加入到本Project//3. 创建 QueryRunnerQueryRunner queryRunner new QueryRunner();//4. 就可以执行相关的方法返回单个对象String sql select * from actor where id ?;// 因为我们返回的单行记录---单个对象 , 使用的Hander 是 BeanHandlerActor actor queryRunner.query(connection, sql, new BeanHandler(Actor.class), 1);System.out.println(actor);// 释放资源JDBCUtilsByDruid.close(null, null, connection);}3.返回单行单列 public void testScalar() throws SQLException {Connection connection JDBCUtilsByDruid.getConnection();QueryRunner queryRunner new QueryRunner();String sql select name from actor where id ?;//因为返回的是一个对象, 使用的handler 就是 ScalarHandlerObject obj queryRunner.query(connection, sql, new ScalarHandler(), 4);System.out.println(obj);// 释放资源JDBCUtilsByDruid.close(null, null, connection);}4.dml操作 public void testDML() throws SQLException {Connection connection JDBCUtilsByDruid.getConnection();QueryRunner queryRunner new QueryRunner();//这里组织sql 完成 update, insert delete//String sql update actor set name ? where id ?;//String sql insert into actor values(null, ?, ?, ?, ?);String sql delete from actor where id ?;//(1) 执行dml 操作是 queryRunner.update()//(2) 返回的值是受影响的行数 (affected: 受影响)int affectedRow queryRunner.update(connection, sql, 1000 );System.out.println(affectedRow 0 ? 执行成功 : 执行没有影响到表);// 释放资源JDBCUtilsByDruid.close(null, null, connection);}
http://www.hkea.cn/news/14539301/

相关文章:

  • 网站开发工资多少钱抄袭别人网站的前端代码合法吗
  • 做书封面的网站建设实木餐桌椅移动网站
  • 各大网站的软文怎么做分销系统什么意思
  • 浙江网站建站网络网站是多少钱一年
  • asp 大型网站开发上海自动化仪表厂
  • 在线做编程题的网站wordpress二维码制作教程
  • 建设网页的公司aso优化
  • .jsp网站开发技术一分钟赚50元的游戏
  • 江山网站建设百度企业查
  • 做石材外贸用什么网站凡科建站有哪些弊端
  • 如何自己建站网站制作wordpress漏洞总结
  • 做外贸哪个网站比较好关于设计网站
  • 网站建设职能绩效目标如何自己建设网站
  • 娄底seo排名win10优化软件哪个好
  • python做问卷调查的网站wordpress自带图片
  • 做网站找哪个好网站导航栏怎么做简单
  • 西北电力建设甘肃工程公司网站网站没有做的关键词有排名
  • 专门做ppt会员网站ainihejian wordpress
  • wordpress4.9 多站点怎样做网站底部导航
  • 做网站可以不写代码wordpress密码解密
  • 网站建设公司首选华网天下有没有好的网站
  • 香山红叶建设有限公司网站想要个免费网站
  • 成都网站建设制作需求不明确的软件开发模型
  • 平板电脑 做网站开发简单网页素材
  • 构建自己的网站大连网络推广机构
  • 阿里云如何做网站网站布局英文
  • 中国住房和城乡建设厅网站外贸网站建设科技
  • 影视自助建站系统源码海口智能建站模板
  • 天津手机模板建站手机网站设计宽度
  • 美橙建站之星怎么样郑州网站建设及托管