Simplify the use of JDBC by combining dbutils and c3p0 with simple implementation and deletion and modification functions

Source: Internet
Author: User

The role of Dbutils:

Dbutils is an open source component of Apache, and for JDBC, dbutils can simplify heap JDBC operations because dbutils encapsulates the operation of JDBC.


main classes included in Dbutils:

1.QueryRunner class: Mainly for the operation of the database.

2.ResultSetHandler Interface: How to encapsulate the result set after the return of the Select.

3.Dbutils class: For closing resources and transaction processing.


C3p0 's introduction:

1. c3p0 Open source free connection pool. The open source project that uses it has spring, hibernate, etc.

2.c3p0 need to add profile c3p0-config.xml and jar packages when used.


Note: When used, the name of the configuration file must be c3p0-config.xml.


c3p0-config.xml File Source:

  <?xml version= "1.0" encoding= "UTF-8"
   <c3p0-config>


  <default-config>
    <property name= "Driverclass" >com.mysql.jdbc.Driver</property>
<property name= "Jdbcurl" >JDBC:MYSQL:///WEB08</PROPERTY>
<property name= "user" >tiantian </property>
<property name= "password" >123456</PROPERTY>
<property name= " Initialpoolsize ">5</property>
<property name=" Maxpoolsize ">20</PROPERTY>
  </ Default-config>
  
  <named-config name= "Atlihao" >&NBSP;
    < Property Name= "Driverclass" >com.mysql.jdbc.driver</property>
<property name= "Jdbcurl" >JDBC: Mysql:///web08</property>
<property name= "user" >root</property>
<property name= " Password ">123456</property>
<property name=" Initialpoolsize ">5</PROPERTY>

<property name= "Maxpoolsize" >20</property>

</named-config> </c3p0-config>


the implementation source of the C3p0utils class (providing DataSource objects for the test class only):


public class C3p0utils {
Note: 1. To create an object using the parameterless constructor, select the data in the <default-config> tag in C3p0-config.xml
2. To create an object using the parameter constructor (for example: Combopooleddatasource ("Atlihao")), select <named-config name= "Atlihao" in C3p0-config.xml > The data in the label
private static Combopooleddatasource datasource=new Combopooleddatasource ();
private static Combopooleddatasource datasource=new Combopooleddatasource ("Atlihao");
public static DataSource Getdatasource () {
return dataSource;
}
public static Connection getconnection () {
try {
return Datasource.getconnection ();
catch (SQLException e) {
throw new RuntimeException (e);
}
}
}


Testdbutils class Source (test Dbutils tool Class):

/**
* Test Dbutils Tool Class
* @author Q ' q
*
*/
public class Testdbutils {
/**
* Test Delete User method
*/
@Test
public void Testdeleteuser () {
try {
1. Create Core class Queryrunner
Queryrunner qr=new Queryrunner (C3p0utils.getdatasource ());
2. Write SQL statements
String sql= "Delete from Tbl_user where uid=?";
3. Set values for placeholders
Object[] params= {9};
4. Perform the Add operation
int rows=qr.update (sql,params);
if (rows>0) {
System.out.println ("delete succeeded.") ");
}else {
System.out.println ("Delete failed.") ");
}
catch (SQLException e) {
throw new RuntimeException (e);
}
}


This is my first time to write their own blog, I hope you make a lot of suggestions, I will try to change their writing, to share what I learned, programming, need the most is patience, refueling.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.