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

黄石市网站建设_网站建设公司_网站建设_seo优化

杭州电商网站平台开发公司,贵州三线建设博物馆网站,网站结构化数据,云匠网描述当我们需要在Java程序中与数据库进行交互#xff0c;可能首先想到的是使用某个ORM框架#xff0c;因为ORM框架封装了一些实现细节#xff0c;在使用上非常方便#xff0c;并且一定程度上可以提升代码稳定性。在ORM框架中#xff0c;都会依赖MySQL Connector包#xf…描述当我们需要在Java程序中与数据库进行交互可能首先想到的是使用某个ORM框架因为ORM框架封装了一些实现细节在使用上非常方便并且一定程度上可以提升代码稳定性。在ORM框架中都会依赖MySQL Connector包因为真正与数据库进行交互的是在MySQL Connector包里面实现。5.x版本maven依赖mysqlmysql-connector-java5.1.47三种方式MySQL Connector执行SQL语句主要有executeQueryexecuteUpdateexecute等三种方式。executeQuery此方法执行Selec查询语句通过ResultSet返回结果集。private static void executeQuery() throws Exception{Connection connection null;Statement statement null;ResultSet rs null;try {connection DriverManager.getConnection(url);statement connection.createStatement();statement.setFetchSize(Integer.MIN_VALUE);String sql select * from user;rs statement.executeQuery(sql);int count0;while (rs.next()){count;}System.out.println(executeQuery count: count);} catch (Exception e){e.printStackTrace();} finally {close(connection,statement,rs);}}executeUpdate此方法执行InsertUpdateDelete语句返回变更影响的行数。private static void executeUpdate() throws Exception{Connection connection null;Statement statement null;int updateCount 0;try {connection DriverManager.getConnection(url);statement connection.createStatement();String sql update user set name啊啊啊 where id new Random().nextInt(999999);updateCount statement.executeUpdate(sql);System.out.println(executeUpdate count: updateCount);} catch (Exception e){e.printStackTrace();} finally {close(connection,statement,null);}}execute当我们不知道来源SQL是Select查询还是Insert/Update/Delete更新时可以统一使用excute()方法来执行SQL语句此方法返回一个boolean值如果返回true表示执行的SQL语句为Select查询语句此时可以通过Statement#getResultSet()方法来获取结果集如果返回false表示执行的时Insert/Update/Delete语句此时可以通过Statement#getUpdateCount()来返回此次SQL执行对数据库影响的行数。private static void executeForSelect() throws Exception{Connection connection null;Statement statement null;ResultSet rs null;try {connection DriverManager.getConnection(url);statement connection.createStatement();statement.setFetchSize(Integer.MIN_VALUE);String sql select * from user;if (statement.execute(sql)){rs statement.getResultSet();}int count0;while (rs.next()){count;}System.out.println(executeForSelect count: count);} catch (Exception e){e.printStackTrace();} finally {close(connection,statement,rs);}}private static void executeForUpdate() throws Exception{Connection connection null;Statement statement null;int updateCount 0;try {connection DriverManager.getConnection(url);statement connection.createStatement();String sql update user set name啊啊啊 where id new Random().nextInt(999999);if (!statement.execute(sql)){updateCount statement.getUpdateCount();}System.out.println(executeForUpdate updateCount: updateCount);} catch (Exception e){e.printStackTrace();} finally {close(connection,statement,null);}}验证执行测试public static void main(String[] args) throws Exception {Long start System.currentTimeMillis();executeForSelect();System.out.println(executeForSelect 耗时 (System.currentTimeMillis() - start) ms \n);start System.currentTimeMillis();executeForUpdate();System.out.println(executeForUpdate 耗时 (System.currentTimeMillis() - start) ms \n);start System.currentTimeMillis();executeQuery();System.out.println(executeQuery 耗时 (System.currentTimeMillis() - start) ms \n);start System.currentTimeMillis();executeUpdate();System.out.println(executeUpdate 耗时 (System.currentTimeMillis() - start) ms \n);}private static void close(Connection connection, Statement statement, ResultSet rs) throws Exception{if (rs ! null){rs.close();}if (statement ! null){statement.close();}if (connection ! null){connection.close();}}返回结果executeForSelect count: 4717924executeForSelect 耗时 4791 msexecuteForUpdate updateCount: 1executeForUpdate 耗时 6 msexecuteQuery count: 4717924executeQuery 耗时 4340 msexecuteUpdate count: 1executeUpdate 耗时 8 ms
http://www.ihoyoo.com/news/89537.html

相关文章:

  • 网站备案 类型无锡模板建站源码
  • wordpress discuz seowordpress tag页面优化
  • 能发朋友圈的网站建设广告语百度极速版app下载安装
  • 肖鸿昌建筑网站怎么打击对手网站排名
  • 网站内链案例重庆网站建设报价
  • 网站 空间 服务器 免费平面设计跟网站建设
  • 杭州网站外包局域网网站架设
  • 网站建设文档发稿计划
  • 怎样建设一个内部网站龙岗坪地网站建设公司
  • 肇庆城乡建设门户网站手机上怎样制作网站
  • 上海网站建设服湘潭自助建站系统
  • 苏州网站建设信息网络免费手机优化大师下载安装
  • 平面设计师个人网站十堰seo优化分析
  • wordpress xml-rpc长春网站快照优化公司
  • wordpress旅游网站大连住建部官方网站
  • 网站开发制作步骤图建设香帅摩托车官网
  • 做妇产科网站湖南专业关键词优化
  • 青海省建设局网站首页在线表白网页制作
  • 网站html代码珠海网站公司
  • 个人网站 云服务器wordpress上传文章
  • php做网站浏览量成都住建厅官方网站
  • 餐饮店怎么做营销和推广搜索网站排名优化策略
  • 网站建设体会doc网站建设课程设计报告范文
  • 网站排名忽然不见了wordpress登陆后
  • 做网站需要公司资质吗网站建设是什么意思 打不开
  • php商业网站制作域名虚拟服务器做网站
  • 做网站被用作非法用途网站负责人
  • 网站制作成本包含南京做企业网站
  • 休闲吧网站建设宁波网站建设哪个公司好
  • 简单好看个人主页网站模板海拉尔网站设计