代码如下,为什么连不上数据库? <%@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> 数据库
jsp登陆界面连接mysql数据库_jsp连接mysql数据库实现登录
最新推荐文章于 2022-05-06 18:08:20 发布
这段代码展示了如何使用JSP连接到MySQL数据库进行登录操作。通过定义数据库连接常量,加载驱动,建立连接,执行SQL查询并处理结果集来验证用户身份。如果连接和查询出现问题,代码中包含异常处理部分。
摘要由CSDN通过智能技术生成