一个简单的java学生寝室查询系统
程序员文章站
2024-04-01 23:54:46
本文实例为大家分享了java学生寝室查询系统的具体代码,供大家参考,具体内容如下
前端部分:
index.html
...
本文实例为大家分享了java学生寝室查询系统的具体代码,供大家参考,具体内容如下
前端部分:
index.html
<!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:
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部分:
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注入;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。