gpt4 book ai didi

java - 返回数据库中是否存在用户

转载 作者:行者123 更新时间:2023-11-29 13:14:30 24 4
gpt4 key购买 nike

我有一个 MySQL 数据库,其中有一个用户 ID 和一个密码列。他们每个人都有“疯狂”的值(value)。我有一些问题几天来一直在努力解决,因为我已经很长时间没有编程了。我有一个 JSP 页面,其中有我的表单和一个连接 MySQL 数据库的 servlet。当我在用户名和密码中输入 mads 时,我总是收到消息“您无效”。这意味着它没有给我答案“用户是有效的”,正如我想要的那样。我不想创建用户,我只想检查该用户是否存在于数据库中。我的 JSP 和 servlet 代码在这里:我希望有人可以帮助我,因为我真的不知道出了什么问题。

最诚挚的问候麦兹

<%@ page language="java" contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Validation</title>
<style type="text/css">
/* border-radius er rundt hjørner på input..*/
input[type=text] {padding:5px; border:2px solid #ccc; webkit-border-radius: 5px; border-radius:5px;}
input[type=text]:focus{border-color:yellow;}
input[type=password] {padding:5px; border:2px solid #ccc; webkit-border-radius: 5px; border-radius:5px;}
input[type=password]:focus{border-color:yellow;}
input[type=submit] {padding: 5px 15px; background:#ccc; border:0 none; cursor:pointer; webkit-border-radius:5px; border-radius: 5px;}
</style>
</head>
<body>
<br><br><br>
<center>
<h1>Please enter user name and password</h1>

<form name="frm" action="LoginValidation" method="post">
<input type="text" name="user">
<input type ="password" name="pass">
<input type="submit" value="Check" class="submit">
</form>
</center>
</body>

还有我的 Servlet:

import java.io.*;
//import java.util.*;
import java.sql.*;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.servlet.*;

@WebServlet(urlPatterns = {"/LoginValidation"})
public class Validation extends HttpServlet {

private static final long serialVersionUID = 1L;
private ServletConfig config;

public void init (ServletConfig config)
throws ServletException{
this.config = config;
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {


PrintWriter out = response.getWriter();
String connectionURL = "jdbc:mysql://localhost/dblogin";
Connection connection = null;
ResultSet rs;
String userid = request.getParameter("userid");
String password =request.getParameter("password");
////and your select statement

try{
String sql = "SELECT * FROM login WHERE userid = ? AND password = ?";
Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(connectionURL, "root", "");

PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, userid);
preparedStatement.setString(2, password);

Statement s = connection.createStatement();
rs =preparedStatement.executeQuery();

////if there are next ib rs so you have a user by this id and password
if(rs.next()) {
out.println("The user is valid");
}
else {
out.println("You are not valid");
}

}catch(Exception e) {
System.out.println("The exception is" + e);
}
}
}

最佳答案

可能的修复:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
PrintWriter out = response.getWriter();
String connectionURL = "jdbc:mysql://localhost/dblogin";
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
String userid = request.getParameter("user");
String password = request.getParameter("password");
response.setContentType("text/html");

try {
// Load the database driver
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "");
//Add the data into the database
String sql = "SELECT * FROM login WHERE userid = ? AND password = ?";

preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, userid);
preparedStatement.setString(2, password);

rs = preparedStatement.getResultSet();

if(rs.next()) {
// redirect or print but not both...
out.println("The user is valid");
// response.sendRedirect("index_true.jsp");
} else {
out.println("You are not valid");
}
} catch(Exception e) {
System.out.println("Exception is: " + e);
} finally {
// TODO: check for nullity
rs.close();
preparedStatement.close();
connection.close();
}
}

已修复:

  • 使用请求参数从数据库中获取记录
  • 执行正确的语句
  • ...

(可能仍然存在一些问题,尚未测试)

关于java - 返回数据库中是否存在用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21659278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com