📄 leavedb.java
字号:
package org.qhit.shop.db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class LeaveDB {
//1.插入留言
public int insert(LeaveBean l){
//声明
int i=0;
Connection conn=null;
PreparedStatement ps=null;
String sql="insert into leave values(?,getdate(),?,?)";
try {
//1.连接
conn=DBConn.getConn();
//2.创建预编译指令集对象
ps=conn.prepareStatement(sql);
//3.绑定参数(和?对应的)
ps.setString(1,l.getAuthor());
ps.setString(2, l.getContents());
ps.setInt(3, l.getGid());
//4.执行
i=ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(ps!=null)ps.close();
DBConn.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return i;
}
//1.查询所有留言的方法
public List queryAll(String gid){
List all=new ArrayList();
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
LeaveBean l=null;
String sql="select * from leave where gid=? order by dt desc";
try {
//1.连接
conn=DBConn.getConn();
//2.创建预编译指令集对象
ps=conn.prepareStatement(sql);
//4.执行
ps.setInt(1, Integer.parseInt(gid));
rs=ps.executeQuery();
//遍历结果集,将数据封装到List中
while(rs.next()){
l=new LeaveBean();
l.setId(rs.getInt(1));
l.setAuthor(rs.getString(2));
l.setDt(rs.getString(3).substring(0,19));
l.setContents(rs.getString(4));
l.setGid(rs.getInt(5));
all.add(l);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs!=null)rs.close();
if(ps!=null)ps.close();
DBConn.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return all;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -