📄 newsdao.java
字号:
package news.db;
import news.db.*;
import news.form.*;
import java.sql.*;
import java.util.*;
public class NewsDAO{
public int insert(News n){
Connection con = null;
int k=0;
PreparedStatement pstmt = null;
String sql = "";
sql = "insert into news(title,author,content,pubtime)values(?,?,?,now())";
try{
con = DatabaseConnection.getCon();
pstmt = con.prepareStatement(sql);
pstmt.setString(1,n.getTitle());
pstmt.setString(2,n.getAuthor());
pstmt.setString(3,n.getContent());
k=pstmt.executeUpdate();
}catch(Exception e){e.printStackTrace();
}
finally{
try{
if(con!=null){
con.close();
}
}catch(Exception e2){}
}
return k;
}
public ArrayList searchAll(){
Connection con = null;
PreparedStatement pstmt = null;
String sql = "";
ResultSet rs = null;
News n=null;
sql = "select * from news";
ArrayList ar = new ArrayList();
try{
con = DatabaseConnection.getCon();
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()){
n = new News();
n.setTitle(rs.getString("title"));
n.setAuthor(rs.getString("author"));
n.setContent(rs.getString("content"));
n.setPubtime(rs.getDate("pubtime"));
n.setId(rs.getInt("id"));
ar.add(n);
}
}catch(Exception e){e.printStackTrace();
}
finally{
try{
if(con!=null){
con.close();
}
}catch(Exception e2){}
}
return ar;
}
public int delete(String aa){
Connection con = null;
int k=0;
PreparedStatement pstmt = null;
String sql = "";
sql = "delete from news where id=?";
try{
con = DatabaseConnection.getCon();
pstmt = con.prepareStatement(sql);
pstmt.setString(1,aa);
k=pstmt.executeUpdate();
}catch(Exception e){e.printStackTrace();
}
finally{
try{
if(con!=null){
con.close();
}
}catch(Exception e2){}
}
return k;
}
public News search(String id){
Connection con = null;
PreparedStatement pstmt = null;
String sql = "";
ResultSet rs = null;
News u = new News();
sql = "select * from news where id=?";
try{
con = DatabaseConnection.getCon();
pstmt = con.prepareStatement(sql);
pstmt.setString(1,id);
rs = pstmt.executeQuery();
if(rs.next()){
u.setId(rs.getInt("id"));
u.setTitle(rs.getString("title"));
u.setAuthor(rs.getString("author"));
u.setContent(rs.getString("content"));
u.setPubtime(rs.getDate("pubtime"));
}
}catch(Exception e){e.printStackTrace();
}
finally{
try{
if(con!=null){
con.close();
}
}catch(Exception e2){}
}
return u;
}
public int update(News u){
Connection con = null;
int k=0;
PreparedStatement pstmt = null;
String sql = "";
sql = "update news set title=?,author=?,content=? where id=?";
try{
con = DatabaseConnection.getCon();
pstmt = con.prepareStatement(sql);
pstmt.setString(1,u.getTitle());
pstmt.setString(2,u.getAuthor());
pstmt.setString(3,u.getContent());
pstmt.setInt(4,u.getId());
k=pstmt.executeUpdate();
}catch(Exception e){e.printStackTrace();
}
finally{
try{
if(con!=null){
con.close();
}
}catch(Exception e2){}
}
return k;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -