使用连接池的方式连接数据库:使用DBUtil连接ORACLE数据库
2012-07-28 11:17
465 查看
package blog.util; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import blog.exception.DBException; public class DBUtil { private static DataSource ds = null; /** * 从数据库连接池获得一个数据库连接 * @return 数据库连接 * @throws DBException */ public static Connection getConnection() throws DBException { //用数据库连接池的方式实现,JNDI try { if(ds == null){ Context context = new InitialContext(); ds = (DataSource) context.lookup("java:comp/env/jdbc/orcl"); } return ds.getConnection(); } catch (NamingException e) { throw new DBException("数据库连接池查找失败", e); } catch (SQLException e) { throw new DBException("获取数据库连接异常", e); } } public static PreparedStatement getPreparedStatement(Connection conn, String sql) throws DBException { PreparedStatement pstmt = null; try { if (conn != null) { pstmt = conn.prepareStatement(sql); } } catch (SQLException e) { throw new DBException("创建执行语句失败", e); } return pstmt; } /** * @Parameters:autoGeneratedKeys - a flag indicating whether auto-generated keys should be returned; one of Statement.RETURN_GENERATED_KEYS or Statement.NO_GENERATED_KEYS */ public static PreparedStatement getPreparedStatement(Connection conn, String sql, int autoGenereatedKeys) throws DBException { PreparedStatement pstmt = null; try { if (conn != null) { pstmt = conn.prepareStatement(sql, autoGenereatedKeys); } } catch (SQLException e) { throw new DBException("创建执行语句失败", e); } return pstmt; } public static Statement getStatement(Connection conn) throws DBException { Statement stmt = null; try { if (conn != null) { stmt = conn.createStatement(); } } catch (SQLException e) { throw new DBException("创建执行语句失败", e); } return stmt; } public static ResultSet getResultSet(Statement stmt, String sql) throws DBException { ResultSet rs = null; try { if (stmt != null) { rs = stmt.executeQuery(sql); } } catch (SQLException e) { throw new DBException("获得查询结果集失败:" + sql, e); } return rs; } public static void executeUpdate(Statement stmt, String sql) throws DBException { try { if (stmt != null) { stmt.executeUpdate(sql); } } catch (SQLException e) { throw new DBException("更新失败:" + sql, e); } } public static void executeUpdate(PreparedStatement pstmt) throws DBException { try { if (pstmt != null) { pstmt.executeUpdate(); } } catch (SQLException e) { throw new DBException("更新失败", e); } } /** * 归还数据库连接 * @param conn 数据库连接实例 * @throws DBException */ public static void close(Connection conn) throws DBException { try { if (conn != null) { conn.close(); //把数据库连接归还到数据库连接池,并不是真正的断开数据库的连接 } } catch (SQLException e) { throw new DBException("关闭数据库连接异常", e); } } public static void close(Statement stmt) throws DBException { try { if (stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { throw new DBException("关闭数据库语句异常", e); } } public static void close(PreparedStatement pstmt) throws DBException { try { if (pstmt != null) { pstmt.close(); pstmt = null; } } catch (SQLException e) { throw new DBException("关闭数据库语句异常", e); } } public static void close(ResultSet rs) throws DBException { try { if (rs != null) { rs.close(); rs = null; } } catch (SQLException e) { throw new DBException("关闭数据库结果集异常", e); } } }
package blog.exception; public class DBException extends Exception { public DBException() { super(); } public DBException(String message, Throwable cause) { super(message, cause); } public DBException(String message) { super(message); } public DBException(Throwable cause) { super(cause); } }
<Context reloadable="true"> <Resource name="jdbc/orcl" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="xzf" password="1234" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"> </Resource> </Context>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <resource-ref> <res-ref-name>jdbc/orcl</res-ref-name> <!--orcl为数据库实例名--> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>
相关文章推荐
- 使用连接池的方式连接数据库:使用DBUtil连接ORACLE数据库
- 使用连接池的方式连接数据库:使用DBUtil连接MYSQL数据库
- 使用连接池的方式连接数据库:使用DBUtil连接MYSQL数据库
- Oracle BIEE11.1.1.5.0 Client 在使用OCI方式连接oracle数据库时无法连接数据库
- ASP.NET在IIS上部署使用Oracle数据库无法连接数据库解决方法
- Nodejs使用连接池连接数据库
- Java_JDBC连接数据库_使用读取配置文件的方式
- 使用sqlexpress的数据库连接配置,使用附加数据库方式
- 关于使用tomcat自带连接池xapool偶尔无法获取数据库连接的问题(报空指针)
- 使用连接池和缓存机制,处理连接数据库操作
- java 演示使用jdbc-odbc桥连接的方式操作数据库 向数据库中添加数据
- 使用OLE DB方式连接常用数据库的连接字符串的设置
- 《关于在MFC中使用ODBC方式连接数据库可能出现的问题》第一篇
- PHP使用数据库永久连接方式操作MySQL的是与非
- 使用JDBC-ODBC桥接的方式连接数据库
- springboot使用之一:连接生产数据库,添加连接池
- 在Tomcat里使用配置连接池连接数据库
- 数据库连接从每次建立连接到配置文件的解耦和连接池的使用
- 使用C#的两种方式OracleClient组件和OleDB组件连接ORACLE数据库
- 使用OLE DB方式连接常用数据库的连接字符串的设置