69期-Java SE-040_JDBC-4
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了69期-Java SE-040_JDBC-4,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含11187字,纯文字阅读大概需要16分钟。
内容图文
![69期-Java SE-040_JDBC-4](/upload/InfoBanner/zyjiaocheng/510/78c736e189eb45f18ac572546bfbc317.jpg)
c3p0-config.xml
<c3p0-config> <named-config name="testc3p0"> <property name="user">root</property> <property name="password">root</property> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8</property> <property name="initialPoolSize">20</property> <property name="acquireIncrement">5</property> <property name="maxPoolSize">40</property> <property name="minPoolSize">2</property> </named-config> </c3p0-config>
People.java
package com.southwind.entity; public class People { private Integer id; private String name; private Integer money; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getMoney() { return money; } public void setMoney(Integer money) { this.money = money; } @Override public String toString() { return "People{" + "id=" + id + ", name=‘" + name + ‘\‘‘ + ", money=" + money + ‘}‘; } public People(Integer id, String name, Integer money) { this.id = id; this.name = name; this.money = money; } public People() { } }
Test.java
package com.southwind.test; import com.mchange.v2.c3p0.ComboPooledDataSource; import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.SQLException; public class Test { public static void main(String[] args) { ComboPooledDataSource dataSource = new ComboPooledDataSource(); try { dataSource.setDriverClass("com.mysql.cj.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"); dataSource.setUser("root"); dataSource.setPassword("root"); dataSource.setInitialPoolSize(20); dataSource.setAcquireIncrement(5); dataSource.setMaxPoolSize(40); dataSource.setMinPoolSize(2); Connection connection = dataSource.getConnection(); System.out.println(connection); } catch (PropertyVetoException e) { e.printStackTrace(); } catch (SQLException e){ e.printStackTrace(); } } }
Test2.java
package com.southwind.test; import com.mchange.v2.c3p0.ComboPooledDataSource; import com.southwind.entity.People; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Test2 { public static void main(String[] args) { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { ComboPooledDataSource dataSource = new ComboPooledDataSource("testc3p0"); connection = dataSource.getConnection(); String sql = "select * from people where id = ?"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1,1); resultSet = preparedStatement.executeQuery(); if(resultSet.next()){ int id = resultSet.getInt(1); String name = resultSet.getString(2); int money = resultSet.getInt(3); People people = new People(id,name,money); System.out.println(people); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if(connection!=null){ connection.close(); } if(preparedStatement!=null){ preparedStatement.close(); } if(resultSet!=null){ resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }
Test3.java
package com.southwind.test; import com.mchange.v2.c3p0.ComboPooledDataSource; import com.southwind.entity.People; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.MapHandler; import org.apache.commons.dbutils.handlers.MapListHandler; import java.sql.Connection; import java.sql.SQLException; import java.util.List; import java.util.Map; public class Test3 { public static void main(String[] args) { Connection connection = null; try { ComboPooledDataSource dataSource = new ComboPooledDataSource("testc3p0"); connection = dataSource.getConnection(); String sql = "select * from people"; QueryRunner queryRunner = new QueryRunner(); // List<People> list = queryRunner.query(connection,sql,new BeanListHandler<>(People.class),1); // System.out.println(list); // People people = queryRunner.query(connection,sql, new BeanHandler<>(People.class),1); // System.out.println(people); // String sql = "select * from people"; // People people = queryRunner.query(connection,sql,new BeanHandler<>(People.class)); // System.out.println(people); // List<People> list = queryRunner.query(connection,sql,new BeanListHandler<>(People.class)); // System.out.println(list); // String sql = "select * from people where id = ?"; // Map<String,Object> map = queryRunner.query(connection,sql,new MapHandler()); // System.out.println(map.get("id")); // System.out.println(map.get("name")); // System.out.println(map.get("money")); List<Map<String,Object>> list = queryRunner.query(connection,sql,new MapListHandler()); for(Map<String,Object> map:list){ System.out.println(map.get("id")+"-"+map.get("name")+"-"+map.get("money")); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if(connection != null){ connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }
Test4.java
package com.southwind.test; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.apache.commons.dbutils.QueryRunner; import java.sql.Connection; import java.sql.SQLException; public class Test4 { public static void main(String[] args) { try { ComboPooledDataSource dataSource = new ComboPooledDataSource("testc3p0"); Connection connection = dataSource.getConnection(); // String sql = "insert into people(name,money) values(?,?)"; // QueryRunner queryRunner = new QueryRunner(); // int row = queryRunner.update(connection,sql,"小明",600); // System.out.println(row); // String sql = "update people set name = ?,money = ? where id = ?"; // QueryRunner queryRunner = new QueryRunner(); // int row = queryRunner.update(connection,sql,"小红",800,4); // System.out.println(row); String sql = "delete from people where id = ?"; QueryRunner queryRunner = new QueryRunner(); int row = queryRunner.update(connection,sql,4); System.out.println(row); } catch (SQLException e) { e.printStackTrace(); } } }
69期-Java SE-040_JDBC-4
标签:dstat mon insert img let except apache handlers final
本文系统来源:https://www.cnblogs.com/HiJackykun/p/11182587.html
内容总结
以上是互联网集市为您收集整理的69期-Java SE-040_JDBC-4全部内容,希望文章能够帮你解决69期-Java SE-040_JDBC-4所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。