1.数据库连接的配置信息: jdbc.properties (在工程的目录下)个人配置
数据库的名称为:stus
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost/stus
name=root
password=1234
2.获取连接
读取配置信息,加载驱动。连接。(这个在后面的例子常用到)
package com.fengya.util;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtil {
static String driverClass = null;
static String url = null;
static String name = null;
static String password = null;
static {
try {
//1.创建一个属性配置对象
Properties pps = new Properties();
/*
* 两种方法读取属性文件:
* 1.servletContext
* 2.使用类加载器
*/
// InputStream is = new FileInputStream("config/jdbc.properties");
//方法2:使用类加载器,去读取src下的资源文件。后面在servlet
InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("config/jdbc.properties");
//导入输入流
pps.load(is);
//2.读取属性
driverClass = pps.getProperty("driverClass");
url = pps.getProperty("url");
name = pps.getProperty("name");
password = pps.getProperty("password");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接对象
* @return
*/
public static Connection getConn() {
Connection conn = null;
try {
Class.forName(driverClass);
//静态代码块--->类加载了,就执行。java.sql.DriverManager.registerDriver(new Driver());
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=1234");
//2.建立连接 参数1:协议 + 访问的数据库,参数2:用户名, 参数3:密码。
conn = DriverManager.getConnection(url, name, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void release(Connection conn, Statement st, ResultSet rs) {
closeRs(rs);
closeSt(st);
closeConn(conn);
}
private static void closeRs(ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
rs = null;
}
}
private static void closeSt(Statement st) {
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
st = null;
}
}
private static void closeConn(Connection conn) {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn = null;
}
}
}
JDBCUtil2
package com.fengya.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JDBCUtil2 {
public static Connection conn = null;
public static ComboPooledDataSource dataSource = null;
static {
try {
dataSource = new ComboPooledDataSource();
conn = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static ComboPooledDataSource getDataSource() {
return dataSource;
}
public static Connection getConn() {
return conn;
}
public static void release(Connection conn, PreparedStatement ps, ResultSet rs) {
closeConn(conn);
closePS(ps);
closeRS(rs);
}
public static void release(Connection conn, PreparedStatement ps) {
closeConn(conn);
closePS(ps);
}
public static void closeConn(Connection conn) {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn = null;
}
}
public static void closePS(PreparedStatement ps) {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ps = null;
}
}
public static void closeRS(ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
rs = null;
}
}
}