網頁註冊登入(三)
阿新 • • 發佈:2018-11-09
登入檢驗
<%@ page language="java" import="java.sql.*" contentType="text/html;charset=utf-8"%>
<html><head>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String users=request.getParameter("username");
String pass=request.getParameter("pwd");
boolean flag=false;
PreparedStatement sql=null;
ResultSet rs=null;
Connection conn=null;
%>
<%
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String userPwd = "123456";
String dbName="students";
String url1 = "jdbc:mysql://localhost:3306/"+dbName;
String url2="?user="+userName+"&password="+userPwd;
String url3="&useUnicode=true&characterEncoding=utf-8";
String url=url1+url2+url3;
Class.forName(driverName);//載入驅動
conn= DriverManager.getConnection(url,userName,userPwd);
sql =conn.prepareStatement("select * from student where username=? and password=?");
sql.setString(1,users);
sql.setString(2,pass);
rs=sql.executeQuery();
if (rs.next()) {
flag=true;
}
rs.close();
sql.close();
conn.close();
%>
<!-- 判斷是否是正確的登入使用者 -->
<% if (flag==true){ %>
<jsp:forward page="suc.jsp"/>
<%}
else
%>
<jsp:forward page="fail.jsp"/>
</body>
</html>