JDBC-Extracted tool classes
Package utils;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.util.Properties;
Import com.mysql.jdbc.Statement;
public class Jdbcutils {
private static final String Driverclass;
private static final String URL;
private static final String USERNAME;
private static final String PASSWORD;
static{
Properties P = new properties ();
try {
P.load (New FileInputStream ("Src/jdbc.properties"));
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
Driverclass = P.getproperty ("Driverclass");
url = p.getproperty ("url");
USERNAME = P.getproperty ("USERNAME");
PASSWORD = P.getproperty ("PASSWORD");
}
/**
Link pool and JDBC federated extracted tool classes
Package utils;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.util.Properties;
Import Javax.sql.DataSource;
Import Org.apache.commons.dbcp.BasicDataSourceFactory;
Import Com.mchange.v2.c3p0.ComboPooledDataSource;
Import com.mysql.jdbc.Statement;
public class Jdbcutils {
/** * Get connection, return connection is Apache * @return */public static Connection getdbcpconnection () {Connection Connection = null; try {Properties Pro = new Properties (); Pro.load (New FileInputStream ("Src\\dbcpconfig.properties")); DataSource ds = Basicdatasourcefactory.createdatasource (pro); Connection = Ds.getconnection ();//Enhanced Connetion} catch (Exception e) {e.printstacktrace (); } return connection;} /** * Get connection, return connection not Apache * configuration file name is fixed c3p0-config.xml and placed in SRC directory * Combopooleddatasource will automatically go to the SRC directory to find the C3p0-config.xml configuration file * @return */public static Connection getc3p0connection () { Connection Connection = null; try {DataSource ds =new Combopooleddatasource ();//The parameter of this place is matching the configuration file, if not written, the default connection = Ds.getconnection () ;//Enhanced Connetion} catch (Exception e) {e.printstacktrace (); } return connection;} /** * Release Resources * @param stmt * @param conn enhanced */public static void release (Statement stmt,connection conn) {if (stmt! = Null) {try {stmt.close (); } catch (SQLException e) {e.printstacktrace (); }} if (conn! = null) {try {//has become returned ... conn.close (); } catch (SQLException e) {e.printstacktrace (); }}}/** * Frees resources * @param stmt * @param conn enhanced */public static void release (ResultSet rs,statement stmt,connection con N) {if (rs! = null) {try {rs.close (); } catch (SQLException e) {e.printstacktrace (); }} if (stmt! = null) {try {stmt.close (); } catch (SQLException e) {e.printstacktrace (); }} if (conn! = null) {try {//has modified the close (), it was destroyed, and now the method becomes the return connection. Conn.close (); } catch (SQLException e) {e.printstacktrace (); } }}
}
Dbutis using the Import package
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import Java.util.Date;
Import Javax.sql.DataSource;
Import Org.apache.commons.dbutils.QueryRunner;
Import Org.apache.commons.dbutils.ResultSetHandler;
Import Org.junit.Test;
Import Com.mchange.v2.c3p0.ComboPooledDataSource;
/**
- Because: Before Dbutis is used
- (1) Database link object, SQL statement action object, encapsulate result set object, these three large objects will be defined repeatedly
(2) Code duplication of encapsulated data, complex operation and large code size
(3) Code duplication for releasing resources
Results: (1) When the programmer is developing, there is a lot of repetitive work. (2) long cycle of development, low efficiency
So Apache developed a set of tool classes that encapsulate JDBC that Dbutis provides programmers with
- @author Wangshuang
*/
public class Test {
DataSource ds = new Combopooleddatasource ();br/> @Test
/**
-
- Create the Queryrunner class first
-
- Call the update () method to complete the redaction operation
/
Queryrunner qr = new Queryrunner (DS);
try {
Add to
Qr.update ("INSERT into product values (null,?,?,?)", "pear", 3.5,new Date ());
} catch (SQLException e) {br/>e.printstacktrace ();
}
}
@Test
Queryrunner qr = new Queryrunner ();
try {
Qr.query ("Select from product where pid =?", new Myresultsethandler (), 1);
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
/**
- Similar Beanhandler
- @author Wangshuang
*/
Class Myresultsethandler implements Resultsethandler<product>{br/> @Override
Product P = new product ();
P.setpid (Arg0.getint (1));
P.setpname (arg0.getstring (2));
P.setprice (Arg0.getdouble (3));
P.setpdate (Arg0.getdate (4));
return p;
}
}
The basic way of the Java Operations database in 3