您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了java學(xué)生寢室查詢系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
前端部分:
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">學(xué)號(hào)</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("請(qǐng)輸入正確的姓名"); } } } function confirmSid(){ if(((sid.value).trim()).length!=0){ reg=/^([0-9]){10,10}$/; if(!reg.test(sid.value)){ sid.value=""; alert("請(qǐng)輸入正確的學(xué)號(hào)"); } } } 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"; // 數(shù)據(jù)庫(kù)的用戶名與密碼,需要根據(jù)自己的設(shè)置 static final String USER = "root"; static final String PASS = "cd7089028"; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.log("執(zhí)行doGet方法...."); this.execute(request, response); } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ this.log("執(zhí)行doPost方法...."); this.execute(request,response); } //執(zhí)行方法 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{ // 注冊(cè) JDBC 驅(qū)動(dòng) Class.forName(JDBC_DRIVER); // DriverManager.registerDriver(new com.mysql.jdbc.Driver()); // 打開鏈接 System.out.println("連接數(shù)據(jù)庫(kù)..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); // 執(zhí)行查詢 System.out.println(" 實(shí)例化Statement對(duì)..."); stmt = conn.createStatement(); String sql; sql = "SELECT * FROM SumRoid where SId="+sid; ResultSet rs = stmt.executeQuery(sql); // 展開結(jié)果集數(shù)據(jù)庫(kù) 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"); // 輸出數(shù)據(jù) 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("<h4>"+sname+"同學(xué)歡迎你</h4>"); out.println("<p>公寓號(hào):"+flatNum+"</p>" +"<p>寢室號(hào): " + bedrootNum+"</p>" +"<p>姓名: " + name+"</p>" +"<p>學(xué)號(hào): " + Sid+"</p>" +"<p>性別: " + ssex+"</p>" + "<p>學(xué)院: " + academy+"</p>" +"<p>專業(yè): " + major+"</p>"); } out.println("<BODY>"); out.println("</HTML>"); // 完成后關(guān)閉 rs.close(); stmt.close(); conn.close(); out.flush(); out.close(); }catch(SQLException se){ // 處理 JDBC 錯(cuò)誤 se.printStackTrace(); }catch(Exception e){ // 處理 Class.forName 錯(cuò)誤 e.printStackTrace(); }finally{ // 關(guān)閉資源 try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ }// 什么都不做 try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } } }
數(shù)據(jù)庫(kù)mysql部分:
將excel中學(xué)生的信息存放在A盤的stu.txt文件中;
創(chuàng)建table:
3 . 將數(shù)據(jù)讀入mysql數(shù)據(jù)庫(kù)中:
load data local infile 'A:/stu.txt' into table SumRoid;
至此,一個(gè)超級(jí)簡(jiǎn)單的查詢系統(tǒng)已完畢。
說明:
1. 在做表單輸入驗(yàn)證時(shí),bug很多;
2. jdbc的驅(qū)動(dòng)一定要導(dǎo)入web-inf lib中;
3. web.xml配置一定要檢查好;
4. 不能預(yù)防sql注入;
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。