日期:2014-05-16  浏览次数:20552 次

一个DBManager类(针对各种数据库连接操作的代码)

package com.function;
?
import java.io.StringReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Vector;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import com.function.DealString;

import sun.jdbc.rowset.CachedRowSet;

public class DBManager {
?private static Connection conn = null;
?private static final String jndiname="java:comp/env/jdbc/fb";
?/** 以下为数据连接方式是连接池操作,需要事先配置好相应的连接池以及数据源 */
?public static Connection getConnection(){

??try {
???Context initCtx = new InitialContext();

???DataSource ds = (DataSource) initCtx.lookup(jndiname);
???conn = ds.getConnection();
???conn.setAutoCommit(true);
??} catch (Exception e) {
???e.printStackTrace();
??}
??return conn;
??
?}

?/** 以下是连接ORACLE数据库 */
?public static Connection getOracleConnection() {
??String url = "jdbc:oracle:thin:@10.10.10.1:1521:hzw";
??String driver = "oracle.jdbc.driver.OracleDriver";
??String uid = "govstock";
??String pwd = "govstock";
??try {
???Class.forName(driver);
??} catch (ClassNotFoundException ex) {
???System.out.println("装载驱动时出错!" + ex.getMessage());
??}
??try {
???//conn = DriverManager.getConnection(url, uid, pwd);
???conn=getConnection();
??} catch (Exception ex1) {
???System.out.println("得到连接时出错" + ex1.getMessage());
??}
??return conn;
?}

?/** 以下是连接MSSQL数据库 */
?public static Connection getMsSqlConnection() {
??String url = "jdbc:microsoft:sqlserver://10.10.10.86:1433;DatabaseName=ccgp-cs1";
??String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
??String uid = "sa";
??String pwd = "sa";
??try {
???Class.forName(driver);
??} catch (ClassNotFoundException ex) {
???System.out.println("装载驱动时出错!" + ex.getMessage());
??}
??try {
???conn = DriverManager.getConnection(url, uid, pwd);
??} catch (SQLException ex1) {
???System.out.println("得到连接时出错" + ex1.getMessage());
??}
??return conn;
?}
?/** 以下是释放数据库资源Connection */
?public static void cleanup(Connection conn) {

??try {
???if (conn != null && !conn.isClosed()) {
????conn.close();
????
???}
??} catch (Exception e) {
???e.printStackTrace();
??}
?}
?/** 以下是释放数据库资源Connection,PreparedStatement */
?public static void cleanup(Connection conn, PreparedStatement ps) {
??try {

???if (ps != null) {
????ps.close();
???}
??} catch (Exception e) {
???e.printStackTrace();
??}
??try {
???if (conn != null && !conn.isClosed()) {
????conn.close();
???}
??} catch (Exception e) {
???e.printStackTrace();
??}
?}
?

?/** 以下是释放数据库资源Connection,PreparedStatement,ResultSet */
?public static void cleanup(Connection conn, PreparedStatement ps,
???ResultSet rs) {

??try {
???if (rs != null) {
????rs.close();
???}
??} catch (Exception e) {
???e.printStackTrace();
??}
??try {
???if (ps != null) {
????ps.close();
???}
??} catch (Exception e) {
??}
??try {
???if (conn != null && !conn.isClosed()) {
????conn.close();
???}
??} catch (Exception e) {
???e.printStackTrace();
??}
?}
?/** 以下是执行查询操作时需要带入查询参数 返回CachedRowSet*/
?public static CachedRowSet executeQuery(String sql, Object[] values) {
??CachedRowSet crs = null;
??PreparedStatement st = null;
??Connection conn = null;
??ResultSet rs = null;