1. 程式人生 > >基於jsp的新聞釋出系統

基於jsp的新聞釋出系統

其他的操作分別為增刪改查的具體程式碼,分別對應Servlet中或jsp中需要例項化的程式碼,下面就是具體的DAO類程式碼:
package dao;
import com.lut.beans.NewsRealese;
import static java.lang.System.out;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class NewsRealeseDao {
    public ArrayList queryAllNews() throws Exception {
        Connection conn = null;
        ArrayList newsRealese = new ArrayList();
        try {
            //獲取連線  
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@localhost:1521:orcl";
            conn = DriverManager.getConnection(url, "scott", "wjk139");
            //執行SQL語句 
            String sql = "select * from newmessage";//獲取
            Statement stat = conn.createStatement();
            ResultSet rs = stat.executeQuery(sql);
            while (rs.next()) {   //例項化VO
                NewsRealese news = new NewsRealese();
                news.setNewsId(rs.getString("newsid"));
                news.setClassId(rs.getString("CLASSID"));
                news.setKindId(rs.getString("KINDID"));
                news.setMyOther(rs.getString("MYOTHER"));
                news.setHeadTitle(rs.getString("HEADTITLE"));
                news.setContent(rs.getString("CONTENT"));
                news.setConnectRealtive(rs.getString("CONNECTREALTIVE"));
                news.setAuthor(rs.getString("AUTHOR"));
                news.setEditor(rs.getString("EDITOR"));
                news.setNewsFrom(rs.getString("NEWSFROM"));
                news.setTop(rs.getString("TOP"));
                news.setNewsTime(rs.getString("NEWSTIME"));
                news.setHits(rs.getString("HITS"));
                news.setState(rs.getString("STATE"));
                news.setTag(rs.getString("TAG"));
                newsRealese.add(news);
            }
            rs.close();
            stat.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            try {//關閉連線
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (Exception ex) {
            }
            return newsRealese;
        }
    }
    //查詢一個訊息
    public ArrayList queryOneNews(int newsid) throws Exception {
        Connection conn = null;
        ArrayList newsRealese = new ArrayList();
        int temp_id = newsid;
        try {
            //獲取連線  
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@localhost:1521:orcl";
            conn = DriverManager.getConnection(url, "scott", "wjk139");
            //執行SQL語句 
            Statement stat = conn.createStatement();
            String sql = "select * from newmessage where newsid=?";//獲取newsid,使用?代替字串,是一種預編譯的提交方式
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, Integer.valueOf(newsid));
            ResultSet rs = ps.executeQuery();// 之前已經給了sql字串,所以executeQuery是無參的。              
            while (rs.next()) {   //例項化VO
                NewsRealese news = new NewsRealese();
                news.setNewsId(rs.getString("newsid"));
                news.setClassId(rs.getString("CLASSID"));
                news.setKindId(rs.getString("KINDID"));
                news.setMyOther(rs.getString("MYOTHER"));
                news.setHeadTitle(rs.getString("HEADTITLE"));
                news.setContent(rs.getString("CONTENT"));
                news.setConnectRealtive(rs.getString("CONNECTREALTIVE"));
                news.setAuthor(rs.getString("AUTHOR"));
                news.setEditor(rs.getString("EDITOR"));
                news.setNewsFrom(rs.getString("NEWSFROM"));
                news.setTop(rs.getString("TOP"));
                news.setNewsTime(rs.getString("NEWSTIME"));
                news.setHits(rs.getString("HITS"));
                news.setState(rs.getString("STATE"));
                news.setTag(rs.getString("TAG"));
                newsRealese.add(news);
            }
            rs.close();
            stat.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            try {//關閉連線
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (Exception ex) {
            }
            return newsRealese;
        }
    }
    //刪除資料
        public String deleteOneNews(int newsid) throws Exception {
        Connection conn = null;
        ArrayList newsRealese = new ArrayList();
        int temp_id = newsid;
        try {
            //獲取連線  
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@localhost:1521:orcl";
            conn = DriverManager.getConnection(url, "scott", "wjk139");
            //執行SQL語句 
            Statement stat = conn.createStatement();
            String sql = "DELETE FROM  newmessage WHERE newsid =?";//獲取newsid,使用?代替字串,是一種預編譯的提交方式
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, Integer.valueOf(newsid));
            ResultSet rs = ps.executeQuery();// 之前已經給了sql字串,所以executeQuery是無參的。              
            rs.close();
            stat.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            try {//關閉連線
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (Exception ex) {
            }
            return newsRealese.toString();
        }
    }
    //插入資料
    public String insertOneNews(ArrayList addnews_list) throws Exception {
        Connection conn = null;
        /* for(int i=0,j=1;i<addnews_list.size();i++,j++)
         {
                     
         System.out.println("j:"+j+"值:"+addnews_list.get(i).toString());
         }*/
        try {
            //獲取連線  
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@localhost:1521:orcl";
            conn = DriverManager.getConnection(url, "scott", "wjk139");
            //執行SQL語句 
            Statement stat = conn.createStatement();
            String sql = "insert into newmessage(newsId,classId,kindId,myOther,headTitle,content,connectRealtive,author,editor,newsFrom"
                    + ",top,newsTime,hits,state,tag) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, Integer.valueOf(addnews_list.get(0).toString()));
            ps.setInt(2, Integer.valueOf(addnews_list.get(1).toString()));
            ps.setInt(3, Integer.valueOf(addnews_list.get(2).toString()));
            ps.setInt(4, Integer.valueOf(addnews_list.get(3).toString()));
            ps.setString(5, addnews_list.get(4).toString());
            ps.setString(6, addnews_list.get(5).toString());
            ps.setString(7, addnews_list.get(6).toString());
            ps.setString(8, addnews_list.get(7).toString());
            ps.setString(9, addnews_list.get(8).toString());
            ps.setString(10, addnews_list.get(9).toString());
            ps.setInt(11, Integer.valueOf(addnews_list.get(10).toString()));
            ps.setString(12, addnews_list.get(11).toString());
            ps.setString(13, addnews_list.get(12).toString());
            ps.setString(14, addnews_list.get(13).toString());
            ps.setString(15, addnews_list.get(14).toString());
            for (int i = 0, j = 1; i < addnews_list.size(); i++, j++) {
                System.out.println("j:" + j + "值:" + addnews_list.get(i).toString());
            }
            int i = ps.executeUpdate();
            conn.commit();
            System.out.println("成功新增" + i + "行");
            stat.close();
            conn.close();
            return i + "conn:" + conn;
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            try {//關閉連線
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (Exception ex) {
            }
        }
        return conn.toString();
    }
    //更新資料
     public String updateOneNews(ArrayList addnews_list) throws Exception {
        Connection conn = null;
        /* for(int i=0,j=1;i<addnews_list.size();i++,j++)
         {
                     
         System.out.println("j:"+j+"值:"+addnews_list.get(i).toString());
         }*/
        try {
            //獲取連線  
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@localhost:1521:orcl";
            conn = DriverManager.getConnection(url, "scott", "wjk139");//不安全
            //執行SQL語句 
            Statement stat = conn.createStatement();
            String sql = "UPDATE newmessage set classId=?,kindId=?,myOther=?,headTitle=?,content=?,"
          + "connectRealtive=?,author=?,editor=?,newsFrom=?,top=?,newsTime=?,hits=?,state=?,tag=? where newsid=?";  
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, Integer.valueOf(addnews_list.get(1).toString()));
            ps.setInt(2, Integer.valueOf(addnews_list.get(2).toString()));
            ps.setInt(3, Integer.valueOf(addnews_list.get(3).toString()));
            ps.setString(4, addnews_list.get(4).toString());
            ps.setString(5, addnews_list.get(5).toString());
            ps.setString(6, addnews_list.get(6).toString());
            ps.setString(7, addnews_list.get(7).toString());
            ps.setString(8, addnews_list.get(8).toString());
            ps.setString(9, addnews_list.get(9).toString());
            ps.setInt(10, Integer.valueOf(addnews_list.get(10).toString()));
            ps.setString(11, addnews_list.get(11).toString());
            ps.setString(12, addnews_list.get(12).toString());
            ps.setString(13, addnews_list.get(13).toString());
            ps.setString(14, addnews_list.get(14).toString());
            ps.setInt(15, Integer.valueOf(addnews_list.get(0).toString()));
            for (int i = 0, j = 1; i < addnews_list.size(); i++, j++) {
                System.out.println("j:" + j + "值:" + addnews_list.get(i).toString());
            }
            int i = ps.executeUpdate();
            conn.commit();
            System.out.println("成功更新" + i + "行");
            stat.close();
            conn.close();
            return i + "conn:" + conn;
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            try {//關閉連線
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (Exception ex) {
            }
        }
        return conn.toString();
    }
    public String ischecked(String user, String pass) throws Exception {
        Connection conn = null;
        //獲取連線  
        Class.forName("oracle.jdbc.driver.OracleDriver");
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        try {
            conn = DriverManager.getConnection(url,user, pass);//不安全
            //建立連線
            return conn.toString();
           } 
        catch (SQLException e) 
          {
           e.printStackTrace();
          }
          finally 
        {
               if (conn != null)
               {
                     try {
                            conn.close();
                          } catch (SQLException e)
                          {
                              e.printStackTrace();
                           }
         }
     }
     return null;
   } 
}

(2)首頁展示頁面的編寫,命名為index.html,由於可能有網路的延遲,我們使用漸進的方式跳轉(漸進方式跳轉:window.location.href="newRealese_brief.jsp",從index.html頁面跳轉到NewRealese_brief.jsp頁面,其實這裡的跳轉功能使用的是JavaScript中的window物件進行跳轉的),程式碼如下所示: