数据库的相关操作:如连接、查询、添加、删除、修改、分页显示

2016-02-19 18:07 4 1 收藏

图老师小编精心整理的数据库的相关操作:如连接、查询、添加、删除、修改、分页显示希望大家喜欢,觉得好的亲们记得收藏起来哦!您的支持就是小编更新的动力~

【 tulaoshi.com - Web开发 】

  数据库的相关操作:如连接、查询、添加、删除、修改、分页显示
   
  package study.database;

  /**
  * pTitle: JSP模式学习/p
  * pDescription: 数据库的相关操作:如连接、查询、添加、删除、修改/p
  * pCopyright: Copyright (c) 2004/p
  * pCompany: /p
  * @author 李艳生
  * @version 1.0
  */
  import java.sql.*;
  import java.io.*;
  import java.util.*;

  public class Operation {
  //数据库驱动程序
  private String strDriver = "";
  //数据库连接字符串
  private String strURL = "";
  //数据库用户名
  private String username = "";
  //数据库密码
  private String password = "";

  private Connection conn = null;
  private Statement stmt = null;
  ResultSet rs = null;

  /** 读到数据库配置信息
  */
  private void loadProp(){
  InputStream is = getClass().getResourceAsStream("/setup.txt");
  Properties props = new Properties();

  try{
  props.load(is);
  }catch(Exception e){
  System.err.println("不能读取配置文件. 请确保setup.txt在classes指定的路径中");
  }

  Enumeration propNames = props.propertyNames();
  while (propNames.hasMoreElements()) {
  String name = (String) propNames.nextElement();
  if (name.endsWith(".driver")) {
  String poolName = name.substring(0, name.lastIndexOf("."));
  strDriver = props.getProperty(poolName + ".driver");
  strURL = props.getProperty(poolName + ".url");
  username = props.getProperty(poolName + ".user");
  password = props.getProperty(poolName + ".password");
  }
  }
  }

  /** 在创建Operation对象时连接数据库
  */
  public Operation() {
  //读到数据库配置信息
  loadProp();

  try{
  Class.forName(strDriver);
  }catch(java.lang.ClassNotFoundException e) {
  System.err.println("数据库连接错误:" + e.getMessage());
  }

  try{
  conn=DriverManager.getConnection(strURL,username,password);
  }catch(SQLException ex) {
  System.err.println("数据库连接错误:" + ex.getMessage());
  }
  }

  /** 数据库查询
  * sql:SQL查询语句
  */
  public ResultSet query(String sql) {
  try{
  stmt=conn.createStatement();
  rs=stmt.executeQuery(sql);
  }catch(SQLException ex) {
  System.err.println("数据库查询错误:" + ex.getMessage());
  }

  return rs;
  }

  /** 数据库添加、修改、删除
  * sql:SQL语句
  */
  public void update(String sql) {
  try{
  stmt=conn.createStatement();
  stmt.executeUpdate(sql);
  }catch(SQLException ex) {
  System.err.println("数据库更新错误:"+ex.getMessage());
  }
  }

  /** 得到查询结果的总记录数
  * rs:查询结果集
  */
  public int totalRecord(ResultSet rs) throws Exception{
  int total=0;
  //指针移到最后一条记录上
  rs.last();
  total = rs.getRow();
  rs.first();

  return total;
  }

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

  /** 分页显示
  * currPage: 当前页数
  * pageSize: 页大小
  * pageCount: 总页数
  * filename: 使用分页的文件名(文件名后面要加?,多个参数以&分开,有参数的最后以&结束)
  * http://school.cnd8.com/ 返回字符串: a href="filename?page=当前页数"
  */
  public String showPages(int currPage, int pageSize, int pageCount, String filename){
  String addr;

  addr = "table width=100% border=0 align=center cellpadding=0 cellspacing=0form method=Post name=pageformtrtddiv align=right当前第strongfont color=red" + currPage + "/font/strong页 " +
  "共strongfont color=red" + pageCount + "/font/strong页每页strongfont color=red" + pageSize + "/font/strong条 ";

  if(currPage pageCount){
  currPage = pageCount;
  }
  if(currPage 1){
  currPage = 1;
  }

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

  if(currPage 2){
  addr += "首 页 上一页 ";
  }
  else{
  addr += "a href=" + filename + "page=1首 页/a ";
  addr += "a href=" + filename + "page=" + (currPage - 1) + "上一页/a ";
  }

  if(currPage = pageCount){
  addr += "下一页 尾 页 ";
  }
  else{
  addr += "a href=" + filename + "page=" + (currPage + 1) + "下一页/a ";
  addr += "a href=" + filename + "page=" + pageCount + "尾 页/a ";
  }

  addr += "转到:select name=´page´ size=´1´ style=´font-size: 9pt´ onChange=´javascript :pageform.submit()´ ";
  for(int i = 1; i = pageCount; i ++){
  if(currPage==i){
  addr += "option value=" + i + " selected 第 "+i+"页 /option ";

  }
  else{
  addr += "option value=" + i + " 第 "+i+"页 /option ";
  }
  }
  addr += "/select/div/td/tr/form/table";

  return addr;
  }

  /** 关闭数据集
  */
  public void closestmt() {
  try{
  stmt.close();
  }catch(SQLException ex) {
  System.err.println("数据集关闭错误:"+ex.getMessage());
  }
  }

  /** 关闭数据库连接
  */
  public void closeconn() {
  try{
  conn.close();
  }catch(SQLException ex) {
  System.err.println("数据库连接关闭错误:"+ex.getMessage());
  }
  }
  }

  其中的JSP页面中的代码大家自己动手写!很容易的!
  http://blog.csdn.net/goldbox/archive/2007/01/26/1494667.aspx

来源:https://www.tulaoshi.com/n/20160219/1616831.html

延伸阅读
标签: PHP
  4.据库连接 通过PHP你可以轻松的连接到数据库,请求数据并将其显示在你的web站点中,甚至修改数据库中的数据。MySQL是一种很流行的数据库,并且在互联网中有许多有关PHP与MySQL的教程。MySQL是免费的,这一点也许就吸引了不少人。由于其广泛应用,我就不想在这里赘述MySQL的使用方法了。Oracle被大量在企业应用中采用,因此我们就利用O...
海量数据库的查询优化及分页算法方案  原出处不详 摘自:www.21php.com 随着“金盾工程”建设的逐步深入和公安信息化的高速发展,公安计算机应用系统被广泛应用在各警种、各部门。与此同时,应用系统体系的核心、系统数据的存放地――数据库也随着实际应用而急剧膨胀,一些大规模的系统,如人口系统的数据甚至超过了1000万条,可谓海量。...
标签: Web开发
<?php /********************************************* TOracleViewPagev 2.0 日期:2000-9-23 分页显示Oracle数据库记录的类 更新日期:2000-10-19 增加显示TopRecord的功能,允许第一页显示的记录数与其它页不同。 作者:sharetop email:ycshowtop@21cn.com ***********************************************/ class TOr...
数据库中的数据要最终显示给用户,就要使用数据约束控件,比如前面所使用的文本框以及VB所提供的其他普通约束数据控件,VB中还提供了多种高级约束数据控件,其中包括:高级约束数据网格控件(DBGrid)、高级约束列表控件(DBList)和高级约束组合框控件(DBCombo)。在默认的工具箱中,尚未加入这些控件,要使用它们,首先要先引用它们:右键点击工...
标签: SQLServer
  数据库系统是管理信息系统的核心,基于数据库的联机事务处理(OLTP)以及联机分析处理(OLAP)是银行、企业、政府等部门最为重要的计算机应用之一。从大多数系统的应用实例来看,查询操作在各种数据库操作中所占据的比重最大,而查询操作所基于的SELECT语句在SQL语句中又是代价最大的语句。举例来说,如果数据的量积累到一定的程度,比如...

经验教程

746

收藏

98
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部