idea通过c3p0和QueryRunner对mysql进行增删改查3

link
1、添加jar包
2、编写配置文件c3p0-config.xml,
放在classpath中,或classes目录中(XX项目/src/c3p0-config.xml)

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
  <default-config>
 <property name="driverClass">com.mysql.jdbc.Driver</property>
 <property name="jdbcUrl">jdbc:mysql://localhost:3306/day14</property>
 <property name="user">root</property>
 <property name="password">00000000</property>
        <property name="initialPoolSize">10</property>
        <property name="maxIdleTime">30</property>
        <property name="maxPoolSize">100</property>
        <property name="minPoolSize">10</property>
 </default-config>
</c3p0-config>

3、编写工具类:

public class C3P0Util {
 private static DataSource dataSource = new ComboPooledDataSource();
 public static DataSource getDataSource() {
 return dataSource;
 }
 //获取连接
 public static Connection getConnection(){
 try {
 return dataSource.getConnection();
 } catch (SQLException e) {
 throw new RuntimeException("服务器繁忙");
 }
 }
 public static void release(Connection conn,Statement stmt,ResultSet rs){

 //关闭连接

 if(rs!=null){
 try {
 rs.close();
 } catch (Exception e) {
 e.printStackTrace();
 }
 rs = null;
 }
 if(stmt!=null){
 try {
 stmt.close();
 } catch (Exception e) {
 e.printStackTrace();
 }
 stmt = null;
 }
 if(conn!=null){
 try {
 conn.close();
 } catch (Exception e) {
 e.printStackTrace();
 }
 conn = null;
 }
 } 

}

4、编写sql语句操作数据库:

public class BookDaoImpl implements BookDao{
 public List<Book> findAllBooks() throws SQLException{
 	QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());	
	return qr.query("select * from book", new BeanListHandler<Book>(Book.class));
}
	
 public void addBook(Book book) throws SQLException{
 	QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
 	qr.update("insert into book values(?, ?, ?, ?, ?, ?)", 
 				book.getId(),book.getName(), book.getPrice(),   
				book.getPnum(), book.getCategory(), book.getDescription());
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用QueryRunnerc3p0进行增删改查操作的示例代码: ```java import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3p0Demo { private static ComboPooledDataSource dataSource; static { dataSource = new ComboPooledDataSource(); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test"); dataSource.setUser("root"); dataSource.setPassword("123456"); dataSource.setDriverClass("com.mysql.jdbc.Driver"); } public static void main(String[] args) throws SQLException { QueryRunner queryRunner = new QueryRunner(dataSource); // 查询 List<User> userList = queryRunner.query("SELECT * FROM user", new BeanListHandler<User>(User.class)); System.out.println(userList); // 新增 queryRunner.update("INSERT INTO user (name, age) VALUES (?, ?)", "张三", 20); // 修改 queryRunner.update("UPDATE user SET age = ? WHERE name = ?", 21, "张三"); // 删除 queryRunner.update("DELETE FROM user WHERE name = ?", "张三"); } public static class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } } } ``` 这里使用了c3p0的ComboPooledDataSource来获取数据库连接,并使用QueryRunner进行数据库操作。以上代码中的User仅作为示例,你可以根据自己的需求修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值