• 软件工程概论课堂测试一


    设计思想:首先写一个连接到数据库的方法,写一个用户类,其中包括三个string属性,课堂名称,任课教师,上课地点和gei,set方法。写一个添加的函数,把这三个属性写进数据库。写两个jsp页面,一个是显示界面的jsp,一个是有java代码的jsp,进行数据处理。

    源程序代码:

    <%@page import="java.util.Map"%>
    <%@page import="com.jaovo.msg.Util.ValidateUtil"%>
    <%@ 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>
    
         <title>课堂练习</title>
    </head>
    <body>
     
    
     
         <form action="add.jsp" method="get">
         <table align="center" border="0" width="65%">
            <tr> 
            <th >课程名称:</th>
            <td>
             <input type="text" name="s1"  />
            
            </td>
            </tr>
            <tr>
            <th>任课教师:</th>
            <td>
            <input type="text" name="s2"  />
            <%
            
            out.print(request.getAttribute("error1"));
            %>
             
            </td>
            </tr>
            <tr>
            <th>上课地点:</th>
            <td>
            <input type="text" name="s3"  />
           <%
            out.print(request.getAttribute("error2"));
            %>
            </td>
            </tr>
            <tr align="center" >
            <td colspan="2" >
            <input type="submit"  value="保存" />
            
            </td>
            </tr>
            
         </table>
         </form>
    </body>
    </html>
    <%@page import="com.jaovo.msg.Util.ValidateUtil"%>
    <%@page import="com.jaovo.msg.Util.UserException"%>
    <%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
    <%@page import="com.jaovo.msg.model.User"%>
    <%@ 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>
    
    <%
           String string1=request.getParameter("s1");
           String string2 =request.getParameter("s2");
           String string3 =request.getParameter("s3");
           System.out.print(string2);
           System.out.print(string3);
        
           if(!(string2.equals("刘立嘉")||string2.equals("王建民")||string2.equals("王辉")||string2.equals("刘丹")||string2.equals("杨子光"))){
               request.setAttribute("error1", "错误");
               
           
           %>
           <jsp:forward page="addinput.jsp"></jsp:forward>
           <% 
           }
           
           if(!(string3.equals("一教")||string3.equals("基教")||string3.equals("二教")||string3.equals("三教"))){
               request.setAttribute("error2", "错误");
              
               %>
               <jsp:forward page="addintput.jsp"></jsp:forward>
               <% 
           }
         
           System.out.print(string1);
           System.out.print(string2);
           System.out.print(string3);
           User user=new User();
           user.setString1(string1);
           user.setString2(string2);
           user.setString3(string3);
           UserDaoImpl userDaoImpl=new UserDaoImpl();
           userDaoImpl.add(user);
    
     %>
    
       <h2 style="color:black ; font-size : 60px">提交成功!!</h2>                             <br>                                     
       <a href="http://www.baidu.com">百度</a>
       <a href="addinput.jsp">重新登陆</a>
       <a href="#">用户列表</a>
       <%
           }catch(UserException e){
       %>
        <h2 style="color:green ; font-size :50px "><%=e.getMessage() %></h2>
      
       <%
        //} 
        %>
        
    
    </html>
    ackage com.jaovo.msg.Util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class DBUtil {
        public static Connection getConnection() {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String user = "root";
            String password="root";
            String url="jdbc:mysql://localhost:3306/jaovo_msg";
            Connection connection=null;
            try {
                connection=DriverManager.getConnection(url, user, password);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return connection;
        }
        public static void close(Connection connection) {
            try {
                if(connection!=null) {
                connection.close();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void close(PreparedStatement preparedStatement) {
            try {
                if(preparedStatement!=null) {
                preparedStatement.close();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void close(ResultSet resultSet) {
            try {
                if(resultSet!=null) {
                resultSet.close();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
     
    }
    package com.jaovo.msg.dao;
    
    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 com.jaovo.msg.Util.UserException;
    
    import com.jaovo.msg.Util.DBUtil;
    import com.jaovo.msg.model.User;
    
    public class UserDaoImpl implements IUserDao{
    
        @Override
        public void add(User user) {
            Connection connection=DBUtil.getConnection();
            PreparedStatement preparedStatement=null;
    
                String sql="insert into tuser2(课堂名称,任课教师,上课地点) value  (?,?,?)";
                try {
                System.out.println(user.getString1());
                System.out.println(user.getString2());
                System.out.println(user.getString3());
                preparedStatement=connection.prepareStatement(sql);
                preparedStatement.setString(1, user.getString1());
                preparedStatement.setString(2, user.getString2());
                preparedStatement.setString(3, user.getString3());
                preparedStatement.executeUpdate();
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
        }
    
        
    
    }
    ackage com.jaovo.msg.model;
    
    public class User {
        
        private String string1;
        private String string2;
        private String string3;
        public String getString1() {
            return string1;
        }
        public void setString1(String string1) {
            this.string1 = string1;
        }
        public String getString2() {
            return string2;
        }
        public void setString2(String string2) {
            this.string2 = string2;
        }
        public String getString3() {
            return string3;
        }
        public void setString3(String string3) {
            this.string3 = string3;
        }
        
    
    }

    运行结果截图:

     

     

     

  • 相关阅读:
    SQL Server 2008 Service Broker
    微软官网推Windows 7学习材料
    ASP.NET MVC Code and Slides for Best of Mix 09 Presentation
    DLINQ:SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM
    SQL Server 2008 Developer Training Kit
    TheBeerHouseASP.NET MVC范例
    SQL Server 2008 SP1
    LINQ: There is already an open DataReader associated with this Command which must be closed first
    Quartz.NET 1.0.1发布
    K2 Blackpearl的Outcomes Actions和Line Rule
  • 原文地址:https://www.cnblogs.com/andibier/p/7910814.html
Copyright © 2020-2023  润新知