-
package com.xiao.jdbc;
-
-
import java.sql.Connection;
-
import java.sql.PreparedStatement;
-
import java.sql.ResultSet;
-
import org.junit.Test;
-
import com.xiao.utils.JDBCUtils_02;
-
import com.xiao.utils.JDBCUtils_03;
-
/**
-
* @author 笑笑
-
* @Date 2018年3月15日下午2:42:15
-
*
-
*/
-
//通过加载properties文件来连接数据库的两种方式测试
-
public
class JDBC_03 {
-
//通过加载properties文件来连接数据库的方式一 测试
-
@Test
-
public void test_01() throws Exception{
-
//1.使用工具类JDBCUtils_02注册并获取连接
-
Connection cn = JDBCUtils_02.getConnection();
-
//2.书写SQL语句
-
String sql =
"select * from user_tb";
-
//3.获取SQL语句预编译对象
-
PreparedStatement ps = cn.prepareStatement(sql);
-
//4.执行SQL语句
-
ResultSet rs = ps.executeQuery();
-
//5.处理结果集
-
while(rs.next()){
-
System.out.println(rs.getInt(
"uid")+
" "+rs.getString(
"username"));
-
}
-
//6.释放资源
-
JDBCUtils_02.relase(cn, ps, rs);
-
}
-
//通过加载properties文件来连接数据库的方式二 测试
-
@Test
-
public void test_02() throws Exception{
-
//1.使用工具类JDBCUtils_03注册并获取连接
-
Connection cn = JDBCUtils_03.getConnection();
-
//2.书写SQL语句
-
String sql =
"select * from user_tb";
-
//3.获取SQL语句预编译对象
-
PreparedStatement ps = cn.prepareStatement(sql);
-
//4.执行SQL语句
-
ResultSet rs = ps.executeQuery();
-
//5.处理结果集
-
while(rs.next()){
-
System.out.println(rs.getInt(
"uid")+
" "+rs.getString(
"username"));
-
}
-
//6.释放资源
-
JDBCUtils_02.relase(cn, ps, rs);
-
}
-
}
-
package com.xiao.utils;
-
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.ResultSet;
-
import java.sql.Statement;
-
import java.util.ResourceBundle;
-
/**
-
* @author 笑笑
-
* @Date 2018年3月15日下午2:38:39
-
*
-
*/
-
//通过加载properties文件来连接数据库的方式一
-
public
class JDBCUtils_02 {
-
private
static String driver;
-
private
static String url;
-
private
static String username;
-
private
static String password;
-
/**
-
* 静态代码块加载配置文件信息
-
*/
-
static{
-
ResourceBundle bundle = ResourceBundle.getBundle(
"jdbc");
-
driver = bundle.getString(
"driver");
-
url = bundle.getString(
"url");
-
username = bundle.getString(
"username");
-
password = bundle.getString(
"password");
-
}
-
//注册并获取数据库连接
-
public static Connection getConnection() throws Exception{
-
//1.注册驱动
-
Class.forName(driver);
-
//2.获取数据库连接
-
Connection cn = DriverManager.getConnection(url, username, password);
-
return cn;
-
}
-
//释放资源
-
public static void relase(Connection cn,Statement st,ResultSet rs) throws Exception{
-
if(rs!=
null){
-
rs.close();
-
}
-
if(st!=
null){
-
st.close();
-
}
-
if(cn!=
null){
-
cn.close();
-
}
-
}
-
-
}
-
package com.xiao.utils;
-
-
import java.io.IOException;
-
import java.io.InputStream;
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.ResultSet;
-
import java.sql.Statement;
-
import java.util.Properties;
-
/**
-
* @author 笑笑
-
* @Date 2018年3月15日下午2:38:39
-
*
-
*/
-
//通过加载properties文件来连接数据库的方式二
-
public
class JDBCUtils_03 {
-
private
static String driver;
-
private
static String url;
-
private
static String username;
-
private
static String password;
-
/**
-
* 静态代码块加载配置文件信息
-
*/
-
static {
-
try {
-
// 1.通过当前类获取类加载器
-
ClassLoader classLoader = JDBCUtils_03.class.getClassLoader();
-
// 2.通过类加载器的方法获得一个输入流
-
InputStream in = classLoader.getResourceAsStream(
"jdbc.properties");
-
// 3.创建一个properties对象(集合)
-
Properties props =
new Properties();
-
// 4.加载输入流
-
props.load(in);
-
// 5.获取相关参数的值
-
driver = props.getProperty(
"driver");
-
url = props.getProperty(
"url");
-
username = props.getProperty(
"username");
-
password = props.getProperty(
"password");
-
}
catch (IOException e) {
-
e.printStackTrace();
-
}
-
-
}
-
//注册并获取数据库连接
-
public static Connection getConnection() throws Exception{
-
//1.注册驱动
-
Class.forName(driver);
-
//2.获取数据库连接
-
Connection cn = DriverManager.getConnection(url, username, password);
-
return cn;
-
}
-
//释放资源
-
public static void relase(Connection cn,Statement st,ResultSet rs) throws Exception{
-
if(rs!=
null){
-
rs.close();
-
}
-
if(st!=
null){
-
st.close();
-
}
-
if(cn!=
null){
-
cn.close();
-
}
-
}
-
-
}
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/all_db?useUnicode=true&characterEncoding=utf8 username=root password=root