package com.yanshu.util;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* JDBC连接数据库的工具类
* @author Administrator
*
*/
public class JdbcUtil {
//第一步:声明所有需要的静态的全局变量
private static final long serialVersionUID =1L;
private static final String JDBC_DRIVER;
private static final String DB_URL;
private static final String USER;
private static final String PASS;
private static final Properties pros;
public JdbcUtil() {
// TODO Auto-generated constructor stub
}
//第2不加载读取配置文件,并初始化静态变量
static
{
//读取和处理资源文件的信息
pros=new Properties();
try {
pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JDBC_DRIVER=pros.getProperty("spring.datasource.driver-class-name");
DB_URL=pros.getProperty("spring.datasource.url");
USER = pros.getProperty("spring.datasource.username");
PASS = pros.getProperty("spring.datasource.password");
}
//第二步要加载数据驱动
static{
try {
Class.forName(JDBC_DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获得数据库连接
* getConn()
* @return 返回数据库连接
*/
//第3步获得数据库连接
public static Connection getConn()
{
try {
return DriverManager.getConnection(DB_URL,USER,PASS);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 释放资源
* @param rs 释放结果集ResultSet
* @param cs 释放存储过程 CallableStatement
* @param sta 释放执行sql的对象,静态的
* @param ps 释放执行sql的对象,预编译的
* @param conn 关闭连接
*/
//第4步释放资源
public static void closeConn(ResultSet rs, CallableStatement cs,Statement sta, PreparedStatement ps, Connection conn)
{
try {
if(rs!=null)
{
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(null != cs)
{
cs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(sta != null)
{
sta.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(ps != null)
{
ps.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(conn != null)
{
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 查询方法的思路:
* 1加载驱动,
* 2连接数据库,
* 3创建sql语句执行对象 ,
* 3执行sql语句,
* 4对执行结果进行处理,
* 5关闭相关的连接对象(顺序跟声明的顺序相反)
* @throws SQLException
*/
public static void query() throws SQLException
{
String sql="select * from usersso";
// 首先要获取连接,即连接到数据库
Connection conn=getConn();
//创建sql语句执行对象 ,
Statement st = conn.createStatement();//创建sql执行对象
// 执行sql语句,
ResultSet rs=st.executeQuery(sql);
//5.处理结果集
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println(rs.getInt("id")+" "
+rs.getString("name"));
}
closeConn(rs, null, st, null, conn);
}
/**
* 实现增删改的方法
* 1加载驱动,
* 2连接数据库,
* 3创建sql语句执行对象 ,
* 3执行sql语句,
* @throws SQLException
*/
public static void inset() throws SQLException
{
String sql="";
Connection conn=null;
Statement sta=null;
conn=getConn();
sta=conn.createStatement();
int i=sta.executeUpdate(sql);
closeConn(null, null, sta, null, conn);
}
/**
* 测试jdbc连接数据库是否成功
* @param args
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void main(String[] args) throws ClassNotFoundException, SQLException {
query();
}
}
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* JDBC连接数据库的工具类
* @author Administrator
*
*/
public class JdbcUtil {
//第一步:声明所有需要的静态的全局变量
private static final long serialVersionUID =1L;
private static final String JDBC_DRIVER;
private static final String DB_URL;
private static final String USER;
private static final String PASS;
private static final Properties pros;
public JdbcUtil() {
// TODO Auto-generated constructor stub
}
//第2不加载读取配置文件,并初始化静态变量
static
{
//读取和处理资源文件的信息
pros=new Properties();
try {
pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JDBC_DRIVER=pros.getProperty("spring.datasource.driver-class-name");
DB_URL=pros.getProperty("spring.datasource.url");
USER = pros.getProperty("spring.datasource.username");
PASS = pros.getProperty("spring.datasource.password");
}
//第二步要加载数据驱动
static{
try {
Class.forName(JDBC_DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获得数据库连接
* getConn()
* @return 返回数据库连接
*/
//第3步获得数据库连接
public static Connection getConn()
{
try {
return DriverManager.getConnection(DB_URL,USER,PASS);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 释放资源
* @param rs 释放结果集ResultSet
* @param cs 释放存储过程 CallableStatement
* @param sta 释放执行sql的对象,静态的
* @param ps 释放执行sql的对象,预编译的
* @param conn 关闭连接
*/
//第4步释放资源
public static void closeConn(ResultSet rs, CallableStatement cs,Statement sta, PreparedStatement ps, Connection conn)
{
try {
if(rs!=null)
{
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(null != cs)
{
cs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(sta != null)
{
sta.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(ps != null)
{
ps.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(conn != null)
{
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 查询方法的思路:
* 1加载驱动,
* 2连接数据库,
* 3创建sql语句执行对象 ,
* 3执行sql语句,
* 4对执行结果进行处理,
* 5关闭相关的连接对象(顺序跟声明的顺序相反)
* @throws SQLException
*/
public static void query() throws SQLException
{
String sql="select * from usersso";
// 首先要获取连接,即连接到数据库
Connection conn=getConn();
//创建sql语句执行对象 ,
Statement st = conn.createStatement();//创建sql执行对象
// 执行sql语句,
ResultSet rs=st.executeQuery(sql);
//5.处理结果集
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println(rs.getInt("id")+" "
+rs.getString("name"));
}
closeConn(rs, null, st, null, conn);
}
/**
* 实现增删改的方法
* 1加载驱动,
* 2连接数据库,
* 3创建sql语句执行对象 ,
* 3执行sql语句,
* @throws SQLException
*/
public static void inset() throws SQLException
{
String sql="";
Connection conn=null;
Statement sta=null;
conn=getConn();
sta=conn.createStatement();
int i=sta.executeUpdate(sql);
closeConn(null, null, sta, null, conn);
}
/**
* 测试jdbc连接数据库是否成功
* @param args
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void main(String[] args) throws ClassNotFoundException, SQLException {
query();
}
}