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>




b3d9d266ecc50975d026c3aa0420c8d2.png-wh_

1f4b84643afe008a81b1c570f1d0a5e3.png-wh_

5bc40e0bebb85a3bf79746199fd586db.png-wh_



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

相关文章
如何排查和解决PHP连接数据库MYSQL失败写锁的问题
通过本文的介绍,您可以系统地了解如何排查和解决PHP连接MySQL数据库失败及写锁问题。通过检查配置、确保服务启动、调整防火墙设置和用户权限,以及识别和解决长时间运行的事务和死锁问题,可以有效地保障应用的稳定运行。
146 25
Unity连接Mysql数据库 增 删 改 查
在 Unity 中连接 MySQL 数据库,需使用 MySQL Connector/NET 作为数据库连接驱动,通过提供服务器地址、端口、用户名和密码等信息建立 TCP/IP 连接。代码示例展示了如何创建连接对象并执行增删改查操作,确保数据交互的实现。测试代码中,通过 `MySqlConnection` 类连接数据库,并使用 `MySqlCommand` 执行 SQL 语句,实现数据的查询、插入、删除和更新功能。
使用 JDBC 实现 Java 数据库操作
JDBC(Java Database Connectivity)是 Java 提供的数据库访问技术,允许通过 SQL 语句与数据库交互。本文详细介绍了 JDBC 的使用方法,包括环境准备、编程步骤和完整示例。
208 7
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
99 2

热门文章

最新文章