- package com.core;
- import java.sql.*;
- public class ConnDB {
- public Connection conn=null;
- public ResultSet rs=null;
- public Statement stmt=null;
- private String proxool = "org.logicalcobwebs.proxool.ProxoolDriver";
- private String poolname = "proxool.library";
- //说明:此处需要配置web.xml文件
- public ConnDB() { //构造方法
- }
- public Connection getConnection() {
- try {
- Class.forName(proxool);
- conn = DriverManager.getConnection(poolname);
- } catch (ClassNotFoundException e) {
- System.out.println(e.getMessage());
- }
- if (conn == null) {
- System.out.println("没有获取到数据库连接");
- }
- return conn;
- }
- /*
- *功能:执行查询语句
- */
- public ResultSet executeQuery(String sql) {
- try {
- conn = getConnection();
- stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- rs = stmt.executeQuery(sql);
- }
- catch (SQLException ex) {
- System.err.println(ex.getMessage());
- }
- return rs;
- }
- /*
- *功能:执行更新操作
- */
- public int executeUpdate(String sql) {
- int result = 0;
- try {
- conn = getConnection();
- stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- result = stmt.executeUpdate(sql);
- }
- catch (SQLException ex) {
- result = 0;
- }
- try {
- stmt.close();
- }
- catch (SQLException ex1) {
- }
- return result;
- }
- public int executeUpdate_id(String sql) {
- int result = 0;
- try {
- conn = getConnection();
- stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- result = stmt.executeUpdate(sql);
- String ID = "select @@IDENTITY as id";
- rs = stmt.executeQuery(ID);
- if (rs.next()) {
- int autoID = rs.getInt("id");
- result = autoID;
- }
- }
- catch (SQLException ex) {
- result = 0;
- }
- return result;
- }
- /*
- *功能:关闭数据库的连接
- */
- public void close() {
- try {
- if (rs != null) {
- rs.close();
- }
- }
- catch (Exception e) {
- e.printStackTrace(System.err);
- }
- try {
- if (stmt != null) {
- stmt.close();
- }
- }
- catch (Exception e) {
- e.printStackTrace(System.err);
- }
- try {
- if (conn != null) {
- conn.close();
- }
- }
- catch (Exception e) {
- e.printStackTrace(System.err);
- }
- }
- }