1.JDBC
public static void main(String[] args) throws SQLException {
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/jdbc";
String username = "root";
String password = "root";
conn = DriverManager.getConnection (url, username,
password);
stmt = conn.createStatement();
String sql = "select * from users";
rs = stmt.executeQuery(sql);
..........
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally{
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
}
2.C3p0Utils.java
public class C3p0Utils {
private static DataSource ds;
static {
ds=new ComboPooledDataSource();
}
public static DataSource getDataSource(){
return ds;
}
}
3.c3p0数据源
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver </property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property>
<property name="user">root</property>
<property name="password">root</property>
</default-config>
</c3p0-config>