数据库连接类2

package com.yanek.test;

import java.sql.*;

import javax.naming.*;
import javax.sql.*;

/**
 * 数据库工具类
 * 1,查找DataSource获取数据库连接
 * 2,释放连接资源
 */
public class DBHandle {
    /**
     * 获取数据库连接
     * 1,初始化上下文环境Context
     * 2,在Context中查找DataSource,名字预先给定,这里为:“java:comp/env/jdbc/bbs”
     * 3,如果DataSource不为空,就从DataSource中取出一个连接
     */
    public synchronized static Connection getConn() throws Exception {
        Connection conn = null;

        return conn;
    }
    /**
     * 释放连接相关资源,请注意相关次序
     * 1,先释放结果集
     * 2,再释放语句句柄
     * 3,最后释放连接
     */
    public static void closeResource(Statement st, ResultSet rs, Connection cnn) {
        try {
            if (rs != null) {
                rs.close();
            }
        }
        catch (Exception es) {
            es.printStackTrace();
        }
        try {
            if (st != null) {
                st.close();
            }
        }
        catch (Exception es) {
            es.printStackTrace();
        }
        try {
            if (cnn != null) {
                cnn.close();
                cnn = null;
            }
        }
        catch (Exception es) {
            es.printStackTrace();
        }
    }

    /**
     * 使用DriverManager获取数据库连接
     */
    public static Connection getConnection() throws Exception {
        String driver = "com.mysql.jdbc.Driver";
        String urlString = "jdbc:mysql://localhost/test";
        String user = "root";
        String pwd = "root";
        Class.forName(driver);
        Connection cnn = DriverManager.getConnection(urlString, user, pwd);
        return cnn;
    }

   
   
 public static void main(String[] args) {

 

   try {
    System.out.println("conn:" + DBHandle.getConnection());
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }


 }
}

 

 

 

package com.yanek.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 数据库工具类 1,查找DataSource获取数据库连接 2,释放连接资源
 */
public class DBSource {

 public static Connection getConnection(final String poolName)
   throws SQLException {
  try {
   return DriverManager.getConnection(poolName);
  } catch (Exception e) {

   return null;
  }
 }

 /**
  * 释放连接相关资源,请注意相关次序 1,先释放结果集 2,再释放语句句柄 3,最后释放连接
  */
 public static void closeResource(Statement st, ResultSet rs, Connection cnn) {
  try {
   if (rs != null) {
    rs.close();
   }
  } catch (Exception es) {
   es.printStackTrace();
  }
  try {
   if (st != null) {
    st.close();
   }
  } catch (Exception es) {
   es.printStackTrace();
  }
  try {
   if (cnn != null) {
    cnn.close();
    cnn = null;
   }
  } catch (Exception es) {
   es.printStackTrace();
  }
 }

 /**
  * 使用DriverManager获取数据库连接
  */
 public static Connection getConnection() throws Exception {
  String driver = "com.mysql.jdbc.Driver";
  String urlString = "jdbc:mysql://localhost/test";
  String user = "root";
  String pwd = "root";
  Class.forName(driver);
  Connection cnn = DriverManager.getConnection(urlString, user, pwd);
  return cnn;
 }

 public static void main(String[] args) {

  try {
   System.out.println("conn:" + DBSource.getConnection());
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }
}

上一篇:C#代码扫描工具Sonarqube + Win10+SqlServer2017


下一篇:智能数据连接世界——TalkingData智能数据峰会在京召开