jsp連線sql server呼叫資料來源方法 (轉)
這人方法很簡單,是 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
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JSP連線SQL Server資料庫JSSQLServer資料庫
- 連線資料來源(轉)
- (轉)檢視sql server 資料庫連線數方法SQLServer資料庫
- 用VC程式來建立SQL Server ODBC資料來源 (轉)C程式SQLServer
- WebSphere中的Oracle資料來源設定------JSP和Oracle資料庫的連線 (轉)WebOracleJS資料庫
- SQL Server連線VFP資料庫的實現 (轉)SQLServer資料庫
- SQL Server連線ACCESS資料庫的實現 (轉)SQLServer資料庫
- Perl 連線 SQL Server (轉)SQLServer
- jsp連線資料庫大全(轉)JS資料庫
- 資料來源連線池
- 資料來源連線資料庫資料庫
- SQL Server埠更改後的資料庫連線方式(轉)SQLServer資料庫
- c#連線SQL Server資料庫C#SQLServer資料庫
- SQL server資料庫連線不上SQLServer資料庫
- SQL Server連線SQL Server、SQL Server連線ORACLE 連結伺服器SQLServerOracle伺服器
- 解決無法連線SQL Server資料庫的方法BWSQLServer資料庫
- Sql Server 強制斷開資料庫已有連線的方法SQLServer資料庫
- 用JSP連線mysql資料庫的方法JSMySql資料庫
- SQL Server 資料庫連線字串的宣告SQLServer資料庫字串
- 動態網頁(JSP 檔案)如何連線資料庫(SQL Server)--看這裡網頁JS資料庫SQLServer
- 連線SQL SERVER的公共類 (轉)SQLServer
- JSP連線資料庫JS資料庫
- jsp 資料連線池JS
- 連線SQL ServerSQLServer
- 關於jsp 呼叫bean 訪問sql-server資料庫問題JSBeanSQLServer資料庫
- 連線資料之遠端呼叫Access資料庫(轉)資料庫
- SQL Deverlop連線SQL ServerSQLdevServer
- SpringMVC連線多資料來源配置SpringMVC
- SQL SERVER 自連線、外連線SQLServer
- SQL Server 連線基礎知識(轉)SQLServer
- jsp連線資料庫大全JS資料庫
- 連線到資料庫(JSP)資料庫JS
- 用ASP.NET/C#連線Access和SQL Server資料庫 (轉)ASP.NETC#SQLServer資料庫
- [求助]JSP連線SQL Server 2000問題,看了很多資料還未解決,求救!JSSQLServer
- .NET連線SQL ServerSQLServer
- asp連線sql server 2008資料庫SQLServer資料庫
- Tomcat配置DataSource,連線池資料來源Tomcat
- JDBC---jsp資料庫連線JDBCJS資料庫