package com.addbook.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 连接到数据库的基础类
* @author PGJ
*
*/
public class ConnectDBHandler {
private Connection conn;
protected PreparedStatement pstmt;
protected ResultSet rs;
private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
private static final String USER = "allen";
private static final String PWD = "allen";
/**
* 连接到数据库
*
* @return
* @throws Exception
*/
public Connection openConn() throws Exception {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(URL, USER, PWD);
} catch (ClassNotFoundException e) {
throw new Exception("驱动没有找到,请添加驱动");
} catch (SQLException e) {
throw new Exception("连接数据库失败.");
}
return conn;
}
/**
* 关闭连接
*
* @throws Exception
*/
public void closeConn() throws Exception {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
throw new Exception("关闭连接失败!");
} finally {
conn = null;
}
}
}
/**
* 关闭语句集
*
* @throws Exception
*/
public void closePstmt() throws Exception {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
throw new Exception("关闭语句集失败!");
} finally {
pstmt = null;
}
}
}
/**
* 关闭结果集
*
* @throws Exception
*/
public void closeRs() throws Exception {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
throw new Exception("关闭结果集失败!");
} finally {
rs = null;
}
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 连接到数据库的基础类
* @author PGJ
*
*/
public class ConnectDBHandler {
private Connection conn;
protected PreparedStatement pstmt;
protected ResultSet rs;
private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
private static final String USER = "allen";
private static final String PWD = "allen";
/**
* 连接到数据库
*
* @return
* @throws Exception
*/
public Connection openConn() throws Exception {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(URL, USER, PWD);
} catch (ClassNotFoundException e) {
throw new Exception("驱动没有找到,请添加驱动");
} catch (SQLException e) {
throw new Exception("连接数据库失败.");
}
return conn;
}
/**
* 关闭连接
*
* @throws Exception
*/
public void closeConn() throws Exception {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
throw new Exception("关闭连接失败!");
} finally {
conn = null;
}
}
}
/**
* 关闭语句集
*
* @throws Exception
*/
public void closePstmt() throws Exception {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
throw new Exception("关闭语句集失败!");
} finally {
pstmt = null;
}
}
}
/**
* 关闭结果集
*
* @throws Exception
*/
public void closeRs() throws Exception {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
throw new Exception("关闭结果集失败!");
} finally {
rs = null;
}
}
}
}