1.程序设计思想
建立一个java文件,用于书写数据库的连接。建立一个jsp文件,用于书写注册页面的设置和输入的信息的捕获,并对教师姓名和上课地址进行判断。
2..源代码
//信1605-1张迪20163480 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 = "zd853243"; 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("杩炴帴鏁版嵁搴撳け璐� 閿欒�鍘熷洜 "+ex.getMessage() ); } } public int executeUpdate(String s) { int result = 0; try { result = stmt.executeUpdate(s); } catch (Exception ex) { System.err.println("鎵ц�鏇存柊閿欒� 閿欒�鍘熷洜 "+ex.getMessage()); } return result; } public ResultSet executeQuery(String s) { ResultSet rs = null; try { rs = stmt.executeQuery(s); } catch (Exception ex) { System.err.println("鎵ц�鏌ヨ�閿欒�执 閿欒�鍘熷洜 "+ex.getMessage()); } return rs; } public void close() { try { stmt.close(); conn.close(); } catch (Exception e) { System.err.println("鍏抽棴澶辫触执 閿欒�鍘熷洜 "+e.getMessage()); } System.out.println("宸叉柇寮�涓庢暟鎹�簱鐨勮繛鎺ワ紒" ); } }
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-color:#edf6fa; 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="添加">
</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 a=0;
if(password!=null&&(password.equals("王建民")||password.equals("刘立嘉")||password.equals("王辉")||password.equals("杨子光")||password.equals("刘丹")))
{
a=1;
}
else
{
out.println("<script language='javaScript'> alert('教师姓名不存在')</script>");
a=0;
response.setHeader("refresh", "1;url=index.jsp");
}
if(nickname!=null&&(nickname.startsWith("基教",0))||nickname.startsWith("一教",0)||nickname.startsWith("二教",0)||nickname.startsWith("三教",0))
{
a=1;
}
else
{
out.println("<script language='javaScript'> alert('上课地点不存在')</script>");
a=0;
response.setHeader("refresh", "1;url=index.jsp");
}
if (a==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>
4.截图
5.错误分析
现在程序中还有一点错误,就是显示的汉字是乱码。在写的过程中mysql因为不知名原因连不上,mysql现在还在修复中。
所以请教袁亚琴同学怎么使用sqlserver。还有对教师姓名和上课地址的判断有点错误,请教了姚雅丽同学这个问题,然后完善了我的程序。