📄 leavewordoperate.java
字号:
package com.dimscar.util;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import com.dimscar.struts.vo.LeaveWordBean;
public class LeaveWordOperate extends ConnectionPool {
/**
* 查找数据库所有留言内容,以LeaveWord实例放入Collection里.
* @return 留言所有内容
* @throws SQLException
*/
public Collection getAllLeaveword() throws SQLException{
ResultSet rs = this.Query("select * from leaveword");
Collection collon = new ArrayList();
// 留言簿实例
LeaveWordBean liuyan = null ;
while(rs.next()){
liuyan = new LeaveWordBean();
liuyan.setId(rs.getString("id"));
liuyan.setMotif(rs.getString("motif"));
liuyan.setContent(rs.getString("content"));
liuyan.setThisTime(rs.getString("thisTime"));
liuyan.setRevert(rs.getString("revert"));
collon.add(liuyan);
}
//关闭连接
this.closeDriver();
return collon;
}
/**
* 根据留言ID查询数据库,把找到的记录放入留言簿实例对象当中,然后返回这个对象
* @param id 留言ID
* @return 留言实例对象
* @throws SQLException
*/
public LeaveWordBean getSeleteObject(String id) throws SQLException{
//SQL
String sql = "select * from leaveword where id = '"+id+"'";
//查询对象
ResultSet rs = this.Query(sql);
//留言簿实例
LeaveWordBean liuyan = null ;
//放入对象到实例当中
while(rs.next()){
liuyan = new LeaveWordBean();
liuyan.setId(rs.getString("id"));
liuyan.setMotif(rs.getString("motif"));
liuyan.setContent(rs.getString("content"));
liuyan.setThisTime(rs.getString("thisTime"));
liuyan.setRevert(rs.getString("revert"));
}
this.closeDriver();
return liuyan;
}
/**
* 留言簿留言进行对数据库插入操作,返回一个插入操作是否成功的值.
* @param lwBean 留言簿Bean实例
* @return 留言簿插入留言是否成功!
* @throws SQLException
*/
public int insert(LeaveWordBean lwBean){
int num = 0;
String sql = "insert into leaveword values(?,?,?,?,?)";
//加载驱动
try {
this.onLoadDriver();
PreparedStatement stamt = this.con.prepareStatement(sql);
stamt.setString(1,lwBean.getId());
stamt.setString(2,lwBean.getMotif());
stamt.setString(3,lwBean.getContent());
stamt.setString(4,lwBean.getThisTime());
stamt.setString(5,lwBean.getRevert());
num = stamt.executeUpdate();
stamt.close();
} catch (SQLException e) {
e.printStackTrace();
return 0;
}finally{
this.closeDriver();
}
return num ;
}
/**
* 留言簿留言进行对数据库更新操作,返回是否更新成功 falg
* @param lwBean 留言实例对象
* @return falg boolean是否更新成功
*/
public boolean isUpdate(LeaveWordBean lwBean){
boolean flag = true ;
StringBuffer sql = new StringBuffer("update leaveword set ");
try {
//SQL
sql.append("motif='"+lwBean.getMotif()+"',");
sql.append("content='"+lwBean.getContent()+"',");
sql.append("thisTime='"+lwBean.getThisTime()+"',");
sql.append("revert='"+lwBean.getRevert()+"'");
sql.append(" where id='"+lwBean.getId()+"'");
//加载驱动
this.onLoadDriver();
//更新操作
flag = this.update(new String(sql));
} catch (SQLException e) {
e.printStackTrace();
return false;
}finally{
//关闭连接
this.closeDriver();
}
return flag ;
}
/**
* 留言删除功能,传入一个留言ID的数组,然后组成SQL依次删除留言
* @param id 留言ID的数组
* @return 删除记录数
*/
public int delete(String[] id){
//删除记录数
int num = 0;
if(id==null){
return 0;
}
for(int i = 0; i<id.length; i++){
String sql = "delete from leaveword where id = '"+id[i]+"'";
//删除
if(this.update(sql)){
num++;
}else{
return num;
}
}
return num;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -