zoukankan      html  css  js  c++  java
  • 软件工程课堂测试2

    要做一个简单的保存网页界面,首先用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 stub
    		response.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 stub
    		request.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("<script language='javascript'>alert('教师姓名错误');window.location.href='Login.jsp';</script>");
    	}
    	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("<script language='javascript'>alert('保存成功');window.location.href='LoginSuccess.jsp';</script>");
    	//脚本语言
    	}
    	else
    		{
    		out.println("<script language='javaScript'> alert('保存失败,请从新添加');</script>");
    
    		}	
    	}
    
      } 
    }
    

      Login.jsp:

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     
    <title>登录界面</title>
     
    </head>
     
    <body>
     
      <form name = "form1" method = "post" action = "LoginSuccess.jsp" onsubmit = "return isValidate(form1)" >
    <table>
    <tr> <td>课程名称</td> <td><input type ="text" name="username"></td></tr>
    <tr> <td>上课教师</td>  <td><input type ="text" name="address"></td></tr>
    <tr> <td>上课地点</td>  <td><input type ="text" name="Pname"></td></tr>
    
    <tr><td colspan = "2"><input type = "submit" name = "submit" value = "保存" style = "80px;height:40px;font-size:20px;border:1;"></td>
     </tr>
    </table>
    
    </form>
      
     
    </body>
     
    </html>
    

      LoginSuccess.jsp:

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
      <h1>保存成功 </h1>
    </body>
    </html>
    

      运行截图:

  • 相关阅读:
    86. Partition List
    2. Add Two Numbers
    55. Jump Game
    70. Climbing Stairs
    53. Maximum Subarray
    64. Minimum Path Sum
    122. Best Time to Buy and Sell Stock II
    以场景为中心的产品设计方法
    那些产品经理犯过最大的错
    Axure教程:如何使用动态面板?动态面板功能详解
  • 原文地址:https://www.cnblogs.com/cj-125/p/7911753.html
Copyright © 2011-2022 走看看