jsp登陆界面连接mysql数据库_jsp连接mysql数据库实现登录

这段代码展示了如何使用JSP连接到MySQL数据库进行登录操作。通过定义数据库连接常量,加载驱动,建立连接,执行SQL查询并处理结果集来验证用户身份。如果连接和查询出现问题,代码中包含异常处理部分。
摘要由CSDN通过智能技术生成

代码如下,为什么连不上数据库? <%@page contentType="text/html" pageEncoding="UTF-8" %>  <%@page import="java.sql.*"%>      <html>  <head>  </head>    <body>      <center>          <h1>登陆操作</h1>      </center>      <hr>      <%!//定义若干个数据库连接常量      public static final String DBDRIVER ="com.mysql.jdbc.Driver";      public static final String DBURL = "jdbc:mysql://localhost:3306/carmanage";      public static final String DBUSER = "root";      public static final String DBPASS = "root";      boolean flag = false;      String name = null;   %>      <%       Connection conn = null;  //数据库连接       PreparedStatement pstmt = null;    //数据库预处理操作       ResultSet rs = null;    //查询要处理结果集    %>      <%      try{        %>      <%         Class.forName(DBDRIVER);         conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);                 String sql = "select * from user where userid =? and password =?";           pstmt = conn.prepareStatement(sql);         pstmt.setString(1, request.getParameter("id"));                   pstmt.setString(2, request.getParameter("password"));           rs = pstmt.executeQuery();           if(rs.next()){//如果有数据就执行               flag = true;//表示登陆成功             name = rs.getString(1);                     }          System.out.println("如果有数据就执行22");     %>        <%     }catch(Exception e){         e.printStackTrace();     }finally{         try{             rs.close();             pstmt.close();             conn.close();         }catch(Exception e){                 e.printStackTrace();         }         }          %>        <%          if(flag == true){      %>      <jsp:forward page="login_success.jsp">          <jsp:param name="uname" value="<%=name %>" />      </jsp:forward>        <%             }else{           %>          <jsp:forward page="login_failure.jsp"/>          <%                       }                 %>    </body>  </html> 数据库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值