java-oa系统怎么使用jdbc链接数据库调用查询方法最后在jsp上分页显示???

问题描述

oa系统怎么使用jdbc链接数据库调用查询方法最后在jsp上分页显示???

表名:users , 用户字段:realname

要求

效果:

解决方案

http://blog.csdn.net/aaabendan/article/details/5442144

解决方案二:

http://download.csdn.net/detail/zhai56565/5885775

解决方案三:

JdbcTemplate类

package com.cloudwebsoft.framework.db;

import cn.js.fan.db.ResultIterator;
import cn.js.fan.db.ResultWrapper;
import cn.js.fan.db.SQLFilter;
import cn.js.fan.util.StrUtil;
import cn.js.fan.web.Global;
import com.cloudwebsoft.framework.util.LogUtil;
import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Vector;
import org.apache.log4j.Logger;

public class JdbcTemplate
{
  int rowCount = 0;
  int colCount = 0;
  int pageSize = 10;
  public int curPage = 1;
  public long total = 0L;
  Logger logger;
  Connection connection = null;
  String connName;
  private boolean autoClose = true;

  public JdbcTemplate()
  {
    this.connection = new Connection(Global.getDefaultDB());
    this.connName = this.connection.connName;
    this.logger = Logger.getLogger(JdbcTemplate.class.getName());
  }

  public JdbcTemplate(Connection conn) {
    this.connection = conn;
    this.connName = this.connection.connName;
    this.logger = Logger.getLogger(JdbcTemplate.class.getName());
  }

  public JdbcTemplate(DataSource ds) {
    this.connection = ds.getConnection();
    this.connName = this.connection.connName;
    this.logger = Logger.getLogger(JdbcTemplate.class.getName());
  }

  public JdbcTemplate(DataSource ds, int curPage, int pageSize) {
    this.connection = ds.getConnection();
    this.connName = this.connection.connName;
    this.logger = Logger.getLogger(JdbcTemplate.class.getName());
    this.curPage = curPage;
    this.pageSize = pageSize;
  }

  public Connection getConnection() {
    return this.connection;
  }

  public long getTotal() {
    return this.total;
  }

  public int getColumnCount() {
    return this.colCount;
  }

  public int getRowCount() {
    return this.rowCount;
  }

  public boolean isAutoClose() {
    return this.autoClose;
  }

  public ResultIterator executeQuery(String sql)
    throws SQLException
  {
    ResultIterator ri = new ResultIterator();

    this.rowCount = 0;
    this.colCount = 0;
    ResultSet rs = null;
    Vector result = null;
    HashMap mapIndex = new HashMap();
    try
    {
      checkConnection();

      rs = this.connection.executeQuery(sql);
      if (rs == null) {
        return ri;
      }

      ResultSetMetaData rm = rs.getMetaData();
      this.colCount = rm.getColumnCount();
      for (int i = 1; i <= this.colCount; i++) {
        mapIndex.put(rm.getColumnName(i).toUpperCase(), new Integer(i));
      }
      result = new Vector();
      ResultWrapper rsw = new ResultWrapper(rs);
      while (rsw.next()) {
        Vector row = new Vector();
        for (int i = 0; i < this.colCount; i++)
          row.addElement(rsw.getObject(i + 1));
        result.addElement(row);
        this.rowCount += 1;
      }
    }
    finally
    {
      if (rs != null) {
        try {
          rs.close(); } catch (Exception e) {
        }
        rs = null;
      }

      if ((this.autoClose) && (this.connection.getAutoCommit())) {
        this.connection.close();
        this.connection = null;
      }
    }
    return new ResultIterator(result, mapIndex);
  }

  public ResultIterator executeQueryTFO(String sql)
    throws SQLException
  {
    ResultIterator ri = new ResultIterator();

    this.rowCount = 0;
    this.colCount = 0;
    ResultSet rs = null;
    Vector result = null;
    HashMap mapIndex = new HashMap();
    try
    {
      checkConnection();

      rs = this.connection.executeQueryTFO(sql);
      if (rs == null) {
        return ri;
      }

      ResultSetMetaData rm = rs.getMetaData();
      this.colCount = rm.getColumnCount();
      for (int i = 1; i <= this.colCount; i++) {
        mapIndex.put(rm.getColumnName(i).toUpperCase(), new Integer(i));
      }
      result = new Vector();
      ResultWrapper rsw = new ResultWrapper(rs);
      while (rsw.next()) {
        Vector row = new Vector();
        for (int i = 0; i < this.colCount; i++)
          row.addElement(rsw.getObject(i + 1));
        result.addElement(row);
        this.rowCount += 1;
      }
    }
    finally
    {
      if (rs != null) {
        try {
          rs.close(); } catch (Exception e) {
        }
        rs = null;
      }

      if ((this.autoClose) && (this.connection.getAutoCommit())) {
        this.connection.close();
        this.connection = null;
      }
    }
    return new ResultIterator(result, mapIndex);
  }

  public void addBatch(String sql) throws SQLException {
    this.connection.addBatch(sql);
  }

  public int[] executeBatch() throws SQLException {
    int[] r = null;
    try
    {
      checkConnection();
      r = this.connection.executeBatch();
    }
    finally {
      if ((this.autoClose) && (this.connection.getAutoCommit())) {
        this.connection.close();
        this.connection = null;
      }
    }
    return r;
  }

  public static void fillPreparedStatement(PreparedStatement ps, Object[] objectParams)
    throws SQLException
  {
    if (objectParams == null) {
      return;
    }
    int len = objectParams.length;
    for (int i = 1; i <= len; i++) {
      Object obj = objectParams[(i - 1)];

      if (obj == null)
      {
        ps.setNull(i, 12);
      }
      else if ((obj instanceof String))
        ps.setString(i, (String)obj);
      else if ((obj instanceof Integer))
        ps.setInt(i, ((Integer)obj).intValue());
      else if ((obj instanceof Date))
        ps.setTimestamp(i, new Timestamp(((Date)obj).getTime()));
      else if ((obj instanceof Timestamp))
        ps.setTimestamp(i, (Timestamp)obj);
      else if ((obj instanceof Long))
        ps.setLong(i, ((Long)obj).longValue());
      else if ((obj instanceof Short))
        ps.setShort(i, ((Short)obj).shortValue());
      else if ((obj instanceof Double))
        ps.setDouble(i, ((Double)obj).doubleValue());
      else if ((obj instanceof Float))
        ps.setFloat(i, ((Float)obj).floatValue());
      else if ((obj instanceof Clob))
        ps.setClob(i, (Clob)obj);
      else if ((obj instanceof Blob))
        ps.setBlob(i, (Blob)obj);
      else if ((obj instanceof Boolean))
        ps.setBoolean(i, ((Boolean)obj).booleanValue());
      else if ((obj instanceof Byte)) {
        ps.setByte(i, ((Byte)obj).byteValue());
      }
      else if ((obj instanceof BigDecimal)) {
        ps.setBigDecimal(i, (BigDecimal)obj);
      }
      else
        throw new SQLException("fillPreparedStatement: Object " + obj + " type is not supported. It's sequence number is " + i + " in parameters");
    }
  }

  public ResultIterator executeQuery(String sql, Object[] objectParams, int curPage, int pageSize)
    throws SQLException
  {
    ResultIterator ri = new ResultIterator();

    this.curPage = curPage;
    this.pageSize = pageSize;

    this.rowCount = 0;
    this.colCount = 0;

    ResultSet rs = null;
    Vector result = null;
    PreparedStatement ps = null;
    HashMap mapIndex = new HashMap();
    try
    {
      checkConnection();

      String countsql = SQLFilter.getCountSql(sql);
      ps = this.connection.prepareStatement(countsql);
      fillPreparedStatement(ps, objectParams);

      rs = this.connection.executePreQuery();
      if ((rs != null) && (rs.next())) {
        this.total = rs.getLong(1);
      }
      if (rs != null) {
        rs.close();
        rs = null;
      }
      if (ps != null) {
        ps.close();
        ps = null;
      }

      int totalpages = (int)Math.ceil(this.total / pageSize);
      if (curPage > totalpages)
        curPage = totalpages;
      if (curPage <= 0) {
        curPage = 1;
      }
      String listsql = sql;
      ps = this.connection.prepareStatement(sql);

      if (this.total != 0L) {
        this.connection.setMaxRows(curPage * pageSize);
      }
      fillPreparedStatement(ps, objectParams);
      rs = this.connection.executePreQuery();
      if (rs == null) {
        return ri;
      }

      ResultSetMetaData rm = rs.getMetaData();
      this.colCount = rm.getColumnCount();
      for (int i = 1; i <= this.colCount; i++)
      {
        mapIndex.put(rm.getColumnName(i).toUpperCase(), new Integer(i));
      }

      rs.setFetchSize(pageSize);

      int absoluteLocation = pageSize * (curPage - 1) + 1;

      if (!rs.absolute(absoluteLocation)) {
        return ri;
      }

      result = new Vector();

      ResultWrapper rsw = new ResultWrapper(rs);
      do {
        Vector row = new Vector();
        for (int i = 0; i < this.colCount; i++)
          row.addElement(rsw.getObject(i + 1));
        result.addElement(row);
        this.rowCount += 1;
      }while (rsw.next());
    }
    catch (SQLException e) {
      throw e;
    } finally {
      if (rs != null) {
        try {
          rs.close(); } catch (Exception e) {
        }
        rs = null;
      }
      if (ps != null) {
        try {
          ps.close();
        } catch (Exception e) {
        }
        ps = null;
      }
      if ((this.autoClose) && (this.connection.getAutoCommit())) {
        this.connection.close();
        this.connection = null;
      }
    }
    return new ResultIterator(result, mapIndex, this.total);
  }

  public ResultIterator executeQuery(String sql, Object[] objectParams, long total, int curPage, int pageSize)
    throws SQLException
  {
    ResultIterator ri = new ResultIterator();

    this.curPage = curPage;
    this.pageSize = pageSize;

    this.rowCount = 0;
    this.colCount = 0;

    ResultSet rs = null;
    Vector result = null;
    PreparedStatement ps = null;
    HashMap mapIndex = new HashMap();
    try
    {
      checkConnection();

      int totalpages = (int)Math.ceil(total / pageSize);
      if (curPage > totalpages)
        curPage = totalpages;
      if (curPage <= 0) {
        curPage = 1;
      }
      ps = this.connection.prepareStatement(sql);

      if (total != 0L) {
        this.connection.setMaxRows(curPage * pageSize);
      }
      fillPreparedStatement(ps, objectParams);
      rs = this.connection.executePreQuery();
      if (rs == null) {
        return ri;
      }

      ResultSetMetaData rm = rs.getMetaData();
      this.colCount = rm.getColumnCount();
      for (int i = 1; i <= this.colCount; i++)
      {
        mapIndex.put(rm.getColumnName(i).toUpperCase(), new Integer(i));
      }

      rs.setFetchSize(pageSize);

      int absoluteLocation = pageSize * (curPage - 1) + 1;

      if (!rs.absolute(absoluteLocation)) {
        return ri;
      }

      result = new Vector();

      ResultWrapper rsw = new ResultWrapper(rs);
      do {
        Vector row = new Vector();
        for (int i = 0; i < this.colCount; i++)
          row.addElement(rsw.getObject(i + 1));
        result.addElement(row);
        this.rowCount += 1;
      }while (rsw.next());
    }
    catch (SQLException e) {
      throw e;
    } finally {
      if (rs != null) {
        try {
          rs.close(); } catch (Exception e) {
        }
        rs = null;
      }
      if (ps != null) {
        try {
          ps.close();
        } catch (Exception e) {
        }
        ps = null;
      }
      if ((this.autoClose) && (this.connection.getAutoCommit())) {
        this.connection.close();
        this.connection = null;
      }
    }
    return new ResultIterator(result, mapIndex, total);
  }

  public ResultIterator executeQuery(String sql, Object[] objectParams)
    throws SQLException
  {
    ResultIterator ri = new ResultIterator();

    this.rowCount = 0;
    this.colCount = 0;

    ResultSet rs = null;
    Vector result = null;
    PreparedStatement ps = null;
    HashMap mapIndex = new HashMap();
    try
    {
      checkConnection();

      ps = this.connection.prepareStatement(sql);
      fillPreparedStatement(ps, objectParams);
      rs = this.connection.executePreQuery();
      if (rs == null) {
        return ri;
      }

      ResultSetMetaData rm = rs.getMetaData();
      this.colCount = rm.getColumnCount();
      for (int i = 1; i <= this.colCount; i++) {
        mapIndex.put(rm.getColumnName(i).toUpperCase(), new Integer(i));
      }

      result = new Vector();

      ResultWrapper rsw = new ResultWrapper(rs);
      while (rsw.next()) {
        Vector row = new Vector();
        for (int i = 0; i < this.colCount; i++) {
          try {
            row.addElement(rsw.getObject(i + 1));
          }
          catch (SQLException e) {
            row.addElement(null);
            LogUtil.getLog(getClass()).error(StrUtil.trace(e));
          }
        }
        result.addElement(row);
        this.rowCount += 1;
      }
    }
    catch (SQLException e) {
      throw e;
    } finally {
      if (rs != null) {
        rs.close();
        rs = null;
      }
      if (ps != null) {
        ps.close();
        ps = null;
      }
      if ((this.autoClose) && (this.connection.getAutoCommit())) {
        this.connection.close();
        this.connection = null;
      }
    }
    return new ResultIterator(result, mapIndex);
  }

  public ResultIterator executeQueryTFO(String sql, Object[] objectParams)
    throws SQLException
  {
    ResultIterator ri = new ResultIterator();

    this.rowCount = 0;
    this.colCount = 0;

    ResultSet rs = null;
    Vector result = null;
    PreparedStatement ps = null;
    HashMap mapIndex = new HashMap();
    try
    {
      checkConnection();

      ps = this.connection.prepareStatementTFO(sql);
      fillPreparedStatement(ps, objectParams);
      rs = this.connection.executePreQuery();
      if (rs == null) {
        return ri;
      }

      ResultSetMetaData rm = rs.getMetaData();
      this.colCount = rm.getColumnCount();
      for (int i = 1; i <= this.colCount; i++) {
        mapIndex.put(rm.getColumnName(i).toUpperCase(), new Integer(i));
      }

      result = new Vector();

      ResultWrapper rsw = new ResultWrapper(rs);
      while (rsw.next()) {
        Vector row = new Vector();
        for (int i = 0; i < this.colCount; i++) {
          try {
            row.addElement(rsw.getObject(i + 1));
          }
          catch (SQLException e) {
            row.addElement(null);
            LogUtil.getLog(getClass()).error(StrUtil.trace(e));
          }
        }
        result.addElement(row);
        this.rowCount += 1;
      }
    }
    catch (SQLException e) {
      throw e;
    } finally {
      if (rs != null) {
        rs.close();
        rs = null;
      }
      if (ps != null) {
        ps.close();
        ps = null;
      }
      if ((this.autoClose) && (this.connection.getAutoCommit())) {
        this.connection.close();
        this.connection = null;
      }
    }
    return new ResultIterator(result, mapIndex);
  }

  public ResultIterator executeQuery(String sql, int curPage, int pageSize)
    throws SQLException
  {
    ResultIterator ri = new ResultIterator();

    this.curPage = curPage;
    this.pageSize = pageSize;

    this.rowCount = 0;
    this.colCount = 0;

    ResultSet rs = null;
    Vector result = null;
    HashMap mapIndex = new HashMap();
    try
    {
      checkConnection();

      String countsql = SQLFilter.getCountSql(sql);

      rs = this.connection.executeQuery(countsql);
      if ((rs != null) && (rs.next())) {
        this.total = rs.getLong(1);
      }
      if (rs != null) {
        rs.close();
        rs = null;
      }

      int totalpages = (int)Math.ceil(this.total / pageSize)+1;
      if (curPage > totalpages)
        curPage = totalpages;
      if (curPage <= 0) {
        curPage = 1;
      }
      if (this.total != 0L) {
        this.connection.setMaxRows(curPage * pageSize);
      }
      rs = this.connection.executeQuery(sql);
      if (rs == null) {
        return ri;
      }

      ResultSetMetaData rm = rs.getMetaData();
      this.colCount = rm.getColumnCount();
      for (int i = 1; i <= this.colCount; i++)
      {
        mapIndex.put(rm.getColumnName(i).toUpperCase(), new Integer(i));
      }

      rs.setFetchSize(pageSize);

      int absoluteLocation = pageSize * (curPage - 1) + 1;

      if (!rs.absolute(absoluteLocation)) {
        return ri;
      }

      result = new Vector();

      ResultWrapper rsw = new ResultWrapper(rs);
      do {
        Vector row = new Vector();
        for (int i = 1; i <= this.colCount; i++)
          row.addElement(rsw.getObject(i));
        result.addElement(row);
        this.rowCount += 1;
      }while (rsw.next());
    }
    finally {
      if (rs != null) {
        try {
          rs.close(); } catch (Exception e) {
        }
        rs = null;
      }
      if ((this.autoClose) && (this.connection.getAutoCommit())) {
        this.connection.close();
        this.connection = null;
      }
    }
    return new ResultIterator(result, mapIndex, this.total);
  }

  public int executeUpdate(String sql)
    throws SQLException
  {
    int r = 0;
    try
    {
      checkConnection();
      r = this.connection.executeUpdate(sql);
    } finally {
      if ((this.autoClose) && (this.connection.getAutoCommit())) {
        this.connection.close();
        this.connection = null;
      }
    }
    return r;
  }

  public int executeUpdate(String sql, Object[] objectParams)
    throws SQLException
  {
    int r = 0;
    PreparedStatement ps = null;
    try
    {
      checkConnection();
      ps = this.connection.prepareStatement(sql);
      fillPreparedStatement(ps, objectParams);
      r = this.connection.executePreUpdate();
    }
    finally {
      if (ps != null) {
        try {
          ps.close();
        } catch (Exception e) {
        }
        ps = null;
      }
      if ((this.autoClose) && (this.connection.getAutoCommit())) {
        this.connection.close();
        this.connection = null;
      }
    }
    return r;
  }

  public void beginTrans() throws SQLException {
    this.connection.beginTrans();
  }

  public void commit() throws SQLException
  {
    if (this.connection != null)
      this.connection.commit();
  }

  public void rollback() {
    this.connection.rollback();
  }

  public void close()
  {
    if (this.connection != null)
      this.connection.close();
  }

  public boolean isClosed() {
    if (this.connection == null) {
      return true;
    }
    return this.connection.isClosed();
  }

  public void checkConnection()
    throws SQLException
  {
    if (isClosed())
      this.connection = new Connection(this.connName);
  }

  public void setAutoClose(boolean autoClose)
  {
    this.autoClose = autoClose;
  }
}

解决方案四:

JSP访问数据库分页显示

时间: 2024-11-08 22:08:59

java-oa系统怎么使用jdbc链接数据库调用查询方法最后在jsp上分页显示???的相关文章

JDBC链接数据库的几个步骤_java

此文列出了JDBC链接数据库的4个步骤,供大家参考下: JDBC:JAVA访问数据库的解决方案. 几个步骤:1.加载驱动类; 2.与数据库建立连接:            3.执行SQL语句    4.处理结果集    5.关闭连接 1. 第一步:加载驱动类: 需要注意:不同的数据库,参照的字符串不同,ORACLE的连接为:Class.forName("oracle.jdbc.driver.OracleDriver"); 这一步执行后,程序可能会抛出: ClassNotFoundExc

使用JDBC从数据库中查询数据的方法_Mysql

* ResultSet 结果集:封装了使用JDBC 进行查询的结果 * 1. 调用Statement 对象的 executeQuery(sql) 方法可以得到结果集 * 2. ResultSet 返回的实际上就是一张数据表,有一个指针指向数据表的第一行的前面, * 可以调用next()方法检测下一行是否有效,若有效,返回true,且指针下移, * 相当于iterator 对象的 hasNext() 和 next()方法的结合体 * 3. 当指针定位到一行时,可以通过调用getXxx(index)

yii数据库的查询方法_php实例

本文实例讲述了yii数据库的查询方法.分享给大家供大家参考,具体如下: 这里介绍两种查询方法.一种是直接查询,一种是使用借助criteria实现查询. 复制代码 代码如下: $user=User::model(); 1. 直接查询: $arr=array( "select"=>"username,password,email", //要查询的字段 "condition"=>"username like '%6'",

使用JDBC对数据库进行查询的前期准备工作,以及简单的JDBC访问MySQL数据库(Mac)

首先JDBC的前期数据库数据准备: 1,打开链接好MySQL的Workbench软件,新建一个数据库: 2.然后打开数据库对应的代码编辑窗口,建立表和插入数据记录: 3.然后是打开关于javaWeb编程的Eclipse,然后把数据库链接的jar包拷贝进去,然后加载jar文件 4.然后开始进行JDBC编程: 1 import java.sql.DriverManager; 2 import java.sql.ResultSet; 3 import java.sql.Connection; 4 im

JSP网页分页显示数据库的内容

下面是我的jsp连接mysql数据库的代码,现在有的数据库的条目有1000多个,我想分页显示,每页显示200个,请问怎么改才能实现?   <!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>alternative splicing ofEST</TITLE> <METANAME="Generator

jdbc 链接数据库(链接以后再指定数据库名)

问题描述 我现在想实现导入数据到数据库的功能:1.先链接到sqlserver2.然后选择要导入的数据库名3.导入数据Connectioncon=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=Shape2DB","sa","sa");上面的DatabaseName以及指定了数据库名为Shape2DB实际上我想链接到sqlserver以后通过下面的语

dbutils java 数据库-dbutils查询方法的问题

问题描述 dbutils查询方法的问题 dbutils查询,想实现把实体类封装出来 解决方案 http://tompig.iteye.com/blog/1450756 解决方案二: http://www.oschina.net/question/235076_53536 解决方案三: mongodob查询方法

jdbc链接远程数据库进行修改url操作_java

链接远程数据库的时候,要把获得链接的url进行修改,本文分享了具体代码,供大家参考,具体内容如下 package com.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * jdbc链接远程数据库(公司的176库)操作 * * @Author

ap plet中应用JDBC访问数据库

  ap plet是用Java语言编写的小应用程序,它能够嵌入在HTML中,并由WWW浏览器来解释执行.但是,如何在ap plet中处理Internet世界中大量的数据和分布在网络各个角落的各种各样的资源呢?这就要使用JDBC. 一. JDBC的工作原理 JDBC(Java DataBase Connectivity)是用于执行SQL语句的Java应用程序接口,由一组用Java语言编写的类与接口组成.JDBC是一种规范,它让各数据库厂商为Java程序员提供标准的数据库访问类和接口,这样就使得独立