国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

數(shù)據(jù)庫的相關操作:如連接、查詢、添加、刪除、修改、分頁顯示

2019-11-18 16:42:00
字體:
供稿:網(wǎng)友

數(shù)據(jù)庫的相關操作:如連接、查詢、添加、刪除、修改、分頁顯示
 
package study.database;

/**
* <p>Title: jsp模式學習</p>
* <p>Description: 數(shù)據(jù)庫的相關操作:如連接、查詢、添加、刪除、修改</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author 李艷生
* @version 1.0
*/
import java.sql.*;
import java.io.*;
import java.util.*;

public class Operation {
//數(shù)據(jù)庫驅(qū)動程序
PRivate String strDriver = "";
//數(shù)據(jù)庫連接字符串
private String strURL = "";
//數(shù)據(jù)庫用戶名
private String username = "";
//數(shù)據(jù)庫密碼
private String passWord = "";

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

/** 讀到數(shù)據(jù)庫配置信息
*/
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");
}
}
}

/** 在創(chuàng)建Operation對象時連接數(shù)據(jù)庫
*/
public Operation() {
//讀到數(shù)據(jù)庫配置信息
loadProp();

try{
Class.forName(strDriver);
}catch(java.lang.ClassNotFoundException e) {
System.err.println("數(shù)據(jù)庫連接錯誤:" + e.getMessage());
}

try{
conn=DriverManager.getConnection(strURL,username,password);
}catch(SQLException ex) {
System.err.println("數(shù)據(jù)庫連接錯誤:" + ex.getMessage());
}
}

/** 數(shù)據(jù)庫查詢
* sql:SQL查詢語句
*/
public ResultSet query(String sql) {
try{
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException ex) {
System.err.println("數(shù)據(jù)庫查詢錯誤:" + ex.getMessage());
}

return rs;
}

/** 數(shù)據(jù)庫添加、修改、刪除
* sql:SQL語句
*/
public void update(String sql) {
try{
stmt=conn.createStatement();
stmt.executeUpdate(sql);
}catch(SQLException ex) {
System.err.println("數(shù)據(jù)庫更新錯誤:"+ex.getMessage());
}
}

/** 得到查詢結(jié)果的總記錄數(shù)
* rs:查詢結(jié)果集
*/
public int totalRecord(ResultSet rs) throws Exception{
int total=0;
//指針移到最后一條記錄上
rs.last();
total = rs.getRow();
rs.first();

return total;
}

/** 分頁顯示
* currPage: 當前頁數(shù)
* pageSize: 頁大小
* pageCount: 總頁數(shù)
* filename: 使用分頁的文件名(文件名后面要加?,多個參數(shù)以&分開,有參數(shù)的最后以&結(jié)束)
* http://www.survivalescaperooms.com/ 返回字符串: <a href="filename?page=當前頁數(shù)">
*/
public String showPages(int currPage, int pageSize, int pageCount, String filename){
String addr;

addr = "<table width=100% border=0 align=center cellpadding=0 cellspacing=0><form method=Post name=pageform><tr><td><div align=right>當前第<strong><font color=red>" + currPage + "</font></strong>頁 " +
"共<strong><font color=red>" + pageCount + "</font></strong>頁每頁<strong><font color=red>" + pageSize + "</font></strong>條 ";

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

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 += "轉(zhuǎn)到:<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;
}

/** 關閉數(shù)據(jù)集
*/
public void closestmt() {
try{
stmt.close();
}catch(SQLException ex) {
System.err.println("數(shù)據(jù)集關閉錯誤:"+ex.getMessage());
}
}

/** 關閉數(shù)據(jù)庫連接
*/
public void closeconn() {
try{
conn.close();
}catch(SQLException ex) {
System.err.println("數(shù)據(jù)庫連接關閉錯誤:"+ex.getMessage());
}
}
}

其中的JSP頁面中的代碼大家自己動手寫!很容易的!
http://blog.csdn.net/goldbox/archive/2007/01/26/1494667.aspx


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 汽车| 平远县| 河北省| 米泉市| 如皋市| 沂源县| 舟山市| 石家庄市| 方山县| 锦州市| 新泰市| 嵊州市| 南木林县| 贡觉县| 安徽省| 江安县| 昌黎县| 新乡县| 玛纳斯县| 运城市| 东平县| 汽车| 礼泉县| 黄浦区| 吉木萨尔县| 新绛县| 海伦市| 疏附县| 余干县| 三门峡市| 武强县| 历史| 喀喇沁旗| 墨竹工卡县| 塔城市| 资溪县| 峨眉山市| 左权县| 满洲里市| 邵东县| 高雄市|