一个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;
  try {
   crs = new CachedRowSet();
   if (values == null || values.length < 1) {
    return executeQueryCachedRowSet(sql);
   }
   conn = getOracleConnection();
   st = conn.prepareStatement(sql);
   for (int i = 0; i < values.length; i++) {
    st.setObject(i + 1, values[i]);
   }
   rs = st.executeQuery();
   crs.populate(rs);
  } catch (Exception se) {
   System.out
     .println("SQLException in DBManager.exceuteQuery, sql is :\n"
       + sql);
   se.printStackTrace();
  } finally {
   DBManager.cleanup(conn, null, rs);
  }
  return crs;
 }
 
 /** 以下是执行查询操作时需要带入查询参数、数据库连接 返回CachedRowSet */
 public static CachedRowSet executeQuery(String sql, Object[] values,Connection conn) {
  CachedRowSet crs = null;
  PreparedStatement st = null;
 // Connection conn = null;
  ResultSet rs = null;
  try {
   crs = new CachedRowSet();
   if (values == null || values.length < 1) {
    return executeQueryCachedRowSet(sql);
   }
  // conn = getOracleConnection();
   st = conn.prepareStatement(sql);
   for (int i = 0; i < values.length; i++) {
    st.setObject(i + 1, values[i]);
   }
   rs = st.executeQuery();
   crs.populate(rs);
  } catch (Exception se) {
   System.out
     .println("SQLException in DBManager.exceuteQuery, sql is :\n"
       + sql);
   se.printStackTrace();
  } finally {
   DBManager.cleanup(null, null, rs);
  }
  return crs;
 }
 /**执行sql返回CachedRowSet 带参数、数据库连接 */
 public static CachedRowSet executeQueryCachedRowSet(String sql,Connection conn) {
     CachedRowSet crs = null;
     Statement st = null;
   
     ResultSet rs = null;
     try {
       crs = new CachedRowSet();
       st = conn.createStatement();
       rs = st.executeQuery(sql);
       crs.populate(rs);
     }
     catch (Exception se) {
       System.out.println("SQLException in DBManager.executeQueryCachedRowSet, sql is :\n" +
                          sql);
       se.printStackTrace();
     }
     finally {
       DBManager.cleanup(null, null, rs);
     }
     return crs;
   }
 /**执行sql返回CachedRowSet 不带参数*/
 public static CachedRowSet executeQueryCachedRowSet(String sql) {
     CachedRowSet crs = null;
     Statement st = null;
     Connection conn = null;
     ResultSet rs = null;
     try {
       crs = new CachedRowSet();
       conn =getOracleConnection();
       st = conn.createStatement();
       rs = st.executeQuery(sql);
       crs.populate(rs);
     }
     catch (Exception se) {
       System.out.println("SQLException in DBManager.executeQueryCachedRowSet, sql is :\n" +
                          sql);
       se.printStackTrace();
     }
     finally {
       DBManager.cleanup(conn, null, rs);
     }
     return crs;
   }
 /** 以下是用来执行增加,删除,修改操作 */
 public static int executeByLong(String sql, Object[] values) {
  PreparedStatement ps = null;
  int rows = 0;
  try {
   conn = getOracleConnection();
   conn.setAutoCommit(false);
   ps = conn.prepareStatement(sql);
   if (values == null || values.length < 1) {
    rows = ps.executeUpdate();
    return rows;
   }
   for (int i = 0; i < values.length; i++) {
    ps.setCharacterStream(i + 1,new StringReader(values[i].toString()),values[i].toString().length());
   }
   rows = ps.executeUpdate();
   conn.commit();
  } catch (Exception e) {
   try {
    conn.rollback();
   } catch (Exception ex) {
    ex.printStackTrace();
    
   }
   e.printStackTrace();
  } finally {
   cleanup(conn, ps);
  }
  return rows;
 }

 }
DBManager类

 

 

 

 

附上一个JSP连接oracle数据的小例子:

 

先建立一个表

create table test(test1 varchar(20),test2 varchar(20) ) 

建立testoracle.jsp文件

代码如下: 
<%@ page contentType="text/html;charset=gb2312"%>    
<%@ page import="java.sql.*"%> 
    
    
<%Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();    
String url="jdbc:oracle:thin:@localhost:1521:orcl"; 
//orcl为你的数据库的SID 
String user="scott"; 
String password="tiger"; 
Connection conn= DriverManager.getConnection(url,user,password);    
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);    
String sql="select * from test";    
ResultSet rs=stmt.executeQuery(sql);    
while(rs.next()) {%>    
您的第一个字段内容为:<%=rs.getString(1)%>    
您的第二个字段内容为:<%=rs.getString(2)%>    
<%}%>    
<%out.print("数据库操作成功!");%>    
<%rs.close();    
stmt.close();    
conn.close();    
%>    
    
  

你可能感兴趣的:(java基础)