软件工程课堂测试一

1、设计思想

做一个接收信息的界面,并于后台界面连接,当接受数据后,判断输入消息的正误,并存入数据库中,跳转至对应的界面。

2、代码

(1)get.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="haha.panduan"%>
<%@page import="haha.store"%>
<% request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
//获取客户端传递过来的参数
String classname = request.getParameter("classname");
String teacher = request.getParameter("teacher");
String place = request.getParameter("place");
String jiequ=place.substring(0,2);
panduan pand=new panduan();
store s=new store();
int a=pand.pan(classname,teacher,jiequ);
if(a==0)
{
s.cun(classname,teacher,place);
%>
<<jsp:forward page="in.jsp"></jsp:forward>
<%
}else
%>
<<jsp:forward page="in1.jsp"></jsp:forward>

(2)NewFile.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=UTF-8">
<title>信息提交</title>
</head>
<body>
<form action="get.jsp" method="post">
课程名称 : <input type="text" name="classname" /><br>
任课教师 : <input type="text" name="teacher" /><br>
上课地点 : <input type="text" name="place" /><br>
<input type="submit" value="提交" />
</form>
</body>
</html>

(3)in.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=UTF-8">
<title>Insert title here</title>
</head>
<body>
登录成功
</body>
</html>

(4)in1.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=UTF-8">
<title>Insert title here</title>
</head>
<body>
登录失败
</body>
</html>

(5)link.java

package haha;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class link {

public static Connection get() {
// TODO 自动生成的方法存根

String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL数据库引擎
String connectDB= "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=User";//数据源

try
{
Class.forName(JDriver);//加载数据库引擎,返回给定字符串名的类
}catch(ClassNotFoundException e)
{
//e.printStackTrace();
System.out.println("加载数据库引擎失败");
System.exit(0);
}
System.out.println("数据库驱动成功");
Connection con=null;
try
{
String user="sa";
String password="1063861597";
con=DriverManager.getConnection(connectDB,user,password);//连接数据库对象
System.out.println("连接数据库成功");
java.sql.Statement stmt=con.createStatement();//创建SQL命令对象
}
catch(SQLException e)
{
e.printStackTrace();
//System.out.println("数据库连接错误");
System.exit(0);
}
return con;
}

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();
}
}
}

(6)panduan.java

package haha;

public class panduan {
public int pan(String classname,String teacher,String place)
{
System.out.println(place);
int xuan=0;
if(teacher.equals("王建民")||teacher.equals("刘丹")||teacher.equals("刘立嘉")||teacher.equals("王辉")||teacher.equals("杨子光"))xuan=0;
else return xuan=1;
if(place.equals("一教")||place.equals("二教")||place.equals("三教")||place.equals("基教"))xuan=0;
else return xuan=1;
return xuan;
}
}

(7)store.java

package haha;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import haha.link;

public class store {
public void cun(String classname,String teacher,String place) throws SQLException
{
Connection connection=null;
connection = link.get();
//准备sql语句
String sql = "select count(*) from t_user where username = ?";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
sql = "insert into haha(课程名称,任课教师,上课地点) values(?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, classname);
preparedStatement.setString(2, teacher);
preparedStatement.setString(3, place);
preparedStatement.executeUpdate();

link.close(resultSet);
link.close(preparedStatement);
link.close(connection);
}
}

3、

(1)项目计划日志

NewFile.jsp是主要的登录界面,get.jsp是接收数据的后台,link.jsp是连接数据库的文件,panduan.java是判断输入信息正确性的文件,store.java是将数据存入数据库的文件,in.jsp和in1.jsp是跳转后的界面。

(2)时间记录日志

上午将网页的大体框架和数据库连接成功,下午将 网页和数据库的储存之间建立联系。

(3)错误日志

代码在传输数据的时候,会把数据转换为乱码,这个时候就需要在文件开头添加<% request.setCharacterEncoding("UTF-8"); %>代码即可。

 

posted @ 2017-11-28 21:42  你说你好  阅读(100)  评论(0编辑  收藏  举报