本文实例为大家分享了java学生寝室查询系统的具体代码,供大家参考,具体内容如下
前端部分:
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title >AHPU Freshman dormitory inquiry</ title >
< script src = "confirm.js" ></ script >
< style >
html,body{
margin:0px;
padding:0px;
}
.main{
position: relative;
width:500px;
height:500px;
background: url(image/ahpu1.jpg) no-repeat;
margin:10px auto;
}
form>div{
margin:20px 10px;
}
address{
position: absolute;
bottom:20px;
left:30px;
}
</ style >
</ head >
< body >
< div class = "main" >
< form action = "FindRoom/findInfo" method = "post" >
< div >
< label for = "sname" >姓名</ label >
< input type = "text" name = "sname" id = "sname" required>
</ div >
< div >
< label for = "sid" >学号</ label >
< input type = "text" name = "sid" id = "sid" required>
</ div >
< div >
< input id = "submit" type = "submit" >
< input id = "reset" type = "reset" >
</ div >
</ form >
< address >< a href = "javavscript:" rel = "external nofollow" >双微工作室</ a ></ address >
</ div >
</ body >
</ html >
|
confrim.js:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
var EventUtil={
addhandler: function (element,type,handler){
if (element.addEventListener){
element.addEventListener(type,handler, false );
} else if (element.attachEvent){
element.attachEvent( "on" +type,handler);
} else {
element[ "on" +type]=handler;
}
},
removehandler: function (element,type,handler){
if (element.removeEventListener){
element.removeEventListener(type,handler, false );
} else if (element.detachEvent){
element.detachEvent( "on" +type,handler);
} else {
element[ "on" +type]= null ;
}
},
getEvent: function (event){
return event?event:window.event;
},
getTarget: function (event){
return event.target || event.srcElement;
},
preventDefault: function (event){
if (event.preventDefault){
event.preventDefault();
} else {
event.returnValue= false ;
}
},
stopPropagation: function (event){ //只能阻止事件冒泡
if (event.stopPropagation){
event.stopPropagation();
} else {
event.cancelBubble= true ;
}
}
};
function confirmName(){
if (((sname.value).trim).length!=0){
reg=/^[\u4e00-\u9fa5]{1,10}$/;
if (!reg.test(sname.value)){
sname.value= "" ;
alert( "请输入正确的姓名" );
}
}
}
function confirmSid(){
if (((sid.value).trim()).length!=0){
reg=/^([0-9]){10,10}$/;
if (!reg.test(sid.value)){
sid.value= "" ;
alert( "请输入正确的学号" );
}
}
}
function confirm(){
EventUtil.addhandler(sname, "blur" ,confirmName);
EventUtil.addhandler(sid, "blur" ,confirmSid);
}
window.onload= function (){
var sname=document.querySelector( "#sname" );
var sid=document.querySelector( "#sid" );
var po=document.querySelector( "#submit" );
confirm();
EventUtil.addhandler(po, "submit" ,confrimName);
EventUtil.addhandler(po, "submit" ,confrimSid);
};
|
后端java部分:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
package FindRoom;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class findInfo extends HttpServlet {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver" ;
static final String DB_URL = "jdbc:mysql://localhost:3306/RoomQuery" ;
// 数据库的用户名与密码,需要根据自己的设置
static final String USER = "root" ;
static final String PASS = "cd7089028" ;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this .log( "执行doGet方法...." );
this .execute(request, response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
this .log( "执行doPost方法...." );
this .execute(request,response);
}
//执行方法
public void execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding( "utf-8" );
response.setCharacterEncoding( "utf-8" );
response.setContentType( "text/html" );
PrintWriter out = response.getWriter();
//StringBuffer requestURI=request.getRequestURL();
// String method=request.getMethod();
String sname=request.getParameter( "sname" );
String sid=request.getParameter( "sid" );
response.setContentType( "text/html" );
//RequestDispatcher dispatcher=request.getRequestDispatcher("/RoomQuery/findInfo");
Connection conn = null ;
Statement stmt = null ;
try {
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
// DriverManager.registerDriver(new com.mysql.jdbc.Driver());
// 打开链接
System.out.println( "连接数据库..." );
conn = DriverManager.getConnection(DB_URL,USER,PASS);
// 执行查询
System.out.println( " 实例化Statement对..." );
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM SumRoid where SId=" +sid;
ResultSet rs = stmt.executeQuery(sql);
// 展开结果集数据库
while (rs.next()){
// 通过字段检索
String flatNum=rs.getString( "FlatNum" );
String bedrootNum=rs.getString( "BedroomNum" );
String name = rs.getString( "Sname" );
String Sid = rs.getString( "SId" );
String ssex=rs.getString( "Ssex" );
String academy=rs.getString( "academy" );
String major=rs.getString( "major" );
// 输出数据
out.println( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">" );
out.println( " <HEAD><TITLE>A Servlet</TITLE></HEAD>" );
out.println( "<HTML>" );
out.println( "<BODY>" );
out.println( "<h3>" +sname+ "同学欢迎你</h3>" );
out.println( "<p>公寓号:" +flatNum+ "</p>"
+ "<p>寝室号: " + bedrootNum+ "</p>"
+ "<p>姓名: " + name+ "</p>"
+ "<p>学号: " + Sid+ "</p>"
+ "<p>性别: " + ssex+ "</p>"
+ "<p>学院: " + academy+ "</p>"
+ "<p>专业: " + major+ "</p>" );
}
out.println( "<BODY>" );
out.println( "</HTML>" );
// 完成后关闭
rs.close();
stmt.close();
conn.close();
out.flush();
out.close();
} catch (SQLException se){
// 处理 JDBC 错误
se.printStackTrace();
} catch (Exception e){
// 处理 Class.forName 错误
e.printStackTrace();
} finally {
// 关闭资源
try {
if (stmt!= null ) stmt.close();
} catch (SQLException se2){
} // 什么都不做
try {
if (conn!= null ) conn.close();
} catch (SQLException se){
se.printStackTrace();
}
}
}
}
|
数据库mysql部分:
将excel中学生的信息存放在A盘的stu.txt文件中;
创建table:
复制代码 代码如下:
create table SumRoid(FlatNum char(10),BedroomNum char(3),Sname char(10),SId char(15),Ssex char(2),academy char(30),major char(20));
3 . 将数据读入mysql数据库中:
load data local infile 'A:/stu.txt' into table SumRoid;
至此,一个超级简单的查询系统已完毕。
说明:
1. 在做表单输入验证时,bug很多;
2. jdbc的驱动一定要导入web-inf lib中;
3. web.xml配置一定要检查好;
4. 不能预防sql注入;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/chendongqaq/article/details/77649696