protected String INSERT="insert into news_detail(id,categoryId,title,author,summary,content,createdate) values(seq_news.nextval,?,?,?,?,?,?)";
protected String UPDATE="update news_detail set title=?,author=?,summary=?,content=?,modifydate=? where id=?";
protected String DELETE="delete news_detail where id=?";
protected String SELE="select id,categoryId,title,author,summary,content,createdate from news_detail";
protected String SEID="select categoryId,title,author,summary,content from news_detail where id=?";
//查询信息
public List<Newdatail> getList(){
List<Newdatail> list=new ArrayList<Newdatail>();
Connection con=null;
Statement st=null;
ResultSet re=null;
try {
con=super.getConnection();//连接数据库
st=con.createStatement();
re=st.executeQuery(SELE);
while(re.next()){
int id=re.getInt("id");
int categoryId=re.getInt("categoryId");
String title=re.getString("title");
String author=re.getString("author");
String summary=re.getString("summary");
String content=re.getString("content");
Timestamp time=re.getTimestamp("createdate");
Newdatail news=new Newdatail();
news.setId(id);
news.setCategoryId(categoryId);
news.setTitle(title);
news.setAuthor(author);
news.setSummary(summary);
news.setContent(content);
news.setCreatedate(time);
list.add(news);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeNews();
}
return list;
}
//删除信息
public void delete(int id){
Connection con=null;
PreparedStatement pst=null;
try {
con=super.getConnection();
pst=con.prepareStatement(DELETE);
pst.setInt(1, id);
int i=pst.executeUpdate();
if(i>0)
System.out.println("删除成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeNews();
}
}
//修改信息
public void update(Newdatail news){
Connection con=null;
PreparedStatement pst=null;
try {
con=super.getConnection();
pst=con.prepareStatement(UPDATE);
pst.setString(1, news.getTitle());
pst.setString(2, news.getAuthor());
pst.setString(3, news.getSummary());
pst.setString(4, news.getContent());
pst.setTimestamp(5, new java.sql.Timestamp(news.getModifydate().getTime()));
pst.setInt(6, news.getId());
int i=pst.executeUpdate();
if(i>0)
System.out.println("修改成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeNews();
}
}
//插入信息
public void insert(Newdatail news){
Connection con=null;
PreparedStatement pst=null;
try {
con=super.getConnection();
pst=con.prepareStatement(INSERT);
pst.setInt(1, news.getCategoryId());
pst.setString(2, news.getTitle());
pst.setString(3, news.getAuthor());
pst.setString(4, news.getSummary());
pst.setString(5, news.getContent());
pst.setTimestamp(6, new java.sql.Timestamp(news.getCreatedate().getTime()));
int i=pst.executeUpdate();
if(i>0)
System.out.println("插入成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Newdatail getNewsId(int id) {
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
con=super.getConnection();
ps=con.prepareStatement(SEID);
ps.setInt(1, id);
rs=ps.executeQuery();
if(rs.next()){
Newdatail news =new Newdatail();
news.setCategoryId(rs.getInt("categoryId"));
news.setTitle(rs.getString("title"));
news.setAuthor(rs.getString("author"));
news.setSummary(rs.getString("summary"));
news.setContent(rs.getString("content"));
return news;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeNews();
}
return null;
}
public static void main(String[] args) {
NewsDao newsDao=new NewsDao();
//查询信息
/* List<Newdatail> list=newsDao.getList();
System.out.println("编号\t类型\t标题\t作者\t\t摘要\t\t\t内容\t\t\t\t时间");
for(Newdatail news:list){
System.out.println(news.getId()+"\t"+news.getCategoryId()+"\t"+news.getTitle()+"\t"+news.getAuthor()+"\t"+news.getSummary()+"\t"+news.getContent()+"\t"+new Date());
}*/
//删除信息
// newsDao.delete(21);
//修改信息
/*Newdatail news=new Newdatail();
news.setTitle("休息的一天");
news.setAuthor("fcy");
news.setSummary("今天睡到11点起床");
news.setContent("今天睡到11点起床,然后就去医院");
news.setModifydate(new Date());
news.setId(4);
newsDao.update(news);*/
//插入信息
Newdatail news=new Newdatail();
news.setTitle("学习");
news.setAuthor("fcy");
news.setSummary("每天都要按时起床");
news.setContent("每天都要制定学习计划,按照计划学习");
news.setCreatedate(new Date());
// news.setId(6);
news.setCategoryId(3);
newsDao.insert(news);
}
}
protected String UPDATE="update news_detail set title=?,author=?,summary=?,content=?,modifydate=? where id=?";
protected String DELETE="delete news_detail where id=?";
protected String SELE="select id,categoryId,title,author,summary,content,createdate from news_detail";
protected String SEID="select categoryId,title,author,summary,content from news_detail where id=?";
//查询信息
public List<Newdatail> getList(){
List<Newdatail> list=new ArrayList<Newdatail>();
Connection con=null;
Statement st=null;
ResultSet re=null;
try {
con=super.getConnection();//连接数据库
st=con.createStatement();
re=st.executeQuery(SELE);
while(re.next()){
int id=re.getInt("id");
int categoryId=re.getInt("categoryId");
String title=re.getString("title");
String author=re.getString("author");
String summary=re.getString("summary");
String content=re.getString("content");
Timestamp time=re.getTimestamp("createdate");
Newdatail news=new Newdatail();
news.setId(id);
news.setCategoryId(categoryId);
news.setTitle(title);
news.setAuthor(author);
news.setSummary(summary);
news.setContent(content);
news.setCreatedate(time);
list.add(news);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeNews();
}
return list;
}
//删除信息
public void delete(int id){
Connection con=null;
PreparedStatement pst=null;
try {
con=super.getConnection();
pst=con.prepareStatement(DELETE);
pst.setInt(1, id);
int i=pst.executeUpdate();
if(i>0)
System.out.println("删除成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeNews();
}
}
//修改信息
public void update(Newdatail news){
Connection con=null;
PreparedStatement pst=null;
try {
con=super.getConnection();
pst=con.prepareStatement(UPDATE);
pst.setString(1, news.getTitle());
pst.setString(2, news.getAuthor());
pst.setString(3, news.getSummary());
pst.setString(4, news.getContent());
pst.setTimestamp(5, new java.sql.Timestamp(news.getModifydate().getTime()));
pst.setInt(6, news.getId());
int i=pst.executeUpdate();
if(i>0)
System.out.println("修改成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeNews();
}
}
//插入信息
public void insert(Newdatail news){
Connection con=null;
PreparedStatement pst=null;
try {
con=super.getConnection();
pst=con.prepareStatement(INSERT);
pst.setInt(1, news.getCategoryId());
pst.setString(2, news.getTitle());
pst.setString(3, news.getAuthor());
pst.setString(4, news.getSummary());
pst.setString(5, news.getContent());
pst.setTimestamp(6, new java.sql.Timestamp(news.getCreatedate().getTime()));
int i=pst.executeUpdate();
if(i>0)
System.out.println("插入成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Newdatail getNewsId(int id) {
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
con=super.getConnection();
ps=con.prepareStatement(SEID);
ps.setInt(1, id);
rs=ps.executeQuery();
if(rs.next()){
Newdatail news =new Newdatail();
news.setCategoryId(rs.getInt("categoryId"));
news.setTitle(rs.getString("title"));
news.setAuthor(rs.getString("author"));
news.setSummary(rs.getString("summary"));
news.setContent(rs.getString("content"));
return news;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeNews();
}
return null;
}
public static void main(String[] args) {
NewsDao newsDao=new NewsDao();
//查询信息
/* List<Newdatail> list=newsDao.getList();
System.out.println("编号\t类型\t标题\t作者\t\t摘要\t\t\t内容\t\t\t\t时间");
for(Newdatail news:list){
System.out.println(news.getId()+"\t"+news.getCategoryId()+"\t"+news.getTitle()+"\t"+news.getAuthor()+"\t"+news.getSummary()+"\t"+news.getContent()+"\t"+new Date());
}*/
//删除信息
// newsDao.delete(21);
//修改信息
/*Newdatail news=new Newdatail();
news.setTitle("休息的一天");
news.setAuthor("fcy");
news.setSummary("今天睡到11点起床");
news.setContent("今天睡到11点起床,然后就去医院");
news.setModifydate(new Date());
news.setId(4);
newsDao.update(news);*/
//插入信息
Newdatail news=new Newdatail();
news.setTitle("学习");
news.setAuthor("fcy");
news.setSummary("每天都要按时起床");
news.setContent("每天都要制定学习计划,按照计划学习");
news.setCreatedate(new Date());
// news.setId(6);
news.setCategoryId(3);
newsDao.insert(news);
}
}
数据库操作示例
1678

被折叠的 条评论
为什么被折叠?



