标签:ret util utils resultset tst xml配置 curl course 连接
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///jdbctest?serverTimezone=Hongkong</property>
<property name="user">root</property>
<property name="password">1234</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">20</property>
</default-config>
</c3p0-config>
package jdbcUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JDBCUtils {
private static final ComboPooledDataSource cpds = new ComboPooledDataSource();
public static Connection getConection() throws Exception {
return cpds.getConnection();
}
public static void release(Statement stmt, Connection conn) {
if(stmt != null) {
try {
stmt.close();
}catch(Exception e) {
e.printStackTrace();
}
stmt = null;
}
if(conn != null) {
try {
conn.close();
}catch(Exception e) {
e.printStackTrace();
}
conn = null;
}
}
public static void release(ResultSet rs, Statement stmt, Connection conn) {
if(rs != null) {
try {
rs.close();
}catch(Exception e) {
e.printStackTrace();
}
rs = null;
}
if(stmt != null) {
try {
stmt.close();
}catch(Exception e) {
e.printStackTrace();
}
stmt = null;
}
if(conn != null) {
try {
conn.close();
}catch(Exception e) {
e.printStackTrace();
}
conn = null;
}
}
}
package demo1;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.junit.Test;
import jdbcUtils.JDBCUtils;
public class JDBCDemo4 {
@Test
public void demo4() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 获得连接
conn = JDBCUtils.getConection();
// 编写SQL
String sql = "select * from course";
// 预编译SQL
pstmt = conn.prepareStatement(sql);
// 执行SQL
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt("id") + " " + rs.getString("name") + " " + rs.getString("category") + " "
+ rs.getString("desp") + " " + rs.getTimestamp("createTime"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.release(rs, pstmt, conn);
}
}
}
标签:ret util utils resultset tst xml配置 curl course 连接
原文地址:https://www.cnblogs.com/huowuyan/p/11628092.html