package news;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import user.user;
import news.News;
import common.DBUtil;
public class News_Dao {
private static final String username = null;
private static final String title = null;
private News news=new News();
private DBUtil db=new DBUtil();
public News_Dao()
{
}
public News_Dao(News news)
{
this.news=news;
}
public News getNews() {
return news;
}
public void setNews(News news) {
this.news = news;
}
public int doInsert()
{
String sql = "insert into news (title,content,userid,pubtime) values( '"+news.getTitle()+"','"+news.getContent()+"','"+news.getUserid()+"','"+news.getPubtime()+"');";
int rs = db.executeUpdate(sql);
return rs;
}
/**
* ��ѯ������Ϣ
* @return
*/
/**
* �û��б�IJ鿴 �����
* @return
*/
public ArrayList<News> doSeleteAll(){
//select id
ArrayList<News> list=new ArrayList<News>();//������յĶ�̬��������װ�û�
String sql="select * from news;";
ResultSet rs= db.executeQuery(sql);
try {
while(rs.next()){
News news=new News();//
int id = rs.getInt("id");
news.setId(id);
String title = rs.getString("title");
news.setTitle(title);
String content = rs.getString("content");
news.setContent(content);
String userid=rs.getString("userid");
news.setUserid(userid);
String pubtime = rs.getString("pubtime");
news.setPubtime(pubtime);
list.add(news);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
// return null;
e.printStackTrace();
}finally{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db.closeDB();
}
return list;
}
/**
* ��ѯһ����Ϣ
* @return
*/
public News doSelectOne(int id)
{
String sql="SELECT * FROM news WHERE id="+id+";";//��ѯ���//ƥ��id
ResultSet rs = db.executeQuery(sql);
News news=new News();
boolean b = false;
try {
b=rs.next();
news.setId(rs.getInt("id"));//���� Ȼ������ ���news���� ��ء�
news.setTitle(rs.getString("title"));
news.setContent(rs.getString("content"));
news.setPubtime(rs.getString("pubtime"));
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally{
db.closeDB();
}
return news;
/*
*/
}
public ArrayList<News> doSelectOne()
{
ArrayList<News> list=new ArrayList<News>();//������յĶ�̬��������װ�û�
String sql="select news.id,title,content,username,userid,pubtime from news join user on news.userid=user.id where title like '%"+news.getTitle()+"%';";
ResultSet rs1= db.executeQuery(sql);
try {
while(rs1.next()){
News news=new News();//����һ�����û���һ�о���һ���û�
int id = rs1.getInt("id");
news.setId(id);
String title = rs1.getString("title");
news.setTitle(title);
String content = rs1.getString("content");
news.setContent(content);
String userid= rs1.getString("userid");
news.setUserid(userid);
String pubtime = rs1.getString("pubtime");
news.setPubtime(pubtime);
list.add(news);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
// return null;
e.printStackTrace();
}finally{
try {
rs1.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db.closeDB();
}
return list;
}
/**
* ɾ��ģ��
* @param id
* @return ��=1ɾ���ɹ� <1ɾ��ʧ��
*/
public int doDelte(int id)
{
String sql="delete FROM news where id="+id+";";//ע�����ݿ���벻Ҫд����
int a1=db.executeUpdate(sql);
return a1;
}
/**
* ���
* @param id
* @param title
* @param content
* @return ��=1�ijɹ� <1��ʧ��
*/
public int doedit(int id)
{
int a1=-1;
String sql = "update news set title='"+news.getTitle()+"',content='"+news.getContent()+"' where id='"+id+"';";
a1=db.executeUpdate(sql);
System.out.println(sql);
db.closeDB();
return a1;
}
public News doSelectOne(News news)
{
return null;
}
public News doSelectNew(int id){
String sql = "select * from news where id='"+id+"';";
ResultSet rs=db.executeQuery(sql);
News news=null;
try {
while(rs.next()){
news=new News();
news.setId(rs.getInt("id"));
news.setTitle(rs.getString("title"));
news.setContent(rs.getString("content"));
news.setUserid(rs.getString("userid"));
news.setPubtime(rs.getString("pubtime"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return news;
}
public ArrayList<News> doSelectNewsBy(String tijiao) {
ArrayList<News> list = new ArrayList<News>();
String sql = "select * from news where title = '"+tijiao+"' or userid='"+tijiao+"';";
ResultSet rs = db.executeQuery(sql);
try {
while(rs.next()) {
News news=new News();
news.setId(rs.getInt("id"));
news.setTitle(rs.getString("title"));
news.setContent(rs.getString("content"));
news.setUserid(rs.getString("userid"));
news.setPubtime(rs.getString("pubtime"));
list.add(news);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
db.closeDB();
}
return list;
}
}
最近下载更多
qwqwx585160 LV2
2024年12月15日
bankroll LV5
2024年6月13日
asddwh LV13
2023年12月29日
mengmeng12323423 LV4
2023年6月17日
lwlwlwlw LV3
2023年6月14日
chen_jw LV10
2023年2月17日
include LV8
2022年12月30日
13169178967 LV2
2022年12月11日
傻瓜还没打死烦人 LV7
2022年12月6日
zhunishimian LV6
2022年10月30日

最近浏览