jsp:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<jsp:useBean id="con" scope="page" class="sist.DB" />
bean:
package sist;
import java.sql.*;
import java.io.*;
//import java.util.*;
public class DB {
private Connection conn = null;
private Statement drpStmt = null;
private ResultSet drpRst = null;
private int countInt = 0;
private String strDBError="";
boolean autoCommit;
public DB() {//构造函数开始
try{
//jdbc-odbc
Class.forName("org.gjt.mm.mysql.Driver");
//设置连接数据库信息
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/gordon?useUnicode=true&characterEncoding=gbk");
drpStmt = conn.createStatement();
}
catch(SQLException sqle){
countInt=-1;
strDBError=sqle.getMessage();
}
catch(java.lang.Exception e){
countInt=-2;
strDBError=e.getMessage();
}
}
//构造函数的结束
//构造函数的作用,当创建该类的对象时,将自动调用构造函数,并运行其中的方法,对于本程序来说,当创建本类的对象时,将自动连接数据库
//-------------------------------------------------------------------
public int getErrorCode(){
return countInt;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
public String getErrorInfo(){
return strDBError;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//Q方法开始
//这个方法的返回值类型为结果集,主要完成数据库的查询操作,在使用的时候要向该方法传递SQL字符串
public ResultSet q(String queryString){
countInt=0;
strDBError="";
try{
drpRst = drpStmt.executeQuery(queryString);
countInt=1;
}catch(SQLException sqly){
countInt=-1;
strDBError+="Error occur while useing dataConn.exeQuery(queryString)!<p>The queryString is <p>" + queryString + "<p>The Error Information from DBMS锟斤拷<p>"+sqly.getMessage();
}catch(java.lang.Exception y){
countInt=-2;
strDBError+="<p>java.lang.Exception:"+y.getMessage();
}
return drpRst;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
public int u(String updateString){
countInt=0;
strDBError="";
try{
countInt = drpStmt.executeUpdate(updateString);
}catch(SQLException sqlz){
countInt=-1;
strDBError="Error occur while using dataConn.exeUpdate()!The SQL is:<p>" + updateString + "<p>执行失败:<P>" + sqlz.getMessage();
}
return countInt;
}
//--------------------------------------------------------------------
public int getRowCount(String queryString){
countInt=0;
strDBError="";
ResultSet rs;
int nRowCount=0;
try{
rs=drpStmt.executeQuery("select count(*) from (" + queryString + ") as viewTempQueryString" );
while(rs.next()) nRowCount=rs.getInt(1);
}
catch(SQLException errGetRowCount){
countInt=-1;
strDBError+="Error occur while useing dataConn.getRowCount("+queryString+")! " + errGetRowCount.getMessage();
}catch(java.lang.Exception errOther){
countInt=-2;
strDBError+="" + errOther.getMessage();
}
return nRowCount;
}
//-----------------------------------------------------------
}//end of all
bean位置在C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT\jc\WEB-INF\classes\sist里面
谢谢~~!