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

📄 connectionmanager.java

📁 彩信开发一般应用好了不?彩信开发一般应用好了不?彩信开发一般应用好了不?彩信开发一般应用好了不?彩信开发一般应用好了不?
💻 JAVA
字号:
package mmsproject.common;

import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.Connection;
import mmsproject.FrmMMSManage;

/**
 *
 * <p>Title: 彩信发送接收项目</p>
 *
 * <p>Description: 数据库操作类(连接控制)</p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: </p>
 *
 * @author tomato
 * @version 1.0
 */
public class ConnectionManager {
  public ConnectionManager() {
  }

  /**
   * 取得数据库连接
   * @return
   */
  public static Connection getSqlConnection() {
    Connection conn = null;

    String connStr = GetProperties.ConnectionString;
    String UserName = GetProperties.UserName;
    String Pwd = GetProperties.Password;

    //Properties prop = new Properties();
    //prop.
    try {
      Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
      //装载驱动程序
      conn = DriverManager.getConnection(
          connStr, // 数据库连接路径
          UserName, // 用户名
          Pwd); // 用户密码
    } catch (Exception e) {
      //如果出现异常,则通过递归的方式不断执行,直到取得连接为之
      FrmMMSManage.m_txtStatus.setText(FrmMMSManage.m_txtStatus.getText()
              + "\n"
              + "连接数据库出错" + e.getMessage()
              );
      try{
        Thread.sleep(5000);
      }catch(Exception ex){
        ex.printStackTrace();
      }
      conn = getSqlConnection();
    }
    return conn;
  }

  /**
   * 关闭数据库连接
   * @param conn 已经存在的数据库连接
   */
  public static void closeSqlConnection(Connection conn) {
    if (conn != null) {
      try {
        conn.close();
      } catch (Exception e) {

      }
    }
  }

  /**
   * 关闭数据库结果集
   * @param rs
   */
  public static void closeSqlResultSet(ResultSet rs) {
    if (rs != null) {
      try {
        rs.close();
      } catch (Exception e) {
        //e.printStackTrace();
      }
    }
  }

  /**
   * 关闭statement
   * @param stmt PreparedStatement
   */
  public static void closeSqlPreparedStatement(PreparedStatement stmt) {
    if (stmt != null) {
      try {
        stmt.close();
      } catch (Exception e) {
        //e.printStackTrace();
      }
    }
  }

  /**
   * 关闭statement
   * @param stmt Statement
   */
  public static void closeSqlStatement(Statement stmt) {
    if (stmt != null) {
      try {
        stmt.close();
      } catch (Exception e) {
        //e.printStackTrace();
      }
    }
  }

}

⌨️ 快捷键说明

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