intellij中的Java MySQL登录表单失败
我已经制作了一个与MySQL连接的javaFx登录表单,可以正常工作,但是当我尝试登录时,我会得到错误的名称和密码,我会提供我的代码和MySQL的屏幕截图,这样任何试图帮助的人都不会感到困惑intellij中的Java MySQL登录表单失败,java,mysql,database,jdbc,Java,Mysql,Database,Jdbc,我已经制作了一个与MySQL连接的javaFx登录表单,可以正常工作,但是当我尝试登录时,我会得到错误的名称和密码,我会提供我的代码和MySQL的屏幕截图,这样任何试图帮助的人都不会感到困惑 package sample; import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Group; import javafx.scene.Scene; import javafx
package sample;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DataBaseProject1 extends Application {
Connection conn;
PreparedStatement pst = null;
ResultSet rs = null;
@Override
public void start(Stage primaryStage) throws Exception
{
//GUIS a = new GUIS();
//a.createConnection();
//a.display();
DataBaseProject1 d = new DataBaseProject1();
d.createConnection();
primaryStage.setTitle("Retrive Database Values Into CheckBox");
//primaryStage.getIcons().add(new Image("file:user-icon.png"));
BorderPane layout = new BorderPane();
Scene newscene = new Scene(layout, 1200, 700, Color.rgb(0, 0, 0, 0));
Group root = new Group();
Scene scene = new Scene(root, 320, 200, Color.rgb(0, 0, 0, 0));
scene.getStylesheets().add(getClass().getResource("Style.css").toExternalForm());
Color foreground = Color.rgb(255, 255, 255, 0.9);
//Rectangila Background
Rectangle background = new Rectangle(320, 250);
background.setX(0);
background.setY(0);
background.setArcHeight(15);
background.setArcWidth(15);
background.setFill(Color.rgb(0 ,0 , 0, 0.55));
background.setStroke(foreground);
background.setStrokeWidth(1.5);
VBox vbox = new VBox(5);
vbox.setPadding(new Insets(10,0,0,10));
Label label = new Label("Label");
//label.setTextFill(Color.WHITESMOKE);
label.setFont(new Font("SanSerif", 20));
TextField username = new TextField();
username.setFont(Font.font("SanSerif", 20));
username.setPromptText("Username");
username.getStyleClass().add("field-background");
PasswordField password =new PasswordField();
password.setFont(Font.font("SanSerif", 20));
password.setPromptText("Password");
password.getStyleClass().add("field-background");
Button btn = new Button("Login");
btn.setFont(Font.font("SanSerif", 15));
btn.setOnAction(e ->{
try{
String user = username.getText();
String pass = password.getText();
String query = "SELECT * FROM userdatabasetable Where UserName = " + "'" + user + "'" + " AND Password = " + "'" +pass + "'" + " ";
rs = pst.executeQuery(query);
if(rs.next()){
label.setText("Login Successful");
primaryStage.setScene(newscene);
primaryStage.show();
}else{
label.setText("Login Failed");
}
username.clear();
password.clear();
pst.close();
rs.close();
}catch(Exception e1){
label.setText("SQL Error");
System.out.println("Wrong UserName Or Password");
//System.err.println(e1);
}
});
vbox.getChildren().addAll(label, username, password, btn);
root.getChildren().addAll(background, vbox);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
Connection createConnection ()
{
try
{
//Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/UserDataBase","yusof","1234");
System.out.println("DataBase Connected Successfully");
//con.close();
}
catch (ClassNotFoundException | SQLException ex)
{
Logger.getLogger(DataBaseProject1.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
输出:
数据库连接成功
错误的用户名或密码
MySQL的屏幕截图:您没有初始化PreparedStatement变量,只是用null值声明了
PreparedStatement pst=null代码>
所以当语句rs=pst.executeQuery(查询)时代码>执行,然后抛出错误。在catch块中,您只写了System.out.println(“错误的用户名或密码”)代码>。因此,您收到错误“错误的用户名或密码”
但实际的错误是在执行查询之前没有初始化PreparedStatementpst
变量
因此,初始化pst
变量以解决问题
如果您想知道如何使用prepared语句,那么您可以从这里看到
我已经解决了您代码中的所有问题,因此您可以简单地复制并粘贴下面的代码,希望它对您有所帮助
package sample;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DataBaseProject1 extends Application {
Connection conn;
PreparedStatement pst = null;
ResultSet rs = null;
@Override
public void start(Stage primaryStage) throws Exception
{
//GUIS a = new GUIS();
//a.createConnection();
//a.display();
DataBaseProject1 d = new DataBaseProject1();
d.createConnection();
primaryStage.setTitle("Retrive Database Values Into CheckBox");
//primaryStage.getIcons().add(new Image("file:user-icon.png"));
BorderPane layout = new BorderPane();
Scene newscene = new Scene(layout, 1200, 700, Color.rgb(0, 0, 0, 0));
Group root = new Group();
Scene scene = new Scene(root, 320, 200, Color.rgb(0, 0, 0, 0));
scene.getStylesheets().add(getClass().getResource("Style.css").toExternalForm());
Color foreground = Color.rgb(255, 255, 255, 0.9);
//Rectangila Background
Rectangle background = new Rectangle(320, 250);
background.setX(0);
background.setY(0);
background.setArcHeight(15);
background.setArcWidth(15);
background.setFill(Color.rgb(0 ,0 , 0, 0.55));
background.setStroke(foreground);
background.setStrokeWidth(1.5);
VBox vbox = new VBox(5);
vbox.setPadding(new Insets(10,0,0,10));
Label label = new Label("Label");
//label.setTextFill(Color.WHITESMOKE);
label.setFont(new Font("SanSerif", 20));
TextField username = new TextField();
username.setFont(Font.font("SanSerif", 20));
username.setPromptText("Username");
username.getStyleClass().add("field-background");
PasswordField password =new PasswordField();
password.setFont(Font.font("SanSerif", 20));
password.setPromptText("Password");
password.getStyleClass().add("field-background");
Button btn = new Button("Login");
btn.setFont(Font.font("SanSerif", 15));
btn.setOnAction(e ->{
try{
String user = username.getText();
String pass = password.getText();
String query = "SELECT * FROM userdatabasetable Where UserName = " + "'" + user + "'" + " AND Password = " + "'" +pass + "'" + " ";
d.pst=d.conn.prepareStatement(query);
rs = d.pst.executeQuery(query);
if(rs.next()){
label.setText("Login Successful");
primaryStage.setScene(newscene);
primaryStage.show();
}else{
label.setText("Login Failed");
}
username.clear();
password.clear();
d.pst.close();
rs.close();
}catch(Exception e1){
label.setText("SQL Error");
System.out.println("Wrong UserName Or Password");
//System.err.println(e1);
// e1.printStackTrace();
}
});
vbox.getChildren().addAll(label, username, password, btn);
root.getChildren().addAll(background, vbox);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
Connection createConnection ()
{
try
{
//Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/UserDataBase","yusof","1234");
System.out.println("DataBase Connected Successfully");
//con.close();
}
catch (ClassNotFoundException | SQLException ex)
{
Logger.getLogger(DataBaseProject1.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
我相信你仍然有很长的路要走。不仅在访问视图和数据库之间缺乏基本的逻辑分离,而且:
您在这里有一个异常的SQL注入漏洞:String query=“从userdatabasetable中选择*,其中UserName=“+”+”+“+”和Password=“+”+”+pass+”+”代码>
您没有使用scrypt、bcrypt或类似工具对数据库中的密码进行散列
正如其他人所说,你的错误处理能力很差
很抱歉说了这么严厉的话,但第一点和第二点是二十一世纪的致命罪行。请使用和将其固定
此外,请在漏洞修复后更新您的代码,这样我们就不会有漏洞代码的示例供他人复制和重用。如果我这样做,连接con=null;PreparedStatement pst=null;结果集rs=null;字符串user=username.getText().toString();字符串pass=password.getText().toString();String query=“从userdatabasetable中选择*,其中用户名=?和密码=?”;pst=con.prepareStatement(查询);pst.setString(1,用户);pst固定管柱(2,通过);rs=pst.executeQuery();同样的例外情况我也尝试了这种方法String query=“SELECT*FROM userdatabasetable,其中UserName=?和Password=?”;pst=con.prepareStatement(查询);pst.setString(1,用户);pst固定管柱(2,通过);rs=pst.executeQuery();我是MySql的初学者,这就是为什么我不能很快理解它的原因,但最后,我理解了它,我尝试了它,但它不起作用,因为你在项目中犯了很多错误,所以我检查了所有事情并解决了,因此,使用更新后的帖子不要抱歉,你的话实际上激励了我更加努力地工作,实际上感谢你的诚实,请发布实际的异常stacktrace,而不是你自己的错误消息。使用e1.printStackTrace()
@markrotterveel谢谢你,但我已经找到了解决问题的方法,再次感谢你的努力