📄 gbdao.java~24~
字号:
package com.netbar.dao;
import java.sql.*;
import com.netbar.dao.*;
import com.netbar.bean.*;
import java.util.Vector;
public class gbDao {
public gbDao() {
}
public static int getgbRecords() {
int count = 0;
PreparedStatement pstmt = null;
String countSql="select count(*) from guestbook";
Connection con = com.netbar.dao.databaseOprate.getConnection();
try {
pstmt = con.prepareStatement(countSql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
count = rs.getInt(1);
//System.out.println("得到记录数:"+count);
}
return count;
} catch (Exception ex) {
System.out.println("获取留言记录条数时出错。");
ex.printStackTrace();
return 0;
}
}
//获取留言表中的所有数据,并以留言表Bean对象数组返回。
public com.netbar.bean.gbBean[] getAlldata() {
Connection con = com.netbar.dao.databaseOprate.getConnection();
PreparedStatement pstmt = null;
String sql = "select * from guestbook order by gbTime desc";
int count=this.getgbRecords();
gbBean[] gbbean = new gbBean[count];
for (int i = 0; i < count; i++) {
gbbean[i] = new gbBean();
}
try {
pstmt = con.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
int i = 0;
while (rs.next()) {
gbBean gb = new gbBean();
gb.setGbID(rs.getInt(1));
gb.setGbTitle(rs.getString(2));
gb.setGbContent(rs.getString(3));
gb.setGbReply(rs.getString(4));
gb.setGbPerson(rs.getString(5));
gb.setGbWeb(rs.getString(6));
gb.setGbMail(rs.getString(7));
gb.setGbQQ(rs.getString(8));
gb.setGbFace(rs.getString(9));
gb.setGbIP(rs.getString(10));
gb.setGbPcname(rs.getString(11));
gb.setGbTime(rs.getString(12));
gb.setGbRedate(rs.getString(13));
gbbean[i] = gb;
i++;
}
rs.close();
pstmt.close();
con.close();
return gbbean;
} catch (Exception ex) {
System.out.println("获取所有留言数据失败。");
ex.printStackTrace();
return null;
}
}
//搜索留言信息时对表中的数据进行筛选
public Vector getSelectData(String keywords,String classname) {
Connection con = com.netbar.dao.databaseOprate.getConnection();
PreparedStatement pstmt = null;
String sql = "select * from guestbook where "+classname+"like ? order by gbTime desc";
Vector vc=new Vector();
try {
pstmt = con.prepareStatement(sql);
pstmt.setString(1,keywords);
System.out.println("sql:"+sql);
ResultSet rs = pstmt.executeQuery();
int i = 0;
System.out.println(sql);
while (rs.next()) {
gbBean gb = new gbBean();
gb.setGbID(rs.getInt(1));
gb.setGbTitle(rs.getString(2));
gb.setGbContent(rs.getString(3));
gb.setGbReply(rs.getString(4));
gb.setGbPerson(rs.getString(5));
gb.setGbWeb(rs.getString(6));
gb.setGbMail(rs.getString(7));
gb.setGbQQ(rs.getString(8));
gb.setGbFace(rs.getString(9));
gb.setGbIP(rs.getString(10));
gb.setGbPcname(rs.getString(11));
gb.setGbTime(rs.getString(12));
gb.setGbRedate(rs.getString(13));
vc.add(i,gb);
i++;
}
rs.close();
pstmt.close();
con.close();
return vc;
} catch (Exception ex) {
System.out.println("获取搜索留言数据失败。");
ex.printStackTrace();
return null;
}
}
public static int getSearchRecord(String keywords,String classname){
Connection con = com.netbar.dao.databaseOprate.getConnection();
PreparedStatement pstmt = null;
String sql = "select count(*) from guestbook where "+classname+"like ? order by gbTime desc";
int count=0;
try {
pstmt=con.prepareStatement(sql);
pstmt.setString(1,keywords);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
count=rs.getInt(1);
}
rs.close();
pstmt.close();
con.close();
return count;
} catch (Exception ex) {
ex.printStackTrace();
return 0;
}
}
//签写留言
public static boolean insert(com.netbar.bean.gbBean bean){
Connection con=com.netbar.dao.databaseOprate.getConnection();
PreparedStatement pstmt=null;
String insertSql="insert into guestbook(gbTitle,gbContent,gbReply,gbPerson,gbWeb,gbMail,gbQQ,gbFace,gbIP,gbPcname,gbTime,gbRedate) values(?,?,?,?,?,?,?,?,?,?,?,?)";
try {
pstmt=con.prepareStatement(insertSql);
pstmt.setString(1,bean.getGbTitle());
pstmt.setString(2,bean.getGbContent());
pstmt.setString(3,null);
pstmt.setString(4,bean.getGbPerson());
pstmt.setString(5,bean.getGbWeb());
pstmt.setString(6,bean.getGbMail());
pstmt.setString(7,bean.getGbQQ());
pstmt.setString(8,bean.getGbFace());
pstmt.setString(9,bean.getGbIP());
pstmt.setString(10,bean.getGbPcname());
pstmt.setString(11,bean.getGbTime());
pstmt.setString(12,null);
int count=pstmt.executeUpdate();
return count>0?true:false;
} catch (Exception ex) {
System.out.println("留言内容存储到数据库时发生错误.");
ex.printStackTrace();
return false;
}finally{
try {
if(pstmt!=null){
pstmt.close();
}
if(con!=null){
con.close();
}
} catch (Exception ex) {
System.out.println("关闭插入留言记录连接对象时发生错误!");
ex.printStackTrace();
}
}
}
//end of insert
//按编号删除记录。。。
public static boolean delByID(int gbID){
Connection con=com.netbar.dao.databaseOprate.getConnection();
PreparedStatement pstmt=null;
String delSql="delete from guestbook where gbID=?";
try {
pstmt=con.prepareStatement(delSql);
pstmt.setInt(1,gbID);
int count=pstmt.executeUpdate();
pstmt.close();
return count>0?true:false;
} catch (Exception ex) {
System.out.println("删除留言信息时发生错误。");
ex.printStackTrace();
return false;
}
}
//回复留言信息时通过ID获取单条记录信息。
public com.netbar.bean.gbBean getBeanInfo(int gbid){
com.netbar.bean.gbBean gbbean=new gbBean();
String sql="select * from guestbook where gbID=?";
Connection con=com.netbar.dao.databaseOprate.getConnection();
PreparedStatement pstmt=null;
try {
pstmt=con.prepareStatement(sql);
pstmt.setInt(1,gbid);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
gbbean.setGbID(rs.getInt(1));
gbbean.setGbTitle(rs.getString(2));
gbbean.setGbContent(rs.getString(3));
gbbean.setGbReply(rs.getString(4));
gbbean.setGbPerson(rs.getString(5));
gbbean.setGbWeb(rs.getString(6));
gbbean.setGbMail(rs.getString(7));
gbbean.setGbQQ(rs.getString(8));
gbbean.setGbFace(rs.getString(9));
gbbean.setGbIP(rs.getString(10));
gbbean.setGbPcname(rs.getString(11));
gbbean.setGbTime(rs.getString(12));
gbbean.setGbRedate(rs.getString(13));
}
rs.close();
pstmt.close();
con.close();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
return gbbean;
}
//回复信息时,将修改后的信息提交到数据库。
public boolean updateGb_res(com.netbar.bean.gbBean gbbean){
String sql="update guestbook set gbTitle=?,gbContent=?,gbReply=?,gbPerson=?,gbWeb=?,gbMail=?,gbQQ=?,gbRedate=? where gbID=?";
PreparedStatement pstmt=null;
java.util.Date date=new java.util.Date();
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String currentTime=sdf.format(date);
Connection con=com.netbar.dao.databaseOprate.getConnection();
try {
pstmt=con.prepareStatement(sql);
pstmt.setString(1,gbbean.getGbTitle());
pstmt.setString(2,gbbean.getGbContent());
pstmt.setString(3,gbbean.getGbReply());
pstmt.setString(4,gbbean.getGbPerson());
pstmt.setString(5,gbbean.getGbWeb());
pstmt.setString(6,gbbean.getGbMail());
pstmt.setString(7,gbbean.getGbQQ());
pstmt.setString(8,currentTime);
pstmt.setInt(9,gbbean.getGbID());
int count=pstmt.executeUpdate();
return count>0?true:false;
} catch (Exception ex) {
System.out.println("修改留言信息时出错。。。");
ex.printStackTrace();
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -