JSP頁面連結資料庫
阿新 • • 發佈:2018-12-30
這是最簡單的使用JSP頁面連線MySql資料庫例項,程式碼如下:
註冊頁面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> <script> function check(){ var username = document.getElementById("username").value; var password = document.getElementById("password").value; var newword = document.getElementById("newword").value; if (username==" "){ alert("使用者名稱不能為空"); document.getElementById("username").focus(); return false; } if (password==" "){ alert("密碼不能為空"); document.getElementById("password").focus(); return false; } if (username!=newword){ alert("兩次密碼不相同,請重新輸入"); document.getElementById("newword").focus(); return false; } function validate1() { var flag = addCheck(); if (flag == false) return false; return true; </script> </head> <body bgcolor="green"> <center> <font face="楷體" size="6" color="#000">註冊介面</font> <form action="checkRegister.jsp" method="post" onsubmit="return validate1()"> <table width="300" height="180" border="5" bordercolor="#A0A0A0"> <tr> <th>使用者名稱:</th> <td><input type="text" name="username" value="輸入16個字元以內" maxlength="16" onfocus="if(this.value == '輸入16個字元以內') this.value =''"></td> </tr> <tr> <th>輸入密碼:</th> <td><input type="text" name="password" value="輸入20個字元以內" maxlength="20" onfocus="if(this.value == '輸入20個字元以內') this.value =''"></td> </tr> <tr> <th>確認密碼:</th> <td><input type="text" name="newword" value="請再次輸入密碼" maxlength="20" onfocus="if(this.value == '請再次輸入密碼') this.value =''"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="注 冊"> <input type="reset" value="重 置"></td> </tr> </table> </form> </center> </body> </html>
檢測註冊
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@page import="java.util.logging.*" %> <%@page import="java.sql.*" %> <%@page import="com.mysql.jdbc.Driver" %> <%@page import="java.util.Date" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% String username = new String(request.getParameter("username").getBytes("ISO8859_1"),"utf-8"); String password = new String(request.getParameter("password").getBytes("ISO8859_1"),"utf-8"); Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost/userdate?characterEncoding=utf-8"; //將解碼後的位元組碼重新按UTF-8格式編碼資料返回給客戶端。 String usename = "root"; String psw= "123456"; Connection conn = DriverManager.getConnection(url,usename,psw); //得到連線 PreparedStatement pStmt = conn.prepareStatement("select * from servlet where username = '" + username + "'"); ResultSet rs = pStmt.executeQuery(); if(rs.next()){ out.println("<script language='javascript'>alert('該使用者已存在,請重新註冊!');window.location.href='register.jsp';</script>"); }else{ PreparedStatement tmt = conn.prepareStatement("Insert into servlet values('" + username + "','" + password + "')"); int rst = tmt.executeUpdate(); if (rst != 0){ out.println("<script language='javascript'>alert('使用者註冊成功!');window.location.href='login.jsp';</script>"); }else{ out.println("<script language='javascript'>alert('使用者註冊失敗!');window.location.href='register.jsp';</script>"); } } %> </body> </html>
登陸頁面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body bgcolor="green"> <center> <font face="楷體" size="6" color="#000"><p> 登入介面 </p></font> <hr> <%String str=(String)request.getAttribute("msg"); if(str!=null){ out.println(str);} %> <form action="validate.jsp" method="post"> 使用者名稱 <input type="text" name="username" value="輸入您的使用者名稱" maxlength="16" onfocus=if(this.value=="輸入您的使用者名稱")this.value=""><br><br> 密 碼 <input type="password" name="password" maxlength="16"><br><br> <input type="submit" value="登入"> <input type="reset" value="重置"> <input type="button" onclick="window.open('register.jsp')" value=註冊> <br><br> </form> </center> </body> </html>
檢測登陸
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.logging.*" %>
<%@page import="java.sql.*" %>
<%@page import="com.mysql.jdbc.Driver" %>
<%@page import="java.util.Date" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String username=new String(request.getParameter("username").getBytes("ISO8859_1"),"utf-8");
String password=new String(request.getParameter("password").getBytes("ISO8859_1"),"utf-8");
String driverClass="com.mysql.jdbc.Driver";
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/userdate?characterEncoding=utf-8";
String usename = "root";
String psw= "123456";
Connection conn = DriverManager.getConnection(url, usename, psw);
if(conn != null){
String sql = "select * from servlet where username='"+username+"' and password='"+ password + "'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
out.println("<script language='javascript'>alert('登入成功!');window.location.href='welcom.jsp';</script>");
}else{
out.println("<script language='javascript'>alert('登入失敗!');window.location.href='Login.jsp';</script>");
//密碼不對返回到登陸
}
}
%>
</body>
</html>
歡迎頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>歡迎</p>
<a herf=login.jsp>重新登入</a>
</body>
</html>