Jdbc連接數據庫實驗報告(2)
登錄界面:
find.jsp:
<%@page contentType="text/html" import ="java.sql.*" pageEncoding="UTF-8"%>
<html>
<head><title>登錄</title></head>
<body>
<%
request.setCharacterEncoding("utf-8");
String id=request.getParameter("id");
String passwd=request.getParameter("passwd");
boolean flag=false;
PreparedStatement sql=null;
ResultSet rs=null;
Connection conn=null;
%>
<%
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String userPwd = "aaa312";
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 students_info where id=? and passwd=?");
sql.setString(1,id);
sql.setString(2,passwd);
rs=sql.executeQuery();
if (rs.next()) {
flag=true;
}
rs.close();
sql.close();
conn.close();
%>
<!-- 判斷是否是正確的登錄用戶 -->
<% if(flag==true)
{%>
<jsp:forward page="s.jsp"/>
<% }else response.sendRedirect("f.jsp");%>
</body>
</html>
s.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘welcome.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%String Name=request.getParameter("username");%>
歡迎你,<%=Name%>成功登錄!
</body>
</html>
f.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
對不起,你的信息有誤,請重新登錄! <br/>
<form action="login.jsp" method="post">
<input type="submit" value="返回登錄"/>
</form>
</body>
</html>
Jdbc連接數據庫實驗報告(2)