zoukankan      html  css  js  c++  java
  • java web 增加信息课堂测试00

    按照图片要求设计添加新课程界面。(0.5分)
    在后台数据库中建立相应的表结构存储课程信息。(0.5分)
    实现新课程添加的功能。
    要求判断任课教师为王建民、刘立嘉、刘丹、王辉、杨子光五位教师的其中一位。(0.5分)
    要求上课地点开头为“一教、二教、三教、基教”中的一种。(0.5分)
    实现数据存储功能。(3分)

    设计思想:绘制jsp界面 、编写功能类、信息类、工具类、连接数据库,利用传参取到web界面的输入内容添加到数据库。

    根据输入内容与要求判断的老师、地点对比判断。  查重课程: 从数据库中取值与键入web文本框内容对比,内容全部相同则存在课程。

    源代码:

    声明类

     1 public class test {
     2     private String name;
     3     private String teacher;
     4     private String area;
     5     public String getName() {
     6         return name;
     7     }
     8     public void setName(String name) {
     9         this.name = name;
    10     }
    11     public String getTeacher() {
    12         return teacher;
    13     }
    14     public void setTeacher(String teacher) {
    15         this.teacher = teacher;
    16     }
    17     public String getArea() {
    18         return area;
    19     }
    20     public void setArea(String area) {
    21         this.area = area;
    22     }
    23 }
    View Code

    接口

    1 import studytest.model.test;
    2 
    3 public interface  Itest {
    4     public void add(test test);
    5 }
    View Code

    数据库工具类

     1 import java.sql.Connection;
     2 import java.sql.DriverManager;
     3 import java.sql.PreparedStatement;
     4 import java.sql.ResultSet;
     5 import java.sql.SQLException;
     6 
     7 public class DBUtil {
     8     public static Connection getConnection() {
     9             try {
    10                 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    11             } catch (ClassNotFoundException e1) {
    12                 // TODO Auto-generated catch block
    13                 e1.printStackTrace();
    14             }
    15 
    16         String user="liu";
    17         String password="980130";
    18         String url="jdbc:sqlserver://localhost:1433; DatabaseName=Student";
    19         Connection connection =null;
    20         
    21         
    22         
    23             
    24                 try {
    25                     connection=DriverManager.getConnection(url, user, password);
    26                 } catch (SQLException e1) {
    27                     // TODO Auto-generated catch block
    28                     e1.printStackTrace();
    29                 }
    30             
    31             
    32         
    33         return connection;
    34         
    35     }
    36     //�ر���Դ
    37     public static void close(PreparedStatement preparedStatement) {
    38         
    39              try {
    40                  if( preparedStatement !=null) {
    41                 preparedStatement.close();
    42                  }
    43             } catch (SQLException e) {
    44                 // TODO Auto-generated catch block
    45                 e.printStackTrace();
    46             }
    47     }
    48     
    49     public static void close(ResultSet resultSet)  {
    50         
    51             try {
    52                 if(resultSet!= null) {
    53                 resultSet.close();
    54             }
    55             } catch (SQLException e) {
    56                 // TODO Auto-generated catch block
    57                 e.printStackTrace();
    58             }
    59     }
    60     
    61     public static void close(Connection connection) {
    62         // TODO Auto-generated method stub
    63         
    64             try {
    65                 if(connection!=null) {
    66                 connection.close();
    67                 }
    68                 } catch (SQLException e) {
    69                 // TODO Auto-generated catch block
    70                 e.printStackTrace();
    71             }
    72     }
    73 }
    123
     
     
     1 import java.sql.Connection;
     2 import java.sql.DriverManager;
     3 import java.sql.PreparedStatement;
     4 import java.sql.ResultSet;
     5 import java.sql.SQLException;
     6 
     7 public class DBUtil {
     8     public static Connection getConnection() {
     9             try {
    10                 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    11             } catch (ClassNotFoundException e1) {
    12                 // TODO Auto-generated catch block
    13                 e1.printStackTrace();
    14             }
    15 
    16         String user="**";
    17         String password="****";
    18         String url="jdbc:sqlserver://localhost:1433; DatabaseName=Student";
    19         Connection connection =null;
    20         
    21         
    22         
    23             
    24                 try {
    25                     connection=DriverManager.getConnection(url, user, password);
    26                 } catch (SQLException e1) {
    27                     // TODO Auto-generated catch block
    28                     e1.printStackTrace();
    29                 }
    30             
    31             
    32         
    33         return connection;
    34         
    35     }
    36     //�ر���Դ
    37     public static void close(PreparedStatement preparedStatement) {
    38         
    39              try {
    40                  if( preparedStatement !=null) {
    41                 preparedStatement.close();
    42                  }
    43             } catch (SQLException e) {
    44                 // TODO Auto-generated catch block
    45                 e.printStackTrace();
    46             }
    47     }
    48     
    49     public static void close(ResultSet resultSet)  {
    50         
    51             try {
    52                 if(resultSet!= null) {
    53                 resultSet.close();
    54             }
    55             } catch (SQLException e) {
    56                 // TODO Auto-generated catch block
    57                 e.printStackTrace();
    58             }
    59     }
    60     
    61     public static void close(Connection connection) {
    62         // TODO Auto-generated method stub
    63         
    64             try {
    65                 if(connection!=null) {
    66                 connection.close();
    67                 }
    68                 } catch (SQLException e) {
    69                 // TODO Auto-generated catch block
    70                 e.printStackTrace();
    71             }
    72     }
    73 }
    View Code

    异常处理类

     1 @SuppressWarnings("serial")
     2 public class testException extends RuntimeException {
     3     public testException() {
     4         super();
     5         // TODO Auto-generated constructor stub
     6     }
     7 
     8     public testException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
     9         super(message, cause, enableSuppression, writableStackTrace);
    10         // TODO Auto-generated constructor stub
    11     }
    12 
    13     public testException(String message, Throwable cause) {
    14         super(message, cause);
    15         // TODO Auto-generated constructor stub
    16     }
    17 
    18     public testException(String message) {
    19         super(message);
    20         // TODO Auto-generated constructor stub
    21     }
    22 
    23     public testException(Throwable cause) {
    24         super(cause);
    25         // TODO Auto-generated constructor stub
    26     }
    27 
    28 
    29 }
    View Code

    add类

     1 <%@page import="studytest.Util.testException"%>
     2 <%@page import="studytest.dao.testdaoimpl"%>
     3 <%@page import="studytest.model.test"%>
     4 <%@ page language="java" contentType="text/html; charset=UTF-8"
     5     pageEncoding="UTF-8"%>
     6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     7 <html>
     8 <head>
     9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    10 <title>添加</title>
    11 </head>
    12 <body>
    13 <%
    14 //接收客户端传递过来的参数
    15     
    16     String name    = request.getParameter("name");
    17     name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
    18     String teacher= request.getParameter("teacher");
    19     teacher = new String(teacher.getBytes("ISO-8859-1"), "UTF-8");
    20     String area    = request.getParameter("area");
    21     area= new String(area.getBytes("ISO-8859-1"), "UTF-8");
    22     //String[] tea={"王建民","刘立嘉","刘丹","杨辉","杨子光"};
    23     //String[] ar={"一教","二教","三教","基教"};
    24     test test=new test();
    25     test.setName(name);
    26     test.setTeacher(teacher);
    27     test.setArea(area);
    28      
    29     testdaoimpl testdao=new testdaoimpl();
    30     
    31     try{
    32         
    33         if((teacher.equals("王建民"))||(teacher.equals("刘立嘉"))||(teacher.equals("刘丹"))
    34                 ||(teacher.equals("王辉"))||(teacher.equals("杨子光")))
    35         {
    36         
    37             if((area.startsWith("基教"))||(area.startsWith("一教"))
    38             ||(area.startsWith("二教"))||(area.startsWith("三教"))){
    39                 testdao.add(test);
    40                 %>
    41                 <script type="text/javascript">
    42                 alert("课程添加成功!!");
    43                 </script>
    44                 <%    
    45             }
    46         }
    47     else{
    48         %>
    49         <script type="text/javascript">
    50         alert("发生错误");
    51         </script>
    52         
    53             <%
    54         }
    55     
    56     }catch(testException e){
    57         %>
    58         <h2 style="color:red ; font-size:20px">发生错误 : <%=e.getMessage() %></h2>
    59         <%
    60         }
    61         %>
    62         
    63 </body>
    64 </html>
    View Code

    addinput

     1 <%@page import="java.util.Map"%>
     2 <%@page import="com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg"%>
     3 <%@ page language="java" contentType="text/html; charset=UTF-8"
     4     pageEncoding="UTF-8"%>
     5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     6 <html>
     7 <head>
     8     <title>用户添加页面</title>
     9     <style>
    10 #header{
    11 background-color:#0000FF;
    12 color:white;
    13 text-align:left;
    14 padding:3px;
    15 }
    16 #section1{
    17 background-color:white;
    18 350px;
    19 text-align:center;
    20 margin: 0 auto;
    21 padding:10px;
    22 
    23 }
    24 </style>
    25 </head>
    26 <body bgcolor="pink" >
    27 <div id="header">
    28 <h1>用户注册</h1>
    29 </div>
    30 
    31 <br><br><br><br><br><br>
    32 <body>
    33 
    34     <form action="add.jsp" method="get">
    35         <table align="center" border="1" width="500">
    36             <tr>
    37                 <td>课程名称 : </td>
    38                 <td>
    39                     <input type="text" name="name" />
    40                     
    41                 </td>
    42             </tr>
    43                 <tr>
    44                 <td>任课教师:</td>
    45                 <td>
    46                     <input type="text" name="teacher" />
    47                 
    48             </tr>
    49             <tr>
    50                 <td>上课地点:</td>
    51                 <td>
    52                     <input type="text" name="area" />
    53         
    54                 </td>
    55             </tr>
    56             <tr align="center">
    57                 <td colspan="2">
    58                     <input type="submit"  style="background:#4DFFFF" value="保存" />
    59                 </td>
    60             </tr>
    61         </table>
    62     </form>
    63 </body>
    64 </html>
    View Code

    接口调用 连接数据库添加内容

     1 import java.sql.Connection;
     2 import java.sql.PreparedStatement;
     3 import java.sql.ResultSet;
     4 import java.sql.SQLException;
     5 
     6 import com.sun.xml.internal.bind.v2.runtime.Name;
     7 
     8 import studytest.Util.DBUtil;
     9 import studytest.Util.testException;
    10 import studytest.model.test;
    11 
    12 public class testdaoimpl implements Itest{
    13 
    14     @SuppressWarnings("resource")
    15     @Override
    16     public void add(test test) {
    17         // TODO Auto-generated method stub
    18         
    19         //获得连接对象
    20                 Connection connection=DBUtil.getConnection();
    21                 //准备sql语句
    22                 String s="select * from test where name=?";
    23                 //创建语句传输对象
    24                 PreparedStatement preparedStatement= null;
    25                 ResultSet resultSet =null;
    26                 try {
    27                     preparedStatement = connection.prepareStatement(s);
    28                     preparedStatement.setString(1, test.getName());    
    29                 //接收结果集
    30                     resultSet =preparedStatement.executeQuery();
    31                 
    32                     //遍历结果集
    33                     while(resultSet.next()) {
    34                         if(resultSet.getString("name").equals(test.getName())&&
    35                                 resultSet.getString("teacher").equals(test.getTeacher())&&
    36                                     resultSet.getString("area").equals(test.getArea())) {
    37                             throw new testException("课程存在 ");
    38                         }
    39                 }    
    40                     
    41                     s ="insert into test(name,teacher,area) values(?,?,?)";
    42                     preparedStatement =connection.prepareStatement(s);
    43                     preparedStatement.setString(1, test.getName());
    44                     preparedStatement.setString(2, test.getTeacher());
    45                     preparedStatement.setString(3, test.getArea());
    46                     preparedStatement.executeUpdate();
    47                 } catch (SQLException e) {
    48                     // TODO Auto-generated catch block
    49                     e.printStackTrace();
    50                 }finally {
    51                     DBUtil.close(resultSet);
    52                     DBUtil.close(preparedStatement);
    53                     DBUtil.close(connection);
    54 
    55                 }
    56                 
    View Code

    截图:

     

  • 相关阅读:
    经常出现null错误之tostring
    删除sql计划 调用的目标发生了异常。 (mscorlib) 其他信息: 用户 'sa' 登录失败。
    js打印保存用户输入的内容
    vs调试有时能进去后台,有时不能进去
    vs当前不会命中断点,还没有为该文档加载任何符号
    动软数据库文档生成器 失败错误码HRESULT:0x80010105 解决办法
    关于ckeditor过滤掉html样式标签之我见
    快速批量插入sqlserver方法之我见
    vs找不到svn源代码管理插件之我见
    有关大学,有关爱好,有关学习,有关奋斗,有关理想:大学应该干些什么?我大学三年以来的感悟
  • 原文地址:https://www.cnblogs.com/liulala2017/p/7911175.html
Copyright © 2011-2022 走看看