📄 bbs_dao.java
字号:
package com.bookshop.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.bookshop.db.DBConnection;
import com.bookshop.dto.bbs_dto;
public class bbs_dao {
public ArrayList bbs_select()
{
ArrayList list = new ArrayList();
Connection conn =null;
conn=DBConnection.getConnection();
Statement st = null;
ResultSet rs = null;
String strSQL=null;
try {
st = conn.createStatement();
strSQL="SELECT * FROM tb_bbs order by id desc";
rs = st.executeQuery(strSQL);
while (rs.next()) {
bbs_dto newInfo=new bbs_dto();
newInfo.setId(rs.getString("id"));
newInfo.setContent(rs.getString("content"));
newInfo.setIntime(rs.getString("intime"));
list.add(newInfo);
}
} catch (SQLException e) {
e.printStackTrace();
}
finally
{
try {
rs.close();
st.close();
conn.close();
} catch (SQLException ex1) {
ex1.printStackTrace();
}
}
return list;
}
//根据id查询公告信息
public bbs_dto bbs_sel(String id){
Connection conn=DBConnection.getConnection();
Statement st=null;
ResultSet rs=null;
String sql="select * from tb_bbs where id="+id;
bbs_dto dto=new bbs_dto();
try{
st=conn.createStatement();
rs=st.executeQuery(sql);
if(rs.next()){
dto.setId(rs.getString("id"));
dto.setContent(rs.getString("content"));
}
}catch(SQLException e){e.printStackTrace();}
finally
{
try {
rs.close();
st.close();
conn.close();
} catch (SQLException ex1) { ex1.printStackTrace(); }
}
return dto;
}
//册除公告信息
public int bbs_delete(String id){
Connection conn=DBConnection.getConnection();
Statement st=null;
int n=0;
String sql="delete from tb_bbs where id="+id;
try{
st=conn.createStatement();
n=st.executeUpdate(sql);
}catch (SQLException ex1) { ex1.printStackTrace(); }
finally
{
try {
st.close();
conn.close();
} catch (SQLException ex1) { ex1.printStackTrace(); }
}
return n;
}
//添加公告
public int bbs_insert(String content){
System.out.print("hello_________");
Connection conn=DBConnection.getConnection();
Statement st=null;
String sql="insert into tb_bbs (content) values('"+content+"')";
int n=0;
System.out.println(sql);
try{
st=conn.createStatement();
n=st.executeUpdate(sql);
}catch (SQLException ex1) { ex1.printStackTrace(); }
finally
{
try {
st.close();
conn.close();
} catch (SQLException ex1) { ex1.printStackTrace(); }
}
return n;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -