今日学习进度:
第一天 | 第二天 | 第三天 | 第四天 | 第五天 | |
所花时间(小时) | 4.5 | 6 | |||
代码量(行) | 500 | 600 | |||
博客量(篇) | 1 | 1 | |||
了解到的知识点 | 顶会热词分析1 |
顶会热词分析2 |
Dbutil.java
package classes; 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; import java.util.List; import java.util.Scanner; public class Dbutil { private static String url = "jdbc:mysql://localhost:3306/lunwen?serverTimezone=UTC"; private static String user = "root"; private static String password = "root"; private static String jdbcName="com.mysql.cj.jdbc.Driver"; private Connection con=null; public static Connection getConnection() { Connection con=null; try { Class.forName(jdbcName); con=DriverManager.getConnection(url, user, password); System.out.println("数据库连接成功"); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("数据库连接失败"); e.printStackTrace(); } try { con = DriverManager.getConnection(url,user,password); System.out.println("连接成功"); } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); } return con; } public static void close(Connection con) { if(con!=null) try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(Statement state, Connection conn) { if(state!=null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet rs, Statement state, Connection conn) { if(rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(state!=null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
servlet.java
package classes; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /** * Servlet implementation class servlet */ @WebServlet("/servlet") public class servlet extends HttpServlet { private static final long serialVersionUID = 1L; Dao dao=new Dao(); /** * @see HttpServlet#HttpServlet() */ public servlet() { super(); // TODO Auto-generated constructor stub } public void findlunwen(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException, SQLException { req.setCharacterEncoding("utf-8"); String zhaiyao=req.getParameter("zhaiyao"); String title=req.getParameter("title"); String zuozhe = req.getParameter("zuozhe"); List<lunwenBean> list = dao.getselectlunwen(zhaiyao,title,zuozhe); PrintWriter out = response.getWriter(); JSONArray json=new JSONArray(); for(int i=0;i<list.size();i++) { JSONObject ob=new JSONObject(); ob.put("title", list.get(i).getTitle()); ob.put("zuozhe", list.get(i).getZuozhe()); ob.put("abstract", list.get(i).getZhaiyao()); ob.put("time", list.get(i).getTime()); ob.put("lianjie", list.get(i).getLianjie()); json.add(ob); } //System.out.println("JSON"+json.toString()); System.out.println("zongshu"+list.size()); out.write(json.toString()); } public void reci(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException { request.setCharacterEncoding("utf-8"); Map<String, Integer>sortMap=Dao.getrc(); JSONArray json =new JSONArray(); int k=0; for (Map.Entry<String, Integer> entry : sortMap.entrySet()) { JSONObject ob=new JSONObject(); ob.put("name", entry.getKey()); ob.put("value", entry.getValue()); if(!(entry.getKey().equals("for")||entry.getKey().equals("and")||entry.getKey().equals("With")||entry.getKey().equals("of")||entry.getKey().equals("in")||entry.getKey().equals("From")||entry.getKey().equals("A")||entry.getKey().equals("to")||entry.getKey().equals("a")||entry.getKey().equals("the")||entry.getKey().equals("by"))) { json.add(ob); k++; } if(k==10) break; } System.out.println(json.toString()); response.getWriter().write(json.toString()); } public void aja(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException { PrintWriter out = response.getWriter(); String name = request.getParameter("name").trim(); String type="-1"; try { response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); // out.println("Success---llll"); out.append(type); out.close(); } catch (Exception e){ e.printStackTrace(); } } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String method=request.getParameter("method"); if(method.equals("findlunwen")) { try { findlunwen(request,response); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } else if(method.equals("aja")) { try { aja(request,response); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } else if(method.equals("reci")) { try { reci(request,response); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
今天的运行还是会报错,无法获取到数据库中参数。