DButils的更新与查询,利用C3P0链接数据库
生活随笔
收集整理的這篇文章主要介紹了
DButils的更新与查询,利用C3P0链接数据库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先導入DBUtils的工具包:commons-dbutils-1.6.jar
再導入C3P0的工具包:c3p0-0.9.1.2.jar
首先創建C3P0工具類:
package star.july.util;import java.sql.Connection;import java.sql.SQLException;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;public class C3P0Util {private static ComboPooledDataSource ds = new ComboPooledDataSource();public static Connection getConnection(){try{Connection conn = ds.getConnection();return conn;}catch(SQLException e){e.printStackTrace();throw new RuntimeException();}}public static DataSource getDataSource(){return ds;}}
配置C3P0的zml文件,文件名一定要是:c3p0-config.xml
<c3p0-config><!-- 默認配置 --><default-config><!-- 屬性名稱就是setter方法名稱 --><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/day19</property><property name="user">root</property><property name="password">root</property><property name="initialPoolSize">5</property><property name="maxPoolSize">12</property><property name="checkoutTimeout">3000</property></default-config><!-- 命名配置 --><named-config name="day17"><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/day17</property><property name="user">root</property><property name="password">root</property><property name="initialPoolSize">5</property><property name="maxPoolSize">15</property><property name="checkoutTimeout">3000</property></named-config></c3p0-config>
再創建學生類
package star.july.dbutil;public class Student {private int id;private String name;private String gender;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", gender=" + gender+ "]";}}
演示dbutils工具的更新操作:
package star.july.dbutil;import java.sql.Connection; import java.sql.SQLException;import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.junit.Test;import star.july.util.C3P0Util;//演示dbutils工具的更新操作 public class Demo1 {//創建主程序QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());QueryRunner qr2 = new QueryRunner(); //沒有DataSource//沒有事務操作@Testpublic void test1() throws Exception{ // qr.update("insert into student(name,gender) values(?,?)", "狗蛋","女");qr.update("delete from student where id = ?",4); // System.out.println("影響了"+count+"行");}//有事務操作@Testpublic void test2(){Connection conn = C3P0Util.getConnection();try{//獲取Connection對象//1.開啟事務conn.setAutoCommit(false);qr2.update(conn,"delete from student where id=?",2);//int i =100/0;qr2.update(conn,"delete from student where id=?",3);//提交事務conn.commit();}catch(Exception e){e.printStackTrace();//回滾事務try {conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}}} }
查詢類操作
package star.july.dbutil;import java.util.List;import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.ArrayHandler; import org.apache.commons.dbutils.handlers.ArrayListHandler; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import org.junit.Test;import star.july.util.C3P0Util;//查詢類操作 public class Demo2 {QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());//查詢一條數據:BeanHandler,把結果集封裝一個javabean對象//約定:表的字段名稱和javabean的屬性名稱保持一致@Testpublic void test1() throws Exception{Student stu = qr.query("select * from student where id =?" , new BeanHandler(Student.class),1);System.out.println(stu);}//查詢多條數據:BeanListHandler:把結果集封裝一個List集合(包含多個javabean對象)//約定:表的字段名稱和javabean的屬性名稱保持一致!@Testpublic void test2() throws Exception{List<Student> list = qr.query("select* from student",new BeanListHandler(Student.class));for(Student stu:list){System.out.println(stu);}}//查詢一條數據:ArrayHandler:把結果集封裝一個對象數組@Testpublic void test3()throws Exception{Object[] array = qr.query("select * from student where id = ?", new ArrayHandler(),5);for(int i = 0 ; i <array.length;i++){System.out.print(array[i]);}}//查詢多條數據:ArrayListHandler:把結果集封裝一個List(包含多個對象數組)@Testpublic void test4() throws Exception{List<Object[]> list = qr.query("select * from student", new ArrayListHandler());for(Object[] obj:list){for(Object stu: obj){System.out.print(stu+"\t");}System.out.println();}}//查詢一條記錄(且只有一個字段):聚合查詢 count(*) max() min() avg(): ScalarHandler:封裝一個對象@Testpublic void test5() throws Exception{Long count = qr.query("select count(*) from student", new ScalarHandler());System.out.println(count);} }總結
以上是生活随笔為你收集整理的DButils的更新与查询,利用C3P0链接数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: D3P0实践小例子
- 下一篇: Java的数组集合概括