要做一个简单的保存网页界面,首先用jsp写出保存界面,本次界面比较简单,首先是三个提示语,后面是三个输入框,然后是保存按钮。
后台我写了两个java文件,第一个Bean.java是用来连接数据库以及更新的,第二个Update.java文件是用来更新表,记录界面输入的结果,所以在这个文件里需要判断输入的格式是否正确,比如任课教师只能输入王建民,刘力嘉,刘丹等等,所以这个文件里需要加入判断语句,最后我又加了一个jsp文件,提示输入正确与否,如果输入格式不正确,会有提示,如果输入正确,会提示保存成功 。
源代码:
Bean.java:
import java.sql.*;
public class Bean {String userName = "root";String userPwd = "24365426";String connStr = "jdbc:mysql://localhost:3306/ClassTest";String driverStr= "com.mysql.jdbc.Driver";private Connection conn=null;private Statement stmt=null;//Statement 可以执行数据库查询 更新语句
public Bean()
{try{Class.forName(driverStr);conn = DriverManager.getConnection(connStr,userName, userPwd);//conn是 数据库链接地址stmt = conn.createStatement();}catch(Exception ex){System.out.println("数据库连接失败");}
}public int executeUpdate(String s){int result=0;try{result=stmt.executeUpdate(s);//更新固定返回0和1}catch(Exception ex){System.out.println("更新失败"+ex.getMessage());}return result;}public ResultSet executeQuery(ResultSet rs, String s){try{rs=stmt.executeQuery(s);}catch(Exception ex){System.out.println("查询失败"+ex);}return rs;}public void close(){try{stmt.close();conn.close();}catch(Exception e){}}
}
Update.java:
import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@WebServlet("/Login")
public class Update extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public Update() {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 stubresponse.getWriter().append("Served at: ").append(request.getContextPath());}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubrequest.setCharacterEncoding("utf-8"); response.setHeader("Content-type", "text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8");PrintWriter out = response.getWriter();String username = request.getParameter("username");String homeaddress=request.getParameter("address");if(!homeaddress.equals("王建民")||!homeaddress.equals("刘力嘉")||!homeaddress.equals("刘丹")||!homeaddress.equals("王辉")||!homeaddress.equals("杨子光")){out.print("");}String Pname =request.getParameter("Pname");String miaoshu=request.getParameter("miaoshu");if(homeaddress.equals("王建民")||homeaddress.equals("刘力嘉")||homeaddress.equals("刘丹")||homeaddress.equals("王辉")||homeaddress.equals("杨子光")) {String s = "insert into classtest(username,homeaddress,Pname,miaoshu) values ('"+username+"','"+homeaddress+"','"+Pname+"','"+miaoshu+"')";Bean db= new Bean();int i = db.executeUpdate(s);if(!homeaddress.equals("王建民")||!homeaddress.equals("刘力嘉")||!homeaddress.equals("刘丹")||!homeaddress.equals("王辉")||!homeaddress.equals("杨子光")){out.print("错误");}if(i==1){out.print("");//脚本语言}else{out.println("");} }}
}
Login.jsp:
<%&#64; page language&#61;"java" contentType&#61;"text/html; charset&#61;utf-8"pageEncoding&#61;"utf-8"%>
课程名称 上课教师 上课地点
LoginSuccess.jsp:
<%&#64; page language&#61;"java" contentType&#61;"text/html; charset&#61;utf-8"pageEncoding&#61;"utf-8"%>
保存成功
运行截图&#xff1a;