A: c3p0 connection pool:
Use c3p0 connection pool to use an external file, the file is named external c3p0-config.xml. Add c3p0-XXX.jar package inside lib
We connect to the database inside the database name, user name, password, all used in the xml file inside:
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
//默认加载
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///数据库名</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">20</property>
</default-config>
//指定加载
<named-config name="oracle">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///web_07</property>
<property name="user">root</property>
<property name="password">123</property>
</named-config>
</c3p0-config>
use
ComboPooledDataSource DataSource = new ComboPooledDataSource (); c3p0-config.xml file to load, and the default loading
ComboPooledDataSource datasource = new ComboPooledDataSource ( "oracle"); to specify the load, so that simply completes the connection to the database. But we have to operate the database, so the definition of a utility class:
package utils;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class c3p0Utils {
ComboPooledDataSource DataSource = new ComboPooledDataSource();
public DataSource getDataSource() {
return DataSource;
}
public Connection getConnection() {
Connection conn = null;
try {
conn = DataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
Test categories:
@Test
public void login1() {
ComboPooledDataSource datasource = new ComboPooledDataSource();
//ComboPooledDataSource datasource = new ComboPooledDataSource("oracle");
try {
conn = datasource.getConnection();
String sql = "insert into t_user values(null,?,?,?,?,?,?)";
preps = conn.prepareStatement(sql);
preps.setString(1, "lis");
preps.setString(2, "123234");
preps.setInt(3, 22);
preps.setInt(4, 23);
preps.setString(5, "123");
preps.setInt(6, 23);
int row = preps.executeUpdate();
if (row > 0) {
System.out.println("添加成功!");
} else {
System.out.println("添加失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
JDBCUtils.release(conn, preps, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Two .DBCP connection pool
DBCP external profile XXX.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8
username=root
password=root
It's about the same loading method and JDBC, so the direct write tools:
package utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
public class JDBCUtils2 {
public static DataSource dataSource;
public static String driver;
public static String url;
public static String username;
public static String password;
static {
try {
InputStream is = JDBCUtils2.class.getClassLoader().getResourceAsStream("db.properties");
Properties prop = new Properties();
prop.load(is);
dataSource = BasicDataSourceFactory.createDataSource(prop);
driver = prop.getProperty(driver);
url = prop.getProperty(url);
username = prop.getProperty(username);
password = prop.getProperty(password);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void release(Connection conn, PreparedStatement prep, ResultSet rs) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (prep != null) {
try {
prep.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Three: DBUtils operation of the database
BDUtils JDBC is a simplified development kit required jar package: c3p0-XXX.jar, commons-dbcp-XX.jar, commons-dbutils-XX.jar, commons-pool-XXX.jar, additions and deletions in the database changes when there will be a lot of redundant code, we use DBUtils greatly reducing redundant code:
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;
import utils.c3p0Utils;
public class TestMyConnection {
@Test
public void login1() {
try {
QueryRunner runner = new QueryRunner(c3p0Utils.getDataSource());
String sql = "insert into t_user values(null,?,?,?,?,?,?)";
Object[] params = {"wer","75",32,31,"567",12};
int row = runner.update(sql,params);
if (row > 0) {
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
And during the investigation, the need for us to create a javaBean, a class corresponds to a database table, the class of property for the field, during the investigation, when there will be a JavaBean data in the database: so during the investigation when creating a JavaBean.
This is the query in the current table how many pieces of data:
@Test
public void login1() {
try {
QueryRunner runner = new QueryRunner(c3p0Utils.getDataSource());
String sql = "select count(*) from t_user";
Object[] params = {};
Long nn=(Long) runner.query(sql, new ScalarHandler());
System.out.println(nn);
} catch (SQLException e) {
e.printStackTrace();
}
}
Links: https://pan.baidu.com/s/1Gyq7ERrCh8xDzFbl2YFhxw
extraction code: d3bk