首页 > 编程语言 >JAVAWEB学习-实现增删改查-03

JAVAWEB学习-实现增删改查-03

时间:2023-02-25 21:55:05浏览次数:40  
标签:03 String title text writer 改查 news public JAVAWEB

今天继续写之前的没写完的增删改查,完成增和查

代码

normal1.java

  1 package Test.test1;
  2 
  3 import javax.servlet.ServletException;
  4 import javax.servlet.annotation.WebServlet;
  5 import javax.servlet.http.HttpServlet;
  6 import javax.servlet.http.HttpServletRequest;
  7 import javax.servlet.http.HttpServletResponse;
  8 import java.io.IOException;
  9 import java.io.PrintWriter;
 10 import java.sql.*;
 11 
 12 @WebServlet("/normal1")
 13 public class normal1 extends HttpServlet {
 14 
 15     private static final long serialVersionUID = 1L;
 16     Connection conn = null;
 17     Statement stmt = null;
 18     @Override
 19     public void init() throws ServletException {
 20         String dburl = "jdbc:mysql://localhost:3306/test001?useSSL=false&serverTimezone=UTC";
 21         String username = "root";
 22         String password = "1506583922";
 23         try{
 24             Class.forName("com.mysql.cj.jdbc.Driver");
 25             conn = DriverManager.getConnection(dburl,username,password);
 26             stmt = conn.createStatement();
 27         }catch(SQLException se){
 28             // 处理 JDBC 错误
 29             se.printStackTrace();
 30         }catch(Exception e){
 31             // 处理 Class.forName 错误
 32             e.printStackTrace();
 33         }
 34     }
 35     public void addnews(String title,String text,String writer,String date){
 36 
 37         PreparedStatement preparedStatement=null;
 38         try {
 39             String sql = "INSERT INTO news_info (news_title,news_text,news_writer,news_date) values (?,?,?,?)";
 40             preparedStatement=conn.prepareStatement(sql);
 41             preparedStatement.setString(1,title);
 42             preparedStatement.setString(2,text);
 43             preparedStatement.setString(3,writer);
 44             preparedStatement.setString(3,date);
 45             preparedStatement.executeUpdate();
 46             //System.out.println("添加成功");
 47 
 48         } catch (SQLException  e) {
 49             e.printStackTrace();
 50         }finally{
 51 
 52         }
 53     }
 54     public void getnews(HttpServletResponse response){
 55         try {
 56             String sql;
 57             sql = "SELECT news_title news_text news_writer news_date FROM news_info";
 58             ResultSet rs = stmt.executeQuery(sql);
 59             response.setContentType("text/html;charset=UTF-8");
 60             PrintWriter out = response.getWriter();
 61             while (rs.next()){
 62                 String title = rs.getString("news_title");
 63                 String text = rs.getString("news_text");
 64                 String writer = rs.getString("news_writer");
 65                 String date = rs.getString("news_date");
 66 
 67                 out.println("标题: " + title);
 68                 out.println(", 内容: " + text);
 69                 out.println(", 作者: " + writer);
 70                 out.println(", 日期: " + date);
 71                 out.println("<br />");
 72             }
 73         }catch (Exception e){
 74             e.printStackTrace();
 75         }
 76 
 77     }
 78     @Override
 79     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 80 
 81         response.setContentType("text/html;charset=UTF-8");
 82         PrintWriter out = response.getWriter();
 83         String title = "普通用户界面:新闻浏览";
 84 
 85         String docType = "<!DOCTYPE html> \n";
 86         out.println(docType +
 87                 "<html>\n" +
 88                 "<head><title>" + title + "</title></head>\n" +
 89                 "<body bgcolor=\"#f0f0f0\">\n" +
 90                 "<h1 align=\"center\">" + title + "</h1>\n" +
 91                 "</ul>\n"
 92                 );
 93         getnews(response);
 94         out.println("<br />");
 95         out.println("</body></html>");
 96     }
 97     @Override
 98     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 99         doGet(request, response);
100     }
101     @Override
102     public void destroy() {
103         try {
104             conn.close();
105         }catch (Exception e){
106             e.printStackTrace();
107         }
108     }
109 }

Text.java

 1 package Test.com;
 2 
 3 public class Text {
 4     private String news_title,news_text,news_writer,news_date;
 5     public String getNews_title(){return news_title;}
 6     public String getNews_text(){return news_text;}
 7     public String getNews_writer(){return news_writer;}
 8     public String getNews_date(){return news_date;}
 9 
10     public void setNews_title(String news_title) {
11         this.news_title = news_title;
12     }
13 
14     public void setNews_text(String news_text) {
15         this.news_text = news_text;
16     }
17 
18     public void setNews_writer(String news_writer) {
19         this.news_writer = news_writer;
20     }
21 
22     public void setNews_date(String news_date) {
23         this.news_date = news_date;
24     }
25 }

 

标签:03,String,title,text,writer,改查,news,public,JAVAWEB
From: https://www.cnblogs.com/rsy-bxf150/p/17155519.html

相关文章