⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 conndb.java~29~

📁 图书馆管理系统 第1章 系统简介 图书馆是各大高等院校的重要组成部门
💻 JAVA~29~
字号:
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";

    public Connection getConnection() {
      try {
          Class.forName(proxool);
          conn = DriverManager.getConnection(poolname);
      } catch (ClassNotFoundException e) {
          System.out.println(e.getMessage());
      } catch (SQLException 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);
      }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -