一、JDBC1.0版本JdbcUtils工具类(驱动注册方式)
使用Conneciton驱动注册方式
1、JDBC原始版本
public class Demo {
static String url="jdbc:mysql://localhost:3306/day11";
static String user="root";
static String password="root";
/**
* 获取连接对象
* @return
*/
private static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, "root", "root");
} catch (Exception e) {
throw new RuntimeException();
}
return conn;
}
/**
* 创建表
* @throws SQLException
*/
@Test
public void createTable() throws SQLException {
Connection conn = Demo.getConn();
Statement st=null;
try {
st = conn.createStatement();
String sql="CREATE TABLE stu(id INT ,NAME VARCHAR(20),age int ,birth date);";
int row = st.executeUpdate(sql);
System.out.println("影响了"+row+"行");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally {
if(st!=null) {
st.close();
}
if(conn!=null) {
conn.close();
}
}
}
/**
* 插入数据
* @throws SQLException
*/
@Test
public void insert() throws SQLException {
Connection conn = Demo.getConn();
Statement st=null;
try {
st = conn.createStatement();
String sql="insert into stu(id,name,age) values(1,'小白',12)";
int row = st.executeUpdate(sql);
System.out.println("影响了"+row+"行");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally {
if(st!=null) {
st.close();
}
if(conn!=null) {
conn.close();
}
}
}
/**
* 修改数据
* @throws SQLException
*/
@Test
public void update() throws SQLException {
Connection conn = Demo.getConn();
Statement st=null;
try {
st = conn.createStatement();
String sql="update stu set name='小何' where id=1 ";
int row = st.executeUpdate(sql);
System.out.println("影响了"+row+"行");
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
if(st!=null) {
st.close();
}
if(conn!=null) {
conn.close();
}
}
}
/**
* 查询数据
* @throws SQLException
*/
@Test
public void select() throws SQLException {
Connection conn = Demo.getConn();
Statement st=null;
try {
st = conn.createStatement();
String sql="select * from stu";
ResultSet rs = st.executeQuery(sql);
while(rs.next()) {
//根据索引值获取行数据
int id = rs.getInt(1);
String name = rs.getString(2);
int age = rs.getInt(3);
System.out.println(id+" - "+name+" - "+age);
//根据字段名称获取行数据
int id1 = rs.getInt("id");
String name1 = rs.getString("name");
int age1 = rs.getInt("age");
System.out.println(id1+" - "+name1+" - "+age1);
}
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
if(st!=null) {
st.close();
}
if(conn!=null) {
conn.close();
}
}
}
/**
* 删除数据
* @throws SQLException
*/
@Test
public void delete() throws SQLException {
Connection conn = Demo.getConn();
Statement st=null;
try {
st = conn.createStatement();
String sql="delete from h where id =1 ";
int row = st.executeUpdate(sql);
System.out.println("影响了"+row+"行");
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
if(st!=null) {
st.close();
}
if(conn!=null) {
conn.close();
}
}
}
}
2、JdbcUtil工具1.0 - 原始版本的升级版本
JdbcUtil.java
public class JdbcUtil {
private static String url="jdbc:mysql://localhost:3306/day11";
private static String user="root";
private static String password="root";
/**
* 使用静态代码块来注册驱动,原因:注册驱动只需注册一次即可。
*/
static {
try {
//加载驱动类,自动注册驱动
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 获取连接对象
* @return
*/
public static Connection getConn() {
try {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 关闭连接
* @param conn
* @param st
*/
public static void close(Connection conn,Statement st) {
try {
if(st!=null) {
st.close();
}
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 关闭连接
* @param conn
* @param st
*/
public static void close(Connection conn,Statement st,ResultSet rs) {
try {
if(rs!=null) {
rs.close();
}
if(st!=null) {
st.close();
}
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
测试工具类 Demo.java
public class Demo {
/**
* 插入数据
* @throws SQLException
*/
@Test
public void insert() throws SQLException {
Connection conn = JdbcUtil.getConn();
Statement st=null;
try {
st = conn.createStatement();
String sql="insert into stu(id,name,age) values(1,'小白',12)";
int row = st.executeUpdate(sql);
System.out.println("影响了"+row+"行");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally {
JdbcUtil.close(conn, st);
}
}
/**
* 查询数据
* @throws SQLException
*/
@Test
public void select() throws SQLException {
Connection conn = JdbcUtil.getConn();
Statement st=null;
ResultSet rs=null;
try {
String sql="select * from stu";
st = conn.createStatement();
rs = st.executeQuery(sql);
while(rs.next()) {
//根据索引值获取行数据
int id = rs.getInt(1);
String name = rs.getString(2);
int age = rs.getInt(3);
System.out.println(id+" - "+name+" - "+age);
//根据字段名称获取行数据
int id1 = rs.getInt("id");
String name1 = rs.getString("name");
int age1 = rs.getInt("age");
System.out.println(id1+" - "+name1+" - "+age1);
}
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
JdbcUtil.close(conn, st,rs);
}
}
}
3、JdbcUtil工具2.0 - 读取配置文件(JDBC1.0)
db.properties 配置文件。
该配置文件放在 项目/WEB-INF/bin 根目录下。(即 classpath 目录下)
url=jdbc:mysql://localhost:3306/day11
user=root
password=root
JdbcUtil.java
public class JdbcUtil {
public static Properties p;
/**
* 使用静态代码块来注册驱动,原因:注册驱动只需注册一次即可。
*/
static {
try {
//加载配置文件
p=new Properties();
p.load(JdbcUtil.class.getResourceAsStream("/db.properties"));
//加载驱动类,自动注册驱动
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
System.out.println(JdbcUtil.p);
}
/**
* 获取连接对象
* @return
*/
public static Connection getConn() {
try {
Connection conn = DriverManager.getConnection(p.getProperty("url"), p.getProperty("user"), p.getProperty("password"));
return conn;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 关闭连接
* @param conn
* @param st
*/
public static void close(Connection conn,Statement st) {
try {
if(st!=null) {
st.close();
}
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 关闭连接
* @param conn
* @param st
*/
public static void close(Connection conn,Statement st,ResultSet rs) {
try {
if(rs!=null) {
rs.close();
}
if(st!=null) {
st.close();
}
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
二、JDBC2.0版本JdbcUtils工具类(连接池方式)
使用数据库连接池方式
C3P0连接池
DBCP连接池
1.使用C3P0连接池
c3p0-config.xml
将该xml文件放在classpath根目录下,C3P0会自动加载该配置文件
<c3p0-config>
<!-- 默认数据库 -->
<default-config>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/day11</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">3</property>
<property name="maxPoolSize">6</property>
<property name="maxIdleTime">1000</property>
</default-config>
<!-- 可以使用多数据源
<named-config name="db2">
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">3</property>
<property name="maxPoolSize">6</property>
<property name="maxIdleTime">1000</property>
</named-config>
-->
</c3p0-config>
JdbcUtils.java
public class JdbcUtils {
/**
* 1.初始化C3P0连接池
*/
private static ComboPooledDataSource dataSource;
static {
dataSource=new ComboPooledDataSource();
}
/**
* 2.创建DBUtils的核心工具类对象
*
*
* @return
*/
public static QueryRunner getQueryRunner() {
//创建DBUtils的核心对象。
//在创建QueryRunner对象时,如果传入了数据源对象
//那么在使用QueryRunner的方法时,就不用再传入连接对象、
//会自动从数据源中获取连接。(同时也不用手动关闭连接,它会自动关闭连接资源)
return new QueryRunner(dataSource);
}
}
2. 使用DBCP连接池
db.properties
将该properties文件放在classpath根目录下
url=jdbc:mysql:///jdbc_demo
driverClassName=com.mysql.jdbc.Driver
username=root
password=root
initialSize=3
maxActive=6
maxIdle=3000
JdbcUtils.java
public class JdbcUtils {
/**
* 1.初始化C3P0连接池
*/
private static DataSource dataSource;
static {
try {
Properties p=new Properties();
p.load(JdbcUtils.class.getResourceAsStream("/db.properties"));
dataSource = BasicDataSourceFactory.createDataSource(p);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 2.创建DBUtils的核心工具类对象
*
*
* @return
*/
public static QueryRunner getQueryRunner() {
//创建DBUtils的核心对象。
//在创建QueryRunner对象时,如果传入了数据源对象
//那么在使用QueryRunner的方法时,就不用再传入连接对象、
//会自动从数据源中获取连接。(同时也不用手动关闭连接,它会自动关闭连接资源)
return new QueryRunner(dataSource);
}
}