jsp連線sql server呼叫資料來源方法 (轉)

amyz發表於2007-08-15
jsp連線sql server呼叫資料來源方法 (轉)[@more@]

這人方法很簡單,是 SERVER資料來源的,並對查詢及插入做了些處理,本方法適合於初學者

/*
 * Created on 2002-8-30
 * CopyRight by
 */
package org.DesignAC.database;

import .sql.*;
import java.util.*;
import com..jcx.sqlserver.SQLServerData;
/**
 * @author Biggie
 *
 * Class Function:連線bean,取 資料來源,這個資料來源也可以用
 * ,LOGIC,resin等提供
 * 支援各種容器及平臺
 *  @version 1.0A
 * */
public class DBConnect {
  Host IP
 private String strHostAddress="127.0.0.1";
  Port
 private int intHostPort=1433;
 
 private String strUserName="sa";
 ">file://PassWord
 private String strPassWord="";
  Name
 private String strDataName="dac";
  Connection
 private int intMaxConnection=10;
 private Connection con=null;
 private Statement stmt=null;
 private ResultSet rs=null;
  source
 private SQLServerDataSource source=null;
 ArrayList ArrayRs=new ArrayList();
 /**
  * @param 構造註冊JDBC
  * */
 public DBConnect(){
 try{
 if(source==null){
 source=new SQLServerDataSource();
 source.setDatabaseName(strDataName);
 source.setServerName(strHostAddress);
 source.setPortNumber(intHostPort);
 source.setUser(strUserName);
 source.setPassword(strPassWord);
 (intMaxConnection);
 
 }
 }catch(Exception e){
 System.out.println("open database error:"+e.getMessage());
 }
 }
 /**
  * @param executeQuery查詢資料庫方法
  * @param 每條ArrayList記錄存為String[] 陣列
  * @return ArrayList
  * @exception SQLException
  */
 public ArrayList executeQuery(String strSql) throws SQLException {
 rs=null;
 try{
 con=source.getConnection();
 stmt=con.createStatement();
 rs=stmt.executeQuery(strSql);
 
 ResultSetMetaData rsmd=rs.getMetaData();
 int numberOfColumns = rsmd.getColumnCount();
 
 斷是否為空
 if(!ArrayRs.isEmpty()){
 ArrayRs.clear();
 }
 /*
  * 將每條記錄寫入陣列
  * 將陣列放在ArrayList裡
  */
  while(rs.next()){
   String[] strArrayTemp=new String[numberOfColumns];
   for(int i=0;i   if(rs.get(i+1)==null){
   strArrayTemp[i]= "";
   }else{
   strArrayTemp[i]=rs.getObject(i+1).toString();
   }
   }
   ArrayRs.add(strArrayTemp);
  }
  return (ArrayList)ArrayRs.clone();
 }catch(Exception e){
 System.out.println("query error:" + e.getMessage());
 }finally{
 if (stmt != null) {
 stmt.close();
 }
 if (con != null) {
 con.close();
 }
 }
 return ArrayRs;
 }
 /**
  * @param executeInsert插入資料方法
  * @return 插入條數是否成功(boolean)
  */
 public boolean executeInsert(String strSql) throws SQLException{
 rs=null;
 try{
 con=source.getConnection();
 stmt=con.createStatement();
 
 con.setAutoCommit(true);
 int i=stmt.executeUpdate(strSql);

 if(i==1){
 return (true);
 }
 }catch(Exception e){
 System.out.println("Insert error:"+e.getMessage());
 }finally{
 if (stmt != null) {
 stmt.close();
 }
 if (con != null) {
 con.close();
 }
 }
 return (false);
 }
 /**
  * @param executeUpdate修改資料方法
  * @return 修改資料數(int)
  */
 public int executeUpdate(String strSql) throws SQLException{
 rs=null;
 int j=0;
 try{
 con=source.getConnection();
 stmt=con.createStatement();
 con.setAutoCommit(false);
 
 j=stmt.executeUpdate(strSql);
 if(j>0){
 con.commit();
 }else{
 con.rollback();
 }
 }catch(Exception e){
 System.out.println("update error:"+e.getMessage());
 }finally{
 if (stmt != null) {
 stmt.close();
 }
 if (con != null) {
 con.close();
 }
 }
 return j;
 }
 /**
  * @param executeDelete刪除資料方法
  * @return 刪除資料數(int)
  */
 public int executeDelete(String strSql) throws SQLException{
 rs=null;
 int j=0;
 try{
 con=source.getConnection();
 stmt=con.createStatement();
 
 con.setAutoCommit(false);
 j=stmt.executeUpdate(strSql);
 
 if(j>0){
 con.commit();
 }else{
 con.rollback();
 }
 }catch(Exception e){
 System.out.println("Delete error:"+e.getMessage());
 }finally{
 if (stmt != null) {
 stmt.close();
 }
 if (con != null) {
 con.close();
 }
 }
 return j;
 }
}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-958825/,如需轉載,請註明出處,否則將追究法律責任。

相關文章