创建新的Java项目——dynamic web project
创建新的packge,分别命名为dao(操作数据库) service(实体类) util(连接数据库代码) servlet(连接)
在webconnect下创建新的.jsp文件,用来编写网页布局和代码。
在lib文件夹下添加jar包,用来连接数据库。在jar包上右击build path 选择第一项。
//连接数据库 package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil { // 数据库连接地址 private static String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"; // 数据库的用户名 private static String UserName = "root"; // 数据库的密码 private static String Password = "123456"; public static Connection getConnection() { Connection Conn=null; try { Class.forName("com.mysql.cj.jdbc.Driver"); // 加载驱动 System.out.println("加载驱动成功!!!"); } catch (ClassNotFoundException e) { // TODO: handle exception e.printStackTrace(); } try { //通过DriverManager类的getConenction方法指定三个参数,连接数据库 Conn = DriverManager.getConnection(URL, UserName, Password); System.out.println("连接数据库成功!!!"); //返回连接对象 //return Conn; } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); } return Conn; } public static void main(String[] args)throws SQLException { //测试数据库是否连通 Connection conn = getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; String sql ="select * from teacher"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); System.out.println(getConnection()); while(rs.next()){ System.out.println("空"); } } }
package dao; import util.DBUtil; import java.sql.Connection; 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 service.Course; public class CourseDao { public List<Course> getcourse(){ List<Course>list=new ArrayList<Course>(); Course course=null; Connection conn=DBUtil.getConnection();//连接数据库 String sql="select * from teacher"; Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while(rs.next()) { course = new Course(); course.setid(rs.getInt("id")); course.setname(rs.getString("name")); course.setteacher(rs.getString("teacher")); course.setclassroom(rs.getString("classroom")); list.add(course); } } catch(SQLException e) { e.printStackTrace(); } return list; } public boolean addcourse(Course course) { String sql="insert into teacher('name','teacher','classroom')valus(?,?,?)"; Connection conn=DBUtil.getConnection(); try { PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, course.getname()); pst.setString(2, course.getteacher()); pst.setString(3, course.getclassroom()); int count = pst.executeUpdate(); pst.close(); return count>0?true:false; }catch(SQLException e) { e.printStackTrace(); }return false; } public boolean update(Course course) { String sql="update teacher set'name'=?,'teacher'=?,'classroom'=? where id=?"; Connection conn = DBUtil.getConnection(); try { PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, course.getname()); pst.setString(2, course.getteacher()); pst.setString(3, course.getclassroom()); int count = pst.executeUpdate(); pst.close(); return count>0?true:false; }catch(SQLException e) { e.printStackTrace(); }return false; } public boolean delete(int id) { String sql = "delete from user where id = ?"; Connection conn= DBUtil.getConnection(); try { PreparedStatement pst= conn.prepareStatement(sql); pst.setInt(1,id); int count = pst.executeUpdate(); pst.close(); return count>0?true:false; }catch(SQLException e) { e.printStackTrace(); }return false; } public Course selectUserById(int id){ Connection conn = DBUtil.getConnection(); String sql = "select * from user where id = "+id; Course course = null; try { PreparedStatement pst = conn.prepareStatement(sql); ResultSet rst = pst.executeQuery(); while (rst.next()) { course = new Course(); course.setid(rst.getInt("id")); course.setname(rst.getString("name")); course.setteacher(rst.getString("teacher")); course.setclassroom(rst.getString ("classroom")); } rst.close(); pst.close(); } catch (SQLException e) { e.printStackTrace(); } return course; } }
package servlet; import java.io.IOException; import java.util.List; 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 dao.CourseDao; import service.Course; /** * Servlet implementation class servlet */ @WebServlet("/servlet") public class ListServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ListServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); CourseDao dao =new CourseDao(); List<Course> list=dao.getcourse(); request.setAttribute("courseinfo", list); request.getRequestDispatcher("list.jsp").forward(request, response); } /** * @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); } }
<%@page import="dao.CourseDao"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="service.Course" %> <%@ page import="java.util.List" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>list</title> </head> <body> <% CourseDao courseDao = new CourseDao(); List<Course> list = courseDao.getcourse(); if (list == null || list.size() < 1) { %> <tr bgcolor="white"><td colspan="5" ><h4 align="center">没有数据</h4></td></tr> <% } else { // 遍历用户集合中的数据 for (Course course : list) { %> <tr align="center" bgcolor="white"> <td><%=course.getid()%></td> <td><%=course.getname()%></td> <td><%=course.getteacher()%></td> <td><%=course.getclassroom() %></td> </tr> <% } } %> </body> </html>