Jdbc连接数据库实验报告(2)

  1. 云栖社区>
  2. 博客>
  3. 正文

Jdbc连接数据库实验报告(2)

技术小阿哥 2017-11-27 17:29:00 浏览783
展开阅读全文

登录界面:

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>




b3d9d266ecc50975d026c3aa0420c8d2.png-wh_

1f4b84643afe008a81b1c570f1d0a5e3.png-wh_

5bc40e0bebb85a3bf79746199fd586db.png-wh_



本文转自 寂岚峰 51CTO博客,原文链接:http://blog.51cto.com/13271983/1975024,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论
技术小阿哥
+ 关注