81,092
社区成员
发帖
与我相关
我的任务
分享
package com.bu3g.book.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
private Connection con = null; //这就是一个连接的对象
public DBUtil() {
}
public DBUtil(Connection con) {
this.con=con; //产地一个连接的对象进行连接
}
/**
* 获得一个数据库连接
*
* @return 数据库连接
*/
public Connection getConnection() { // 获取一个对象多的函数。。
try {
if (con==null || con.isClosed()==true) { // 连接关闭或者是连接,,,类
Class.forName("oracle.jdbc.OracleDriver"); // oracle.jdbc.OracleDriver
//try{
this.con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ora9i","scott","tiger");
//这是、数据定位符。。user。。。password。。("jdbc:oracle:thin:@localhost:1521:ora9i","scott","tiger");
//}catch(Exception e){System.out.print("1、"+e);}
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return this.con;
}
/**
* 执行增,删,改,的SQL语句
* @param sql 要执行的语句
* @return 成功执行后影响的记录行数
*/
public int excuteUpdate(String sql){
Connection con=this.getConnection();
Statement st=null;
int rs=-1;//保存影响记录数
try {
st=con.createStatement();
rs=st.executeUpdate(sql);
System.out.println(sql);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
this.closeStatement(st);
this.closeConnection();
}
return rs;
}
/**
* 执行查询
* @param sql
* @return 包含多行的结果集对象
*/
public ResultSet query(String sql){
Connection con=this.getConnection();
Statement st=null;
ResultSet rs=null;
try {
st=con.createStatement();
//System.out.print(st);
rs=st.executeQuery(sql);
System.out.println(sql);
} catch (SQLException e) {
// TODO 自动生成 catch 块
//e.printStackTrace();
System.out.print("2"+e);
}
return rs;
}
public void closeConnection(){
try {
if (this.con!=null && this.con.isClosed()!=true) {
this.con.close();
this.con=null;
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public void closeStatement(Statement st){
if (st!=null) {
try {
st.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
public void closeResultSet(ResultSet rs){
if (rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
public void closeAll(Connection con,Statement st,ResultSet rs){
if (rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
if (st!=null) {
try {
st.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
try {
if (con!=null && con.isClosed()!=true) {
con.close();
con=null;
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static void main(String[] args) {
DBUtil obj=new DBUtil();
// int rs=obj.excuteUpdate("insert into adminInfo (aid, aname, amail, alogin, apass, atishi) values(seq_admininfo.nextval,'张三2','123456@qq.com','admin666','123123','123')");
// obj.closeConnection();
// System.out.println(rs);
ResultSet rs=obj.query("select * from adminInfo");
try {
while (rs.next()) {
System.out.print(rs.getString("aname")+" ");
System.out.println(rs.getString("alogin"));
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
obj.closeResultSet(rs);
obj.closeConnection();
}
}
}