写了一个增加学生的项目。
jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.sql.*"%> <%@ page import="domain.User"%> <%@ page import="java.util.*"%> <!DOCTYPE html> <html> <head> <%ArrayList<User> list=(ArrayList<User>)request.getAttribute("list"); %> <meta charset="UTF-8"> <title>课程录入系统</title> <style> body{ background:url("file:///C:/Users/Lenovo/Desktop/long.jpg") no-repeat center; } </style> </head> <body> <div align="center"> <form action="${pageContext.request.contextPath}/addServlet" method="post" onsubmit="return checkall()"> <table> <tr> <td>课程名称</td> <td> <input type="text" name="ke_cheng" id="ke_cheng" value="" placeholder="请输入"/> </td> </tr> <tr> <td>任课老师</td> <td><input type="text" name="teacher" id="teacher" value="" placeholder="请输入"/></td> </tr> <tr> <td>上课地点</td> <td><input type="text" name="place" id="place" value="" placeholder="请输入"/></td> </tr> <tr> <td><input type="submit" value="保存"></td> </tr> </table> </form> <script type="text/javascript"> function checkall() { var num1=document.getElementById("ke_cheng"); var ke_cheng=num1.value; var num2= document.getElementById("teacher"); var teacher=num2.value; var num3 = document.getElementById("place"); var place=num3.value; if(ke_cheng=="") { alert('课程不能为空'); num1.focus(); return false; } if(teacher=="") { alert('教师不能为空'); num2.focus(); return false; } if(place=="") { alert('地点不能为空'); num3.focus(); return false; } } </script> </div> </body> </html>
dao:
package dao; import java.sql.*; import util.DBUtils; public class Userdao { static Connection conn = null; static PreparedStatement pst = null; static ResultSet rs = null; public static int addCourse(String name,String teacher,String place) { int count =0; String sql = "insert into stu value (?,?,?)"; try { conn = DBUtils.getConnection(); pst = conn.prepareStatement(sql); pst.setString(1,name); pst.setString(2, teacher); pst.setString(3, place); count =pst.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return count; } }
user:
package domain; public class User { private String name; private String teacher; private String place; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTeacher() { return teacher; } public void setTeacher(String teacher) { this.teacher = teacher; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } public User(String name, String teacher, String place) { super(); this.name = name; this.teacher = teacher; this.place = place; } public User() {} }
servlet:
package servlet; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; 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 javax.swing.JOptionPane; import dao.Userdao; import domain.User; import util.DBUtils; /** * Servlet implementation class AddServlet */ @WebServlet("/addServlet") public class AddServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public AddServlet() { 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 List<User> list=new ArrayList<User>(); request.setCharacterEncoding("UTF-8"); String addclass=request.getParameter("ke_cheng"); String addteacher=request.getParameter("teacher"); String addsite=request.getParameter("place"); User admin=null; Connection conn = DBUtils.getConnection(); try { Statement state = conn.createStatement(); String sql = "select * from stu"; ResultSet rs = state.executeQuery(sql); while(rs.next()) { admin = new User(); admin.setName(rs.getString("course")); admin.setTeacher(rs.getString("teacher")); admin.setPlace(rs.getString("place")); list.add(admin); if(admin.getName().equals(addclass)) { JOptionPane.showMessageDialog(null, "操作错误", "课程名称重复", JOptionPane.ERROR_MESSAGE); request.getRequestDispatcher("add.jsp").forward(request, response); return; } } request.setAttribute("list", list); System.out.println(list); rs.close(); }catch (Exception e) { e.printStackTrace(); }finally { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(addteacher.equals("王建民")||addteacher.equals("刘立嘉")||addteacher.equals("刘丹")||addteacher.equals("王辉")||addteacher.equals("杨子光")) { } else { JOptionPane.showMessageDialog(null, "操作错误", "没有这名教师", JOptionPane.ERROR_MESSAGE); request.getRequestDispatcher("add.jsp").forward(request, response); return; } if(addsite.equals("一教")||addsite.equals("二教")||addsite.equals("三教")||addsite.equals("基教")) {} else { JOptionPane.showMessageDialog(null, "操作错误", "上课地点错误", JOptionPane.ERROR_MESSAGE); request.getRequestDispatcher("add.jsp").forward(request, response); return; } Userdao u=new Userdao(); int n = 0; try { n = u.addCourse(addclass,addteacher,addsite); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(n!=0) { int res=JOptionPane.showConfirmDialog(null, "操作成功", "是否继续", JOptionPane.YES_NO_OPTION); if(res==JOptionPane.YES_OPTION){ request.getRequestDispatcher("add.jsp").forward(request, response); //点击“是”后执行这个代码块 }else{ System.out.println("选择否后执行的代码"); //点击“否”后执行这个代码块 return; } } else { JOptionPane.showMessageDialog(null, "操作错误", "请重新操作", JOptionPane.ERROR_MESSAGE); request.getRequestDispatcher("add.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); } }
util:
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtils { public static String db_url = "jdbc:mysql://localhost:3306/zy"; public static String db_user = "root"; public static String db_password = "123456"; public static Connection getConnection() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(db_url, db_user, db_password); } catch (Exception e) { e.printStackTrace(); } return conn; } 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(); } } } }