commons-dbutils-tutorial
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
# commons-dbutils-tutorial
|
||||
|
||||
* BeanHandler
|
||||
|
||||
* BeanListHandler
|
||||
|
||||
* MapListHandler
|
||||
|
||||
* ResultSetHandler
|
||||
|
||||
* ArrayListHandler
|
||||
|
||||
* DataSource
|
||||
|
||||
|
||||
[http://www.javatips.net/blog/2014/03/apache-dbutils-tutorial](http://www.javatips.net/blog/2014/03/apache-dbutils-tutorial)
|
||||
|
||||
[http://www.codesuggestions.com/java/apache-commons-dbutils-tutorial/](http://www.codesuggestions.com/java/apache-commons-dbutils-tutorial/)
|
||||
@@ -0,0 +1,68 @@
|
||||
<?xml version="1.0"?>
|
||||
<project
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>net.aimeizi</groupId>
|
||||
<artifactId>maven-framework-project</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>commons-dbutils-tutorial</artifactId>
|
||||
<name>commons-dbutils-tutorial</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-dbutils</groupId>
|
||||
<artifactId>commons-dbutils</artifactId>
|
||||
<version>1.5</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.179</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-dbcp</groupId>
|
||||
<artifactId>commons-dbcp</artifactId>
|
||||
<version>1.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-pool</groupId>
|
||||
<artifactId>commons-pool</artifactId>
|
||||
<version>1.6</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>c3p0</groupId>
|
||||
<artifactId>c3p0</artifactId>
|
||||
<version>0.9.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mchange</groupId>
|
||||
<artifactId>mchange-commons-java</artifactId>
|
||||
<version>0.2.7</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.jolbox</groupId>
|
||||
<artifactId>bonecp</artifactId>
|
||||
<version>0.8.0.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.31</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package org.commons.dbutils.datasource.tutorial;
|
||||
|
||||
import java.beans.PropertyVetoException;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.jolbox.bonecp.BoneCP;
|
||||
import com.jolbox.bonecp.BoneCPConfig;
|
||||
|
||||
public class BoneCPDataSource {
|
||||
|
||||
private DataSource dataSource;
|
||||
|
||||
private static BoneCPDataSource boneCPDataSource;
|
||||
|
||||
private BoneCP connectionPool;
|
||||
|
||||
private BoneCPDataSource() throws IOException, SQLException,
|
||||
PropertyVetoException {
|
||||
try {
|
||||
// load the database driver (make sure this is in your classpath!)
|
||||
Class.forName("org.h2.Driver");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// setup the connection pool using BoneCP Configuration
|
||||
BoneCPConfig config = new BoneCPConfig();
|
||||
// jdbc url specific to your database, eg
|
||||
// jdbc:mysql://127.0.0.1/yourdb
|
||||
config.setJdbcUrl("jdbc:h2:./target/test;AUTO_SERVER=TRUE");
|
||||
config.setUsername("sa");
|
||||
config.setPassword("");
|
||||
config.setMinConnectionsPerPartition(5);
|
||||
config.setMaxConnectionsPerPartition(10);
|
||||
config.setPartitionCount(1);
|
||||
// setup the connection pool
|
||||
connectionPool = new BoneCP(config);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static BoneCPDataSource getInstance() throws IOException,
|
||||
SQLException, PropertyVetoException {
|
||||
if (boneCPDataSource == null) {
|
||||
boneCPDataSource = new BoneCPDataSource();
|
||||
return boneCPDataSource;
|
||||
} else {
|
||||
return boneCPDataSource;
|
||||
}
|
||||
}
|
||||
|
||||
public Connection getConnection() throws SQLException {
|
||||
return this.connectionPool.getConnection();
|
||||
}
|
||||
|
||||
public DataSource getDataSource() {
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package org.commons.dbutils.datasource.tutorial;
|
||||
|
||||
import java.beans.PropertyVetoException;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||
|
||||
public class C3P0DataSource {
|
||||
|
||||
private DataSource dataSource;
|
||||
|
||||
private static C3P0DataSource c3p0DataSource;
|
||||
|
||||
private ComboPooledDataSource cpds;
|
||||
|
||||
private C3P0DataSource() throws IOException, SQLException,
|
||||
PropertyVetoException {
|
||||
cpds = new ComboPooledDataSource();
|
||||
cpds.setDriverClass("org.h2.Driver"); // loads the jdbc driver
|
||||
cpds.setJdbcUrl("jdbc:h2:./target/test;AUTO_SERVER=TRUE");
|
||||
cpds.setUser("sa");
|
||||
cpds.setPassword("");
|
||||
|
||||
// the settings below are optional -- c3p0 can work with defaults
|
||||
cpds.setMinPoolSize(5);
|
||||
cpds.setAcquireIncrement(5);
|
||||
cpds.setMaxPoolSize(20);
|
||||
cpds.setMaxStatements(180);
|
||||
|
||||
this.setDataSource(cpds);
|
||||
|
||||
}
|
||||
|
||||
public static C3P0DataSource getInstance() throws IOException,
|
||||
SQLException, PropertyVetoException {
|
||||
if (c3p0DataSource == null) {
|
||||
c3p0DataSource = new C3P0DataSource();
|
||||
return c3p0DataSource;
|
||||
} else {
|
||||
return c3p0DataSource;
|
||||
}
|
||||
}
|
||||
|
||||
public Connection getConnection() throws SQLException {
|
||||
return this.cpds.getConnection();
|
||||
}
|
||||
|
||||
public DataSource getDataSource() {
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package org.commons.dbutils.datasource.tutorial;
|
||||
|
||||
import java.beans.PropertyVetoException;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.commons.dbcp.BasicDataSource;
|
||||
|
||||
public class DBCPDataSource {
|
||||
|
||||
private DataSource dataSource;
|
||||
|
||||
private static DBCPDataSource dbcpDataSource;
|
||||
|
||||
private BasicDataSource ds;
|
||||
|
||||
private DBCPDataSource() throws IOException, SQLException,
|
||||
PropertyVetoException {
|
||||
|
||||
ds = new BasicDataSource();
|
||||
ds.setDriverClassName("org.h2.Driver");
|
||||
ds.setUsername("sa");
|
||||
ds.setPassword("");
|
||||
ds.setUrl("jdbc:h2:./target/test;AUTO_SERVER=TRUE");
|
||||
|
||||
// the settings below are optional -- dbcp can work with defaults
|
||||
ds.setMinIdle(5);
|
||||
ds.setMaxIdle(20);
|
||||
ds.setMaxOpenPreparedStatements(180);
|
||||
|
||||
this.setDataSource(ds);
|
||||
|
||||
}
|
||||
|
||||
public static DBCPDataSource getInstance() throws IOException,
|
||||
SQLException, PropertyVetoException {
|
||||
if (dbcpDataSource == null) {
|
||||
dbcpDataSource = new DBCPDataSource();
|
||||
return dbcpDataSource;
|
||||
} else {
|
||||
return dbcpDataSource;
|
||||
}
|
||||
}
|
||||
|
||||
public DataSource getDataSource() {
|
||||
return this.dataSource;
|
||||
}
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public Connection getConnection() throws SQLException {
|
||||
return this.ds.getConnection();
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package org.commons.dbutils.employee.tutorial;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.apache.commons.dbutils.DbUtils;
|
||||
import org.apache.commons.dbutils.QueryRunner;
|
||||
import org.apache.commons.dbutils.handlers.ArrayListHandler;
|
||||
|
||||
public class ArrayListHandlerExample {
|
||||
|
||||
public static void main(String[] args) throws SQLException {
|
||||
|
||||
final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
|
||||
final String driver = "org.h2.Driver";
|
||||
final String usr = "sa";
|
||||
final String pwd = "";
|
||||
|
||||
QueryRunner run = new QueryRunner();
|
||||
|
||||
DbUtils.loadDriver(driver);
|
||||
Connection conn = DriverManager.getConnection(url, usr, pwd);
|
||||
// -----------------------------------------------------------------------------------
|
||||
|
||||
try {
|
||||
List<Object[]> query = run.query(conn, "SELECT * FROM employee",
|
||||
new ArrayListHandler());
|
||||
for (Object[] objects : query) {
|
||||
System.out.println(Arrays.toString(objects));
|
||||
}
|
||||
} finally {
|
||||
DbUtils.close(conn);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package org.commons.dbutils.employee.tutorial;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.apache.commons.dbutils.DbUtils;
|
||||
import org.apache.commons.dbutils.QueryRunner;
|
||||
import org.apache.commons.dbutils.ResultSetHandler;
|
||||
import org.apache.commons.dbutils.handlers.BeanHandler;
|
||||
|
||||
public class BeanHandlerExample {
|
||||
|
||||
public static void main(String[] args) throws SQLException {
|
||||
|
||||
final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
|
||||
final String driver = "org.h2.Driver";
|
||||
final String usr = "sa";
|
||||
final String pwd = "";
|
||||
|
||||
QueryRunner run = new QueryRunner();
|
||||
|
||||
DbUtils.loadDriver(driver);
|
||||
Connection conn = DriverManager.getConnection(url, usr, pwd);
|
||||
// -----------------------------------------------------------------------------------
|
||||
ResultSetHandler<Employee> resultHandler = new BeanHandler<Employee>(
|
||||
Employee.class);
|
||||
|
||||
try {
|
||||
Employee emp = run.query(conn,
|
||||
"SELECT * FROM employee WHERE employeename=?",
|
||||
resultHandler, "Jose");
|
||||
System.out.println(emp.getEmployeeId());
|
||||
} finally {
|
||||
DbUtils.close(conn);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package org.commons.dbutils.employee.tutorial;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.dbutils.DbUtils;
|
||||
import org.apache.commons.dbutils.QueryRunner;
|
||||
import org.apache.commons.dbutils.ResultSetHandler;
|
||||
import org.apache.commons.dbutils.handlers.BeanListHandler;
|
||||
|
||||
public class BeanListHandlerExample {
|
||||
|
||||
public static void main(String[] args) throws SQLException {
|
||||
|
||||
final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
|
||||
final String driver = "org.h2.Driver";
|
||||
final String usr = "sa";
|
||||
final String pwd = "";
|
||||
|
||||
QueryRunner run = new QueryRunner();
|
||||
|
||||
DbUtils.loadDriver(driver);
|
||||
|
||||
Connection conn = DriverManager.getConnection(url, usr, pwd);
|
||||
// -----------------------------------------------------------------------------------
|
||||
ResultSetHandler<List<Employee>> resultListHandler = new BeanListHandler<Employee>(
|
||||
Employee.class);
|
||||
|
||||
try {
|
||||
List<Employee> empList = run.query(conn, "SELECT * FROM employee",
|
||||
resultListHandler);
|
||||
System.out.println(empList);
|
||||
} finally {
|
||||
DbUtils.close(conn);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package org.commons.dbutils.employee.tutorial;
|
||||
|
||||
import java.beans.PropertyVetoException;
|
||||
import java.io.IOException;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.dbutils.QueryRunner;
|
||||
import org.apache.commons.dbutils.ResultSetHandler;
|
||||
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.MapListHandler;
|
||||
import org.commons.dbutils.datasource.tutorial.DBCPDataSource;
|
||||
|
||||
public class DBUtilsDataSourceExample {
|
||||
|
||||
public static void main(String[] args) throws SQLException, IOException,
|
||||
PropertyVetoException {
|
||||
|
||||
QueryRunner run = new QueryRunner(DBCPDataSource.getInstance()
|
||||
.getDataSource());
|
||||
|
||||
// -----------------------------------------------------------------------------------
|
||||
ResultSetHandler<Employee> resultHandler = new BeanHandler<Employee>(
|
||||
Employee.class);
|
||||
ResultSetHandler<List<Employee>> resultListHandler = new BeanListHandler<Employee>(
|
||||
Employee.class);
|
||||
|
||||
Employee emp = run.query("SELECT * FROM employee WHERE employeename=?",
|
||||
resultHandler, "Jose");
|
||||
System.out.println(emp.getEmployeeId());
|
||||
// -----------------------------------------------------------------------------------
|
||||
|
||||
List<Employee> empList = run.query("SELECT * FROM employee",
|
||||
resultListHandler);
|
||||
System.out.println(empList);
|
||||
// -----------------------------------------------------------------------------------
|
||||
|
||||
List<Map<String, Object>> maps = run.query("SELECT * FROM employee",
|
||||
new MapListHandler());
|
||||
System.out.println(maps);
|
||||
// -----------------------------------------------------------------------------------
|
||||
|
||||
List<Object[]> query = run.query("SELECT * FROM employee",
|
||||
new ArrayListHandler());
|
||||
for (Object[] objects : query) {
|
||||
System.out.println(Arrays.toString(objects));
|
||||
}
|
||||
// -----------------------------------------------------------------------------------
|
||||
|
||||
ResultSetHandler<Object[]> handler = new ResultSetHandler<Object[]>() {
|
||||
public Object[] handle(ResultSet rs) throws SQLException {
|
||||
if (!rs.next()) {
|
||||
return null;
|
||||
}
|
||||
ResultSetMetaData meta = rs.getMetaData();
|
||||
int cols = meta.getColumnCount();
|
||||
Object[] result = new Object[cols];
|
||||
|
||||
for (int i = 0; i < cols; i++) {
|
||||
result[i] = rs.getObject(i + 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
Object[] result = run.query(
|
||||
"SELECT * FROM employee WHERE employeename=?", handler, "Jose");
|
||||
System.out.println(Arrays.toString(result));
|
||||
|
||||
// -----------------------------------------------------------------------------------
|
||||
|
||||
try {
|
||||
// Execute the SQL update statement and return the number of
|
||||
// inserts that were made
|
||||
int inserts = run.update(
|
||||
"INSERT INTO employee (employeename) VALUES (?)", "Arun");
|
||||
// The line before uses varargs and autoboxing to simplify the code
|
||||
System.out.println("inserts " + inserts);
|
||||
// Now it's time to rise to the occation...
|
||||
int updates = run.update(
|
||||
"UPDATE employee SET employeename=? WHERE employeeid=?",
|
||||
"Simon", 1);
|
||||
System.out.println("updates " + updates);
|
||||
} catch (SQLException sqle) {
|
||||
// Handle it
|
||||
sqle.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package org.commons.dbutils.employee.tutorial;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import org.apache.commons.dbutils.DbUtils;
|
||||
import org.apache.commons.dbutils.QueryRunner;
|
||||
|
||||
public class DBUtilsInsertUpdateExample {
|
||||
|
||||
public static void main(String[] args) throws SQLException {
|
||||
|
||||
final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
|
||||
final String driver = "org.h2.Driver";
|
||||
final String usr = "sa";
|
||||
final String pwd = "";
|
||||
|
||||
QueryRunner run = new QueryRunner();
|
||||
|
||||
DbUtils.loadDriver(driver);
|
||||
Connection conn = DriverManager.getConnection(url, usr, pwd);
|
||||
// -----------------------------------------------------------------------------------
|
||||
|
||||
try {
|
||||
// Execute the SQL update statement and return the number of
|
||||
// inserts that were made
|
||||
int inserts = run.update(conn,
|
||||
"INSERT INTO employee (employeename) VALUES (?)", "Arun");
|
||||
// The line before uses varargs and autoboxing to simplify the code
|
||||
System.out.println("inserts " + inserts);
|
||||
// Now it's time to rise to the occation...
|
||||
int updates = run.update(conn,
|
||||
"UPDATE employee SET employeename=? WHERE employeeid=?",
|
||||
"Simon", 1);
|
||||
System.out.println("updates " + updates);
|
||||
} catch (SQLException sqle) {
|
||||
// Handle it
|
||||
sqle.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package org.commons.dbutils.employee.tutorial;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private int employeeId;
|
||||
|
||||
private String employeeName;
|
||||
|
||||
public int getEmployeeId() {
|
||||
return employeeId;
|
||||
}
|
||||
|
||||
public void setEmployeeId(int employeeId) {
|
||||
this.employeeId = employeeId;
|
||||
}
|
||||
|
||||
public String getEmployeeName() {
|
||||
return employeeName;
|
||||
}
|
||||
|
||||
public void setEmployeeName(String employeeName) {
|
||||
this.employeeName = employeeName;
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package org.commons.dbutils.employee.tutorial;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.dbutils.DbUtils;
|
||||
import org.apache.commons.dbutils.QueryRunner;
|
||||
import org.apache.commons.dbutils.handlers.MapListHandler;
|
||||
|
||||
public class MapListHandlerExample {
|
||||
|
||||
public static void main(String[] args) throws SQLException {
|
||||
|
||||
final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
|
||||
final String driver = "org.h2.Driver";
|
||||
final String usr = "sa";
|
||||
final String pwd = "";
|
||||
|
||||
QueryRunner run = new QueryRunner();
|
||||
|
||||
DbUtils.loadDriver(driver);
|
||||
Connection conn = DriverManager.getConnection(url, usr, pwd);
|
||||
// -----------------------------------------------------------------------------------
|
||||
|
||||
try {
|
||||
List<Map<String, Object>> maps = run.query(conn,
|
||||
"SELECT * FROM employee", new MapListHandler());
|
||||
System.out.println(maps);
|
||||
} finally {
|
||||
DbUtils.close(conn);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package org.commons.dbutils.employee.tutorial;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import org.apache.commons.dbutils.DbUtils;
|
||||
import org.apache.commons.dbutils.QueryRunner;
|
||||
import org.apache.commons.dbutils.ResultSetHandler;
|
||||
|
||||
public class ResultSetHandlerExample {
|
||||
|
||||
public static void main(String[] args) throws SQLException {
|
||||
|
||||
final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
|
||||
final String driver = "org.h2.Driver";
|
||||
final String usr = "sa";
|
||||
final String pwd = "";
|
||||
|
||||
QueryRunner run = new QueryRunner();
|
||||
|
||||
DbUtils.loadDriver(driver);
|
||||
Connection conn = DriverManager.getConnection(url, usr, pwd);
|
||||
// -----------------------------------------------------------------------------------
|
||||
|
||||
ResultSetHandler<Object[]> handler = new ResultSetHandler<Object[]>() {
|
||||
public Object[] handle(ResultSet rs) throws SQLException {
|
||||
if (!rs.next()) {
|
||||
return null;
|
||||
}
|
||||
ResultSetMetaData meta = rs.getMetaData();
|
||||
int cols = meta.getColumnCount();
|
||||
Object[] result = new Object[cols];
|
||||
|
||||
for (int i = 0; i < cols; i++) {
|
||||
result[i] = rs.getObject(i + 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
Object[] result = run.query(conn,
|
||||
"SELECT * FROM employee WHERE employeename=?", handler,
|
||||
"Rockey");
|
||||
System.out.println(Arrays.toString(result));
|
||||
|
||||
} finally { // Use this helper method so we don't have to check for
|
||||
DbUtils.close(conn);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package org.commons.dbutils.user.tutorial;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.apache.commons.dbutils.DbUtils;
|
||||
import org.apache.commons.dbutils.QueryRunner;
|
||||
import org.apache.commons.dbutils.handlers.BeanHandler;
|
||||
|
||||
public class DbUtilsBeanHandler {
|
||||
public static void main(String[] args) {
|
||||
Connection conn = null;
|
||||
|
||||
final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
|
||||
final String driver = "org.h2.Driver";
|
||||
final String usr = "sa";
|
||||
final String pwd = "";
|
||||
|
||||
User user = null;
|
||||
|
||||
try {
|
||||
// Loading the Driver using DbUtils static method
|
||||
DbUtils.loadDriver(driver);
|
||||
conn = DriverManager.getConnection(url, usr, pwd);
|
||||
QueryRunner query = new QueryRunner();
|
||||
user = (User) query.query(conn,
|
||||
"select * from user where userId=3", new BeanHandler<User>(
|
||||
User.class));
|
||||
|
||||
System.out.println("User Object:: " + user.getUserId() + "\t"
|
||||
+ user.getFirstName() + "\t" + user.getLastName() + "\t"
|
||||
+ user.getEmailId());
|
||||
|
||||
} catch (SQLException se) {
|
||||
se.printStackTrace();
|
||||
} finally {
|
||||
DbUtils.closeQuietly(conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package org.commons.dbutils.user.tutorial;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import org.apache.commons.dbutils.DbUtils;
|
||||
import org.apache.commons.dbutils.QueryRunner;
|
||||
import org.apache.commons.dbutils.handlers.BeanListHandler;
|
||||
|
||||
public class DbUtilsBeanListHandler {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Connection conn = null;
|
||||
|
||||
final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
|
||||
final String driver = "org.h2.Driver";
|
||||
final String usr = "sa";
|
||||
final String pwd = "";
|
||||
|
||||
List<User> users = null;
|
||||
|
||||
try {
|
||||
DbUtils.loadDriver(driver);
|
||||
conn = DriverManager.getConnection(url, usr, pwd);
|
||||
QueryRunner query = new QueryRunner();
|
||||
users = (List<User>) query.query(conn, "select * from user",
|
||||
new BeanListHandler<User>(User.class));
|
||||
for (int i = 0; i < users.size(); i++) {
|
||||
User bean = users.get(i);
|
||||
System.out.println("User Objects:: " + bean.getUserId() + "\t"
|
||||
+ bean.getFirstName() + "\t" + bean.getLastName()
|
||||
+ "\t" + bean.getEmailId());
|
||||
}
|
||||
} catch (SQLException se) {
|
||||
se.printStackTrace();
|
||||
} finally {
|
||||
DbUtils.closeQuietly(conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package org.commons.dbutils.user.tutorial;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.apache.commons.dbutils.DbUtils;
|
||||
import org.apache.commons.dbutils.QueryRunner;
|
||||
import org.apache.commons.dbutils.handlers.MapListHandler;
|
||||
|
||||
public class DbUtilsMapListHandler {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Connection conn = null;
|
||||
|
||||
final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
|
||||
final String driver = "org.h2.Driver";
|
||||
final String usr = "sa";
|
||||
final String pwd = "";
|
||||
|
||||
try {
|
||||
DbUtils.loadDriver(driver);
|
||||
conn = DriverManager.getConnection(url, usr, pwd);
|
||||
QueryRunner query = new QueryRunner();
|
||||
List<Map<String, Object>> mapList = (List<Map<String, Object>>) query
|
||||
.query(conn, "select * from user", new MapListHandler());
|
||||
for (int i = 0; i < mapList.size(); i++) {
|
||||
Map<String, Object> map = (Map<String, Object>) mapList.get(i);
|
||||
System.out.println("------> " + map.get("userId") + "\t"
|
||||
+ map.get("firstName") + "\t" + map.get("emailId"));
|
||||
}
|
||||
} catch (SQLException se) {
|
||||
se.printStackTrace();
|
||||
} finally {
|
||||
DbUtils.closeQuietly(conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package org.commons.dbutils.user.tutorial;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
import org.apache.commons.dbutils.DbUtils;
|
||||
import org.apache.commons.dbutils.handlers.BeanListHandler;
|
||||
|
||||
public class DbUtilsTest1 {
|
||||
public static void main(String[] args) {
|
||||
Connection conn = null;
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
|
||||
final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
|
||||
final String driver = "org.h2.Driver";
|
||||
final String usr = "sa";
|
||||
final String pwd = "";
|
||||
|
||||
List<User> users = null;
|
||||
try {
|
||||
DbUtils.loadDriver(driver);
|
||||
conn = DriverManager.getConnection(url, usr, pwd);
|
||||
stmt = conn.createStatement();
|
||||
rs = stmt.executeQuery("SELECT * FROM user");
|
||||
BeanListHandler<User> listHandler = new BeanListHandler<User>(
|
||||
User.class);
|
||||
users = listHandler.handle(rs);
|
||||
|
||||
for (User user : users) {
|
||||
System.out.println("User Object:: " + user.getUserId() + "\t"
|
||||
+ user.getFirstName() + "\t" + user.getEmailId());
|
||||
}
|
||||
|
||||
} catch (SQLException se) {
|
||||
se.printStackTrace();
|
||||
} finally {
|
||||
DbUtils.closeQuietly(conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package org.commons.dbutils.user.tutorial;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.dbutils.DbUtils;
|
||||
import org.apache.commons.dbutils.handlers.MapListHandler;
|
||||
|
||||
public class DbUtilsTest2 {
|
||||
public static void main(String[] args) {
|
||||
Connection conn = null;
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
|
||||
final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
|
||||
final String driver = "org.h2.Driver";
|
||||
final String usr = "sa";
|
||||
final String pwd = "";
|
||||
|
||||
List<Map<String, Object>> userMaps = null;
|
||||
try {
|
||||
// Loading the Driver using DbUtils static method
|
||||
DbUtils.loadDriver(driver);
|
||||
conn = DriverManager.getConnection(url, usr, pwd);
|
||||
stmt = conn.createStatement();
|
||||
rs = stmt.executeQuery("SELECT * FROM user");
|
||||
MapListHandler mapListHandler = new MapListHandler();
|
||||
userMaps = mapListHandler.handle(rs);
|
||||
|
||||
for (Map<String, Object> mapObj : userMaps) {
|
||||
System.out.println("User Object:: " + mapObj.get("userId")
|
||||
+ "\t" + mapObj.get("lastName") + "\t"
|
||||
+ mapObj.get("phoneNo"));
|
||||
}
|
||||
|
||||
} catch (SQLException se) {
|
||||
se.printStackTrace();
|
||||
} finally {
|
||||
// Closing the connection quietly, means it will handles the
|
||||
// SQLException
|
||||
DbUtils.closeQuietly(conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.commons.dbutils.user.tutorial;
|
||||
|
||||
public class User {
|
||||
private String userId;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String phoneNO;
|
||||
private String emailId;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getPhoneNO() {
|
||||
return phoneNO;
|
||||
}
|
||||
|
||||
public void setPhoneNO(String phoneNO) {
|
||||
this.phoneNO = phoneNO;
|
||||
}
|
||||
|
||||
public String getEmailId() {
|
||||
return emailId;
|
||||
}
|
||||
|
||||
public void setEmailId(String emailId) {
|
||||
this.emailId = emailId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
CREATE TABLE employee (
|
||||
employeeid bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
employeename varchar(255) DEFAULT NULL,
|
||||
PRIMARY KEY (employeeid)
|
||||
);
|
||||
|
||||
insert into employee (employeeid, employeename) values('1','Rockey');
|
||||
insert into employee (employeeid, employeename) values('2','Jose');
|
||||
@@ -0,0 +1,14 @@
|
||||
CREATE TABLE IF NOT EXISTS user (
|
||||
userId int(11) NOT NULL AUTO_INCREMENT,
|
||||
firstName varchar(50) NOT NULL,
|
||||
lastName varchar(50) NOT NULL,
|
||||
phoneNo varchar(50) NOT NULL,
|
||||
emailId varchar(50) NOT NULL,
|
||||
PRIMARY KEY (userId)
|
||||
);
|
||||
|
||||
INSERT INTO user (userId, firstName, lastName, phoneNo, emailId) VALUES
|
||||
(1, 'Pramod', 'Ganta', '9889885566', 'pramod@codesuggestions.com'),
|
||||
(2, 'Suman', 'Manthena', '8858863456', 'suman@codesuggestions.com'),
|
||||
(3, 'Prakash', 'Puli', '9889885566', 'prakash@codesuggestions.com'),
|
||||
(4, 'Rohit', 'Sunkari', '8858863456', 'rohit@codesuggestions.com');
|
||||
@@ -8,12 +8,40 @@
|
||||
</parent>
|
||||
<artifactId>flex-todolist-example</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>flex-todolist-ria-example</module>
|
||||
<module>flex-todolist-web-example</module>
|
||||
<module>flex-todolist-config-example</module>
|
||||
</modules>
|
||||
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>sonatype-repository</id>
|
||||
<url>http://repository.sonatype.org/content/groups/public</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>sonatype-repository</id>
|
||||
<url>http://repository.sonatype.org/content/groups/public</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>net.aimeizi</groupId>
|
||||
<artifactId>maven-framework-project</artifactId>
|
||||
@@ -78,7 +77,8 @@
|
||||
<module>xml-tutorials</module>
|
||||
<module>json-tutorials</module>
|
||||
<module>hibernate-tutorials</module>
|
||||
</modules>
|
||||
<module>commons-dbutils-tutorial</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
|
||||
@@ -134,7 +134,18 @@
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
|
||||
|
||||
<repository>
|
||||
<id>sonatype-repository</id>
|
||||
<url>http://repository.sonatype.org/content/groups/public</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
|
||||
<repository>
|
||||
<id>jboss</id>
|
||||
<name>jboss private nexus</name>
|
||||
@@ -159,17 +170,6 @@
|
||||
</snapshots>
|
||||
</repository>
|
||||
|
||||
<repository>
|
||||
<id>flex-mojos</id>
|
||||
<url>http://josephj.com/lab/flex-mojos/flex-mojos-read-only/repository/</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
|
||||
</repositories>
|
||||
|
||||
<pluginRepositories>
|
||||
@@ -185,10 +185,10 @@
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
|
||||
|
||||
<pluginRepository>
|
||||
<id>flex-mojos</id>
|
||||
<url>http://josephj.com/lab/flex-mojos/flex-mojos-read-only/repository/</url>
|
||||
<id>sonatype-repository</id>
|
||||
<url>http://repository.sonatype.org/content/groups/public</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
</releases>
|
||||
@@ -196,7 +196,7 @@
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
|
||||
|
||||
</pluginRepositories>
|
||||
|
||||
|
||||
@@ -343,7 +343,7 @@
|
||||
<version>2.14.1</version>
|
||||
</plugin>
|
||||
|
||||
<!-- 项目源代码分析报告 -->
|
||||
<!-- 项目æº�代ç �分æž�报å‘?-->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-pmd-plugin</artifactId>
|
||||
@@ -382,7 +382,7 @@
|
||||
<version>2.3</version>
|
||||
</plugin>
|
||||
|
||||
<!-- 用于在站点中生成测试覆盖率报告 -->
|
||||
<!-- 用于在站点ä¸ç”Ÿæˆ�测试覆盖率报å‘?-->
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>cobertura-maven-plugin</artifactId>
|
||||
|
||||
Reference in New Issue
Block a user