j2ee从数据库中选择数据是不工作的。

问题描述 投票:0回答:1

我试图在j2ee.在servlet中创建一个登录页面.我试图检查输入值与数据库与select查询,但它得到false.所以我试图像这样输入值在数据库表中,但它仍然是假的.输出仍然是假的,即使我的用户表数据是完全相同的.请我需要解决这个问题,我卡住了。

enter image description here

java java-ee-6 java-ee-7 java-ee-8
1个回答
0
投票

我试过这个代码为我工作

Mysql 8连接器

避免使用class.forName(),它是可选的,你的连接中缺少密码,请检查下面的代码。

`public static void main(String[] args) { / 1.创建连接 String String url = "jdbc:mysql:/localhost:3306test"; / 2.ADD USER CREDENTIALS String username = "root"; String password = ""; String sql = "SELECT * FROM users WHERE username=? AND password=?;"。

    try(Connection con = DriverManager.getConnection(url, username, password);
        PreparedStatement statement = con.prepareStatement(sql);
        ){
    // 3. JDBC CONNECTIONS
        statement.setString(1, "test");
        statement.setString(2, "test123");
    // 4. STATEMENT FROM CONNECTION
        ResultSet rs = statement.executeQuery();
    // 5. GET RESULT SET FROM STATEMENT
        System.out.println(rs.next());


    }catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // 6. CLOSE ALL THE CONNECTIONS

}`

0
投票

登录控制器如下

    package com.vinay.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.vinay.dao.LoginDao;
import com.vinay.dao.impl.LoginDoaImpl;

/**
 * Servlet implementation class LoginController
 */
@WebServlet("/login")
public class LoginController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static LoginDao loginDao = null;   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginController() {
        super();
        loginDao = new LoginDoaImpl();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName  = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(loginDao.login(userName, password));
    }

}

webappjsp下的jsp文件。

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>
    <form  action="login" method="post">
        <label for="username">UserName</label>
        <input type="text" id="username" placeholder="Usernmae" name="username">
        <label for="password">Password</label>
        <input type="password" id="password" placeholder="Password" name="password">
        <input type="submit" value="Login">
    </form>
</body>
</html>

Doa班

package com.vinay.dao;

public interface LoginDao {
    boolean login(String userName, String password);
}

DBConnectionManager

package com.vinay.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnectionManager {

    private static final String URL = "jdbc:mysql://localhost:3306/test";
    private static final String USER_NAME = "";
    private static final String PASSWORD = "";

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } 
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER_NAME, PASSWORD);
    }

}

DAO实现类

package com.vinay.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.vinay.dao.DBConnectionManager;
import com.vinay.dao.LoginDao;

public class LoginDoaImpl implements LoginDao {

    @Override
    public boolean login(String userName, String password) {
        String sql = "SELECT * FROM users WHERE username=? AND password=?;";
        try(Connection connection = DBConnectionManager.getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);){
            statement.setString(1, userName);
            statement.setString(2, password);
            return statement.execute();
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.vinay</groupId>
    <artifactId>testj2eewebapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>testj2eewebapp Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
            <scope>provided</scope>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>testj2eewebapp</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven 
                defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

我尝试使用maven项目

试着为你的应用程序编写代码,你需要在这里 由于驱动类没有被初始化,所以在静态块中加载驱动。. 因此它会抛出异常。希望这段代码对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.