这人方法很简单,是jsp 调用sql server数据源的,并对查询及插入做了些处理,本方法适合于初学者
/*
* Created on 2002-8-30
* CopyRight by http://www.designac.org
*/
package org.DesignAC.database;
import java.sql.*;
import java.util.*;
import com.microsoft.jdbcx.sqlserver.SQLServerDataSource;
/**
* @author Biggie
*
* Class Function:数据库连接bean,取sql server JDBC数据源,这个数据源也可以用
* Tomcat,WebLogic,resin等提供
* 支持各种容器及平台
* @version 1.0A
* */
public class DBConnect {
file://You Host IP
private String strHostAddress="127.0.0.1";
file://Host Port
private int intHostPort=1433;
private String strUserName="sa";
private String strPassWord="";
file://DataBase Name
private String strDataName="dac";
file://Max Connection
private int intMaxConnection=10;
private Connection con=null;
private Statement stmt=null;
private ResultSet rs=null;
file://JDBC 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);
file://source.setHostProcess(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();
file://判断是否为空
if(!ArrayRs.isEmpty()){
ArrayRs.clear();
}
/*
* 将每条记录写入数组
* 将数组放在ArrayList里
*/
while(rs.next()){
String[] strArrayTemp=new String[numberOfColumns];
for(int i=0;i<numberOfColumns;i++){
if(rs.getObject(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;
}
}