package conn; import java.sql.*; public class Conn { Connection conn=null; public Connection getConn1(){ try{ String driver="sun.jdbc.odbc.JdbcOdbcDriver"; String url="jdbc:odbc:newsdemo"; Class.forName(driver); conn=DriverManager.getConnection(url,"qxb","123"); } catch(Exception gc1e){ gc1e.printStackTrace(); } return conn; } public Connection getConn2(){ try{ String driver="com.microsoft.jdbc.sqlserver:SQLServerDriver"; String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=newsdemo"; Class.forName(driver); conn=DriverManager.getConnection(url,"qxb","123"); } catch(Exception gc1e){ gc1e.printStackTrace(); } return conn; } } /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package po.columnT; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; /** * MyEclipse Struts * Creation date: 10-14-2009 * * XDoclet definition: * @struts.form name="column" */ public class Column extends ActionForm { /* * Generated fields */ /** name property */ private String name; /** columnID property */ private int columnID; /* * Generated Methods */ /** * Method validate * @param mapping * @param request * @return ActionErrors */ public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub return null; } /** * Method reset * @param mapping * @param request */ public void reset(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub } /** * Returns the name. * @return String */ public String getName() { return name; } /** * Set the name. * @param name The name to set */ public void setName(String name) { this.name = name; } /** * Returns the columnID. * @return String */ public int getColumnID() { return columnID; } /** * Set the columnID. * @param columnID The columnID to set */ public void setColumnID(int columnID) { this.columnID = columnID; } } package po.columnT; import java.sql.* ; import java.util.*; import conn.Conn; import po.newsT.*; public class ColumnDao { public boolean setColumn(Column column){ try{ Connection conn=new Conn().getConn1(); String sql="INSERT INTO columnT (name) VALUES(?)"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setString(1, column.getName()); pstmt.addBatch(); pstmt.execute(); conn.close(); return true; } catch(Exception sne){ sne.printStackTrace(); return false; } } public boolean deleteColumn(Column column){ try{ Connection conn=new Conn().getConn1(); String sql="DELETE FROM columnT WHERE columnID=?"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setInt(1,column.getColumnID()); pstmt.addBatch(); pstmt.execute(); conn.close(); return true; } catch(Exception sne){ sne.printStackTrace(); return false; } } public boolean updateColumn(Column column){ try{ Connection conn=new Conn().getConn1(); String sql="UPDATE columnT SET name=? WHERE columnID=?"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setString(1,column.getName()); pstmt.setInt(2,column.getColumnID()); pstmt.addBatch(); pstmt.execute(); conn.close(); return true; } catch(Exception sne){ sne.printStackTrace(); return false; } } public List<Column> getColumn(){ List<Column> list=new ArrayList<Column>(); try{ Connection conn=new Conn().getConn1(); String sql="SELECT * FROM columnT"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.addBatch(); ResultSet rs=pstmt.executeQuery(); while(rs.next()){ Column column=new Column(); column.setColumnID(rs.getInt("columnID")); column.setName(rs.getString("name")); list.add(column); } conn.close(); } catch(Exception e){ e.printStackTrace(); } return list; } public String getNameByID(int columnID){ String name="" ; try{ Connection conn=new Conn().getConn1(); String sql="SELECT * FROM columnT WHERE columnID=?"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setInt(1, columnID); pstmt.addBatch(); ResultSet rs=pstmt.executeQuery(); while(rs.next()){ name=(rs.getString("name")); } conn.close(); } catch(Exception gnbie){ gnbie.printStackTrace(); } return name; } } /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package po.CommentT; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; /** * MyEclipse Struts * Creation date: 10-14-2009 * * XDoclet definition: * @struts.form name="comment" */ public class Comment extends ActionForm { /* * Generated fields */ /** pltime property */ private String pltime; /** commentID property */ private int commentID; /** content property */ private String content; /** newsID property */ private int newsID; /** author property */ private String author; /* * Generated Methods */ /** * Method validate * @param mapping * @param request * @return ActionErrors */ public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub return null; } /** * Method reset * @param mapping * @param request */ public void reset(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub } /** * Returns the pltime. * @return String */ public String getPltime() { return pltime; } /** * Set the pltime. * @param pltime The pltime to set */ public void setPltime(String pltime) { this.pltime = pltime; } /** * Returns the commentID. * @return String */ public int getCommentID() { return commentID; } /** * Set the commentID. * @param commentID The commentID to set */ public void setCommentID(int commentID) { this.commentID = commentID; } /** * Returns the content. * @return String */ public String getContent() { return content; } /** * Set the content. * @param content The content to set */ public void setContent(String content) { this.content = content; } /** * Returns the newsID. * @return String */ public int getNewsID() { return newsID; } /** * Set the newsID. * @param newsID The newsID to set */ public void setNewsID(int newsID) { this.newsID = newsID; } /** * Returns the author. * @return String */ public String getAuthor() { return author; } /** * Set the author. * @param author The author to set */ public void setAuthor(String author) { this.author = author; } } package po.CommentT; import po.columnT.*; import java.sql.*; import java.util.*; import conn.Conn; import po.newsT.*; public class CommentDao { public boolean setComment(Comment comment){ try{ Connection conn=new Conn().getConn1(); String sql="INSERT INTO commentT (content,pltime,author,newsID) VALUES(?,?,?,?)"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setString(1,comment.getContent()); pstmt.setString(2,comment.getPltime()); pstmt.setString(3,comment.getAuthor()); pstmt.setInt(4,comment.getNewsID()); pstmt.addBatch(); pstmt.execute(); conn.close(); return true; } catch(Exception sne){ sne.printStackTrace(); return false; } } public boolean deleteComment(int newsID){ try{ Connection conn=new Conn().getConn1(); String sql="DELETE FROM commentT WHERE newsID=?"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setInt(1,newsID); pstmt.addBatch(); pstmt.execute(); conn.close(); return true; } catch(Exception sne){ sne.printStackTrace(); return false; } } //get all comment by it;s news ID public List<Comment> getCommentByNewsID(int newsID){ List<Comment> list=new ArrayList<Comment>(); try{ Connection conn=new Conn().getConn1(); String sql="SELECT * FROM commentT WHERE newsID=?"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setInt(1, newsID); pstmt.addBatch(); ResultSet rs=pstmt.executeQuery(); while(rs.next()){ Comment comment=new Comment(); comment.setCommentID(rs.getInt("commentID")); comment.setContent(rs.getString("content")); comment.setPltime(rs.getString("pltime")); comment.setAuthor(rs.getString("author")); list.add(comment); } conn.close(); } catch(Exception e){ e.printStackTrace(); } return list; } } /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package po.newsT; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; /** * MyEclipse Struts * Creation date: 10-14-2009 * * XDoclet definition: * @struts.form name="News" */ public class News extends ActionForm { /* * Generated fields */ /** title property */ private String title; /** fbtime property */ private String fbtime; /** content property */ private String content; /** newsID property */ private int newsID; /** author property */ private String author; /** columnID property */ private int columnID; /* * Generated Methods */ /** * Method validate * @param mapping * @param request * @return ActionErrors */ public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub return null; } /** * Method reset * @param mapping * @param request */ public void reset(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub } /** * Returns the title. * @return String */ public String getTitle() { return title; } /** * Set the title. * @param title The title to set */ public void setTitle(String title) { this.title = title; } /** * Returns the fbtime. * @return String */ public String getFbtime() { return fbtime; } /** * Set the fbtime. * @param fbtime The fbtime to set */ public void setFbtime(String fbtime) { this.fbtime = fbtime; } /** * Returns the content. * @return String */ public String getContent() { return content; } /** * Set the content. * @param content The content to set */ public void setContent(String content) { this.content = content; } /** * Returns the newsID. * @return String */ public int getNewsID() { return newsID; } /** * Set the newsID. * @param newsID The newsID to set */ public void setNewsID(int newsID) { this.newsID = newsID; } /** * Returns the author. * @return String */ public String getAuthor() { return author; } /** * Set the author. * @param author The author to set */ public void setAuthor(String author) { this.author = author; } /** * Returns the columnID. * @return String */ public int getColumnID() { return columnID; } /** * Set the columnID. * @param columnID The columnID to set */ public void setColumnID(int columnID) { this.columnID = columnID; } } package po.newsT; import java.sql.*; import java.util.*; import conn.Conn; import po.columnT.*; public class NewsDao { public boolean setNews(News news){ try{ Connection conn=new Conn().getConn1(); String sql="INSERT INTO newsT (title,content,fbtime,author,columnID) VALUES(?,?,?,?,?)"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setString(1, news.getTitle()); pstmt.setString(2, news.getContent()); pstmt.setString(3, news.getFbtime()); pstmt.setString(4, news.getAuthor()); pstmt.setInt(5, news.getColumnID()); pstmt.addBatch(); pstmt.execute(); conn.close(); return true; } catch(Exception sne){ sne.printStackTrace(); return false; } } public boolean deleteNews(News news){ try{ Connection conn=new Conn().getConn1(); String sql="DELETE FROM newsT WHERE newsID=?"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setInt(1,news.getNewsID()); pstmt.addBatch(); pstmt.execute(); conn.close(); return true; } catch(Exception sne){ sne.printStackTrace(); return false; } } public boolean updateNews(News news){ try{ Connection conn=new Conn().getConn1(); String sql="UPDATE newsT SET title=?,content=?,author=?,columnID=? WHERE newsID=?"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setString(1, news.getTitle()); pstmt.setString(2, news.getContent()); pstmt.setString(3, news.getAuthor()); pstmt.setInt(4, news.getColumnID()); pstmt.setInt(5, news.getNewsID()); System.out.println(news.getContent()); pstmt.addBatch(); pstmt.execute(); conn.close(); return true; } catch(Exception sne){ sne.printStackTrace(); return false; } } //get all news by there group !! public List<News> getNewsByColumn(Column column){ List<News> list=new ArrayList<News>(); try{ Connection conn=new Conn().getConn1(); String sql="SELECT * FROM newsT WHERE columnID=?"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setInt(1, column.getColumnID()); pstmt.addBatch(); ResultSet rs=pstmt.executeQuery(); while(rs.next()){ News news=new News(); news.setNewsID(rs.getInt("newsID")); news.setAuthor(rs.getString("author")); news.setContent(rs.getString("content")); news.setFbtime(rs.getString("fbtime")); news.setColumnID( (rs.getInt("columnID"))); news.setTitle(rs.getString("title")); list.add(news); } conn.close(); } catch(Exception e){ e.printStackTrace(); } return list; } //get one news by it's ID public News getNewsByID(int newsID){ News news=null; try{ Connection conn=new Conn().getConn1(); String sql="SELECT * FROM newsT WHERE newsID=?"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setInt(1, newsID); pstmt.addBatch(); ResultSet rs=pstmt.executeQuery(); while(rs.next()){ news =new News(); news.setNewsID(rs.getInt("newsID")); news.setAuthor(rs.getString("author")); news.setContent(rs.getString("content")); news.setFbtime(rs.getString("fbtime")); news.setColumnID( (rs.getInt("columnID"))); news.setTitle(rs.getString("title")); } conn.close(); } catch(Exception e){ e.printStackTrace(); } return news; } } package po.newsT; import po.columnT.Column; import java.util.*; import java.sql.*; import po.columnT.*; import po.newsT.*; import po.CommentT.*; public class test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub NewsDao newsDao=new NewsDao(); News news=new News(); ColumnDao columnDao=new ColumnDao(); Column column=new Column(); CommentDao commentDao =new CommentDao(); Comment comment =new Comment(); news.setTitle("bt11"); news.setAuthor("ba111 "); news.setColumnID(1); news.setContent("bb11"); news.setFbtime("2003-13-3"); news.setNewsID(1); column.setColumnID(news.getColumnID()); column.setName("d"); List<News> list=newsDao.getNewsByColumn(column); List<Column> listColumn=columnDao.getColumn(); columnDao.setColumn(column); Column delcolumn=new Column(); delcolumn.setColumnID(3); comment.setNewsID(1); comment.setAuthor("zaa"); comment.setContent("fasfsa"); comment.setPltime("2008-3-3"); commentDao.setComment(comment); comment.setNewsID(2); comment.setAuthor("zaadddddddd"); comment.setContent("fasfsdddddddddda"); comment.setPltime("2008-1-2"); commentDao.setComment(comment); commentDao.deleteComment(1); List<Comment> listComment=commentDao.getCommentByNewsID(1); for(int i=0;i<listComment.size();i++){ comment =listComment.get(i); System.out.println(comment.getContent()); } listComment=commentDao.getCommentByNewsID(2); for(int i=0;i<listComment.size();i++){ comment =listComment.get(i); System.out.println(comment.getContent()); } } } /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package usert; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; /** * MyEclipse Struts * Creation date: 10-14-2009 * * XDoclet definition: * @struts.form name="usert" */ public class Usert extends ActionForm { /* * Generated fields */ /** answer1 property */ private String answer1; /** answer3 property */ private String answer3; /** useCount property */ private String useCount; /** content property */ private String content; /** trueAnswer property */ private String trueAnswer; /** testID property */ private String testID; /** answer4 property */ private String answer4; /** answer2 property */ private String answer2; /* * Generated Methods */ /** * Method validate * @param mapping * @param request * @return ActionErrors */ public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub return null; } /** * Method reset * @param mapping * @param request */ public void reset(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub } /** * Returns the answer1. * @return String */ public String getAnswer1() { return answer1; } /** * Set the answer1. * @param answer1 The answer1 to set */ public void setAnswer1(String answer1) { this.answer1 = answer1; } /** * Returns the answer3. * @return String */ public String getAnswer3() { return answer3; } /** * Set the answer3. * @param answer3 The answer3 to set */ public void setAnswer3(String answer3) { this.answer3 = answer3; } /** * Returns the useCount. * @return String */ public String getUseCount() { return useCount; } /** * Set the useCount. * @param useCount The useCount to set */ public void setUseCount(String useCount) { this.useCount = useCount; } /** * Returns the content. * @return String */ public String getContent() { return content; } /** * Set the content. * @param content The content to set */ public void setContent(String content) { this.content = content; } /** * Returns the trueAnswer. * @return String */ public String getTrueAnswer() { return trueAnswer; } /** * Set the trueAnswer. * @param trueAnswer The trueAnswer to set */ public void setTrueAnswer(String trueAnswer) { this.trueAnswer = trueAnswer; } /** * Returns the testID. * @return String */ public String getTestID() { return testID; } /** * Set the testID. * @param testID The testID to set */ public void setTestID(String testID) { this.testID = testID; } /** * Returns the answer4. * @return String */ public String getAnswer4() { return answer4; } /** * Set the answer4. * @param answer4 The answer4 to set */ public void setAnswer4(String answer4) { this.answer4 = answer4; } /** * Returns the answer2. * @return String */ public String getAnswer2() { return answer2; } /** * Set the answer2. * @param answer2 The answer2 to set */ public void setAnswer2(String answer2) { this.answer2 = answer2; } }