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

程序设计思想:
创建一个Bean.java文件,用来连接数据库.
创建一个add.jsp文件,用来进行表格的创建,并对教师姓名和上课地点进行判断,不符合要求的不能进行添加
创建一个index.jsp,显示以添加信息
程序源代码:
package DB;
import java.sql.*;
public class DBBean {
 
 private String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
 private String connStr ="jdbc:sqlserver://localhost:1433;DatabaseName=jaovo_msg";
 private String dbusername = "sa";
 private String dbpassword = "yyq838485140///";
 private Connection conn = null;
 private Statement stmt = null;
 public DBBean() {
  try {
   Class.forName(driverStr);
   conn = DriverManager.getConnection(connStr, dbusername, dbpassword);
   stmt = conn.createStatement();
   
  } catch (Exception ex) {
   //System.err.println("连接数据库失败 \n错误原因 "+ex.getMessage() );
  }
 }
 public int executeUpdate(String s) {
  int result = 0;
  try {
   
   result = stmt.executeUpdate(s);
  }
  catch (Exception ex)
  {
   System.err.println("执行更新错误\n错误原因  "+ex.getMessage());
  }
  return result;
 }
 public ResultSet  executeQuery(String s) {
  ResultSet rs = null;
  try {
   
   rs = stmt.executeQuery(s);
  }
  catch (Exception ex) {
   System.err.println("执行查询错误ִ\n错误原因 "+ex.getMessage());
  }
  return rs;
 }
 public void close() {
  try {
   stmt.close();
   conn.close();
  }
  catch (Exception e) {
   System.err.println("关闭失败ִ\n错误原因 "+e.getMessage());
  }
  System.out.println("已断开与数据库的连接!" );
 }
}
//add.jsp
<%@ page language="java" contentType="text/html; charset=gb2312" import="java.sql.*"
    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 style=" background-image:url(images/A1.jpg); background-repeat:no-repeat; background-position:center top; overflow:hidden;">

<form action="add.jsp"method="post">
<table align="center"width="50%" border="1">
<caption>添加课程信息</caption>
 <tr>
 <th width="30%">编号:</th>
 <td width="70%"><input name="id" type="text"></td>
 </tr>
 
 
 <tr>
 <th>课程名称:</th>
 <td ><input name="name" type="text"></td>
 </tr>
 
 <tr>
 <th>任课教师:</th>
 <td ><input name="password" type="text"></td>
 </tr>
 
 <tr>
 <th>上课地点:</th>
 <td ><input name="nickname" type="text"></td>
 </tr>
 
 
 <tr>
 <th colspan="2">
 <input type="submit" name="submit" value="提交">
 <input type="reset" value="重置">
 </th>
 </tr>
 </table>
 </form>
 <jsp:useBean id="db" class="DB.DBBean" scope="page" />
 <%
 request.setCharacterEncoding("gb2312");
 String submit=request.getParameter("submit");
 if(submit!=null&&!submit.equals(""))
 {   int id=Integer.parseInt(request.getParameter("id"));
  String name=request.getParameter("name");
  String password=request.getParameter("password");
  String nickname=request.getParameter("nickname");
 
  int c=0;
 if(password!=null&&(password.equals("王建民")||password.equals("刘立嘉")||password.equals("刘丹")||password.equals("王辉")||password.equals("杨子光")))
  {c=1;}
  else
  {
   out.println("<script language='javaScript'> alert('添加失败,任课教师不存在,单击确定返回添加主页!')</script>");
   c=0;
   response.setHeader("refresh", "1;url=index.jsp");
  }
  if(nickname!=null&&(nickname.startsWith("基教",0)||nickname.startsWith("一教",0)||nickname.startsWith("二教",0)||nickname.startsWith("三教",0)))
  {c=1;}
  else
  {
   out.println("<script language='javaScript'> alert('添加失败,上课地点错误,单击确定返回添加主页!')</script>");
   c=0;
   response.setHeader("refresh", "1;url=index.jsp");
  }
 
  if(c==1)
  {
  String sql="insert into t_user(id,username,password,nickname) values('"+id+"','"+name+"','"+password+"','"+nickname+"')";
  int i=db.executeUpdate(sql);
  if(i==1)
  {
   out.println("<script language='javaScript'> alert('添加成功,单击确定跳转到主页!')</script>");
      response.setHeader("refresh", "1;url=index.jsp");
  }
  else
  {
   out.println("<script language='javaScript'> alert('添加失败,单击确定返回添加主页!')</script>");
   response.setHeader("refresh", "1;url=index.jsp"); 
  }
  db.close();
  }
 
 }
 %>
</body>
</html>
//index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.sql.*"
    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 style=" background-image:url(images/A1.jpg); background-repeat:no-repeat; background-position:center top; overflow:hidden;">
 <center><a href=add.jsp>添加课程信息</a></center><p>
   <table align="center" width="50%" border=1>
   <tr><th>编号</th><th>课程名称</th><th>任课教师</th><th>上课地点</th><th>管理</th>
  
   <jsp:useBean id="db" class="DB.DBBean" scope="page" />
    <%
   String s="select * from t_user";
   ResultSet rs = db.executeQuery(s);
    while(rs.next())
   {
    int id=rs.getInt(1);
 out.println("<tr><td>"+rs.getInt(1)+"</td><td>"+rs.getString(2)+"</td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td><td><a href='edit.jsp?id="+id+"'>修改</a><a href='del.jsp?id="+id+"'>删除</a></td></tr>");
   }
    rs.close();
    db.close();
  
   %>
   </table>
 
  
</body>
</html>
程序结果截图:

 

存在问题:

没有准确找到添加判断的地点;

没有新建项目和数据库,是在原有的登录注册代码上改的,所以数据库名称不能对应

项目过程中给予帮助人员:寇肖萌

 

posted @ 2017-11-28 18:26  DaisyYuan  阅读(125)  评论(0编辑  收藏  举报