conn.jsp
<%
String sDBDriver = "COM.ibm.db2.jdbc.app.DB2Driver";
String sConnStr = "jdbc:db2:faq";
Connection conn = null;
Statement stmt = null;
ResultSet rs=null;
try {
Class.forName(sDBDriver); 
}
catch(java.lang.ClassNotFoundException e) {
out.print("faq(): " + e.getMessage());
}try{
conn = DriverManager.getConnection(sConnStr,"wsdemo","wsdemo1"); 
stmt = conn.createStatement();
}catch(SQLException e){
out.print(e.toString());
}
%>query.jsp<%@ page language="java" import="java.sql.*" %>
<%@ page contentType="text/html; charset=gb2312" %>
<%@ include file="conn.jsp" %>
<%
......
int pages=0;
int pagesize=10;
ResultSet result = null;
ResultSet rcount = null;pages = new Integer(request.getParameter("pages")).intValue();if (pages>0)
{String sql=" state='我不傻'";
int count=0;
try {
rcount = stmt.executeQuery("SELECT count(id) as id from user where "+sql);
catch(SQLException ex) {
out.print("aq.executeQuery: " + ex.getMessage());
}
if(rcount.next())
count = rcount.getInt("id");
rcount.close();
if (count>0)
{
sql="select * from user where "+sql;
try {
result = stmt.executeQuery(sql);
}
catch(SQLException ex) {
out.print("aq.executeQuery: " + ex.getMessage());
}
int i;
String name;
// result.first();
// result.absolute((pages-1)*pagesize);
// 此方法jdbc2.0支持。编译通过,但执行不过,不知是不是跟驱动有关,只好用下面的笨办法。
for(i=1;i<=(pages-1)*pagesize;i++)
result.next();
for(i=1;i<=pagesize;i++) {
if (result.next()) {
name=result.getString("name");
out.print(name);
}
result.close();
int n= (int)(count/pagesize);
if (n*pagesize<count) n++;
if (n>1)
{for(i=1;i<=n;i++)
out.print("<a href=query.jsp?pages="+i+">"+i+"&nbsp;</a>"); } } } %> 
 

解决方案 »

  1.   

    /*
    /*@ author:yjx
    /*@ date:2001-7-9
    *//*import package file*/
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.sql.*;
    import java.util.*;
    import java.lang.*;public class Loginservlet extends HttpServlet
    {
    public void doPost(HttpServletRequest req,
                        HttpServletResponse resp)
        throws ServletException,java.io.IOException
        {
    PrintWriter out;
    String          strUserName,strPWD;
    String          strDBDriver,strURL;
    String          strSQL,strTemp;
    Connection      conn=null;
    Statement       stmt=null;
    ResultSet       rs=null;
    int             iExist=0;//0:not exits,1:exits
    HttpSession     session; session=req.getSession();
            out = resp.getWriter(); //get parameters
    strUserName=req.getParameter("username");
         strPWD=req.getParameter("password");        //set database connection parameter value
    strDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
    strURL="jdbc:odbc:wygl";        if(strUserName!=null || strUserName.compareTo("")!=0){
    //instance session variable:
    session.putValue("login_name","");
    session.putValue("user_name","");
    session.putValue("dept_id","");
    session.putValue("duty","");
    session.putValue("mright","");
    session.putValue("uitype","");
    session.putValue("logined","false");
    session.putValue("info","null");
        
    //connect database
    try{
    Class.forName(strDBDriver);
    }catch(java.lang.ClassNotFoundException e){
    System.err.println("error:"+e.getMessage());
    } try{
    conn=DriverManager.getConnection(strURL);
    stmt=conn.createStatement();
    }catch(SQLException e){
    System.out.println("error:"+e.getMessage());
    }
                strSQL="select * from sys_user where login_name='"+strUserName+"' and user_pwd='"+strPWD+"'"; try{
    rs=stmt.executeQuery(strSQL);
    if(rs.next()){
    session.putValue("login_name",strUserName); session.putValue("logined","true"); strTemp=rs.getString("user_name");
    session.putValue("user_name",strTemp); strTemp=rs.getString("dept_id");
    session.putValue("dept_id",strTemp);

    strTemp=rs.getString("duty");
    session.putValue("duty",strTemp); strTemp=rs.getString("mright");
    session.putValue("mright",strTemp); strTemp=rs.getString("uitype");
    session.putValue("uitype",strTemp); resp.sendRedirect("/wygl/jsp/sysindex.jsp");
    }else{
    resp.sendRedirect("/wygl/jsp/errorindex.jsp");
    }
    }catch(SQLException e){
    System.err.println("error:"+e.getMessage());
    }
    }
    }
    }
      

  2.   

    另外一个例子:
    /*
    * @dbbean.java
    * Author:nicolas
    * Date:2001-3-21
    * version:1.0v
    * Function:connect database,query table,update table by parameter
    */
    package dbbean;
    import java.sql.*;
    import java.io.*;public class dbbean{
    //String sConnStr="jdbc:oracle:thin:@172.16.1.2:1521:labora","scott","tiger";
    String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
    Connection conn=null;
    ResultSet rs=null;
        
    /*
        * constructor:try to create a dababase driver
      */
    public dbbean(){
    try{
    Class.forName(sDBDriver);
    }catch(java.lang.ClassNotFoundException e){
    System.err.println("dbbean():  "+e.getMessage());
    }
    } public ResultSet executeQuery(String sqlStr){
    rs=null;
    try{
    conn=DriverManager.getConnection("jdbc:odbc:wygl");
    Statement stmt=conn.createStatement();
    rs=stmt.executeQuery(sqlStr);
    }catch(SQLException e){
    System.err.println("executeQuery():  "+e.getMessage());
    }
    return rs;
    } public int executeInsert(String sqlStr){
    try{
    conn=DriverManager.getConnection("jdbc:odbc:wygl");
    Statement stmt=conn.createStatement();
    stmt.executeUpdate(sqlStr);
    return 1;//成功
    }catch(SQLException e){
    System.err.println("executeInsert():  "+e.getMessage());
    return 0;//失败
    }
    }    public int executeUpdate(String sqlStr){
    try{
    conn=DriverManager.getConnection("jdbc:odbc:wygl");
    Statement stmt=conn.createStatement();
    stmt.executeUpdate(sqlStr);
    return 1;//成功
    }catch(SQLException e){
    System.err.println("executeInsert():  "+e.getMessage());
    return 0;//失败
    }
    } public int executeDelete(String sqlStr){
    try{
    conn=DriverManager.getConnection("jdbc:odbc:wygl");
    Statement stmt=conn.createStatement();
    stmt.executeUpdate(sqlStr);
    return 1;//成功
    }catch(SQLException e){
    System.err.println("executeDelete():  "+e.getMessage());
    return 0;//失败
    }
    }
    }
      

  3.   

    另外一个例子:
    /*
    * @dbbean.java
    * Author:nicolas
    * Date:2001-3-21
    * version:1.0v
    * Function:connect database,query table,update table by parameter
    */
    package dbbean;
    import java.sql.*;
    import java.io.*;public class dbbean{
    //String sConnStr="jdbc:oracle:thin:@172.16.1.2:1521:labora","scott","tiger";
    String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
    Connection conn=null;
    ResultSet rs=null;
        
    /*
        * constructor:try to create a dababase driver
      */
    public dbbean(){
    try{
    Class.forName(sDBDriver);
    }catch(java.lang.ClassNotFoundException e){
    System.err.println("dbbean():  "+e.getMessage());
    }
    } public ResultSet executeQuery(String sqlStr){
    rs=null;
    try{
    conn=DriverManager.getConnection("jdbc:odbc:wygl");
    Statement stmt=conn.createStatement();
    rs=stmt.executeQuery(sqlStr);
    }catch(SQLException e){
    System.err.println("executeQuery():  "+e.getMessage());
    }
    return rs;
    } public int executeInsert(String sqlStr){
    try{
    conn=DriverManager.getConnection("jdbc:odbc:wygl");
    Statement stmt=conn.createStatement();
    stmt.executeUpdate(sqlStr);
    return 1;//成功
    }catch(SQLException e){
    System.err.println("executeInsert():  "+e.getMessage());
    return 0;//失败
    }
    }    public int executeUpdate(String sqlStr){
    try{
    conn=DriverManager.getConnection("jdbc:odbc:wygl");
    Statement stmt=conn.createStatement();
    stmt.executeUpdate(sqlStr);
    return 1;//成功
    }catch(SQLException e){
    System.err.println("executeInsert():  "+e.getMessage());
    return 0;//失败
    }
    } public int executeDelete(String sqlStr){
    try{
    conn=DriverManager.getConnection("jdbc:odbc:wygl");
    Statement stmt=conn.createStatement();
    stmt.executeUpdate(sqlStr);
    return 1;//成功
    }catch(SQLException e){
    System.err.println("executeDelete():  "+e.getMessage());
    return 0;//失败
    }
    }
    }