我遵循了一个教程,但我更改了很多代码,所以在我的 DatabseHandler 中,我有连接、查询和执行
我没有遇到任何问题,直到这一刻我预计它会在 PgAdmin 中插入数据但是代码却给我发送了问题
数据库处理程序
package com.example.esginterface.database;
import java.sql.*;
import com.example.esginterface.config.Config;
import com.example.esginterface.config.Constants;
import javafx.scene.control.TextField;
public class DatabaseHandler extends Config {
private final static String url = "jdbc:postgresql://localhost:5432/esg-calculator";
private final static String user = "postgres";
private final static String password = "1234";
Connection dbConnection;
public Connection getDbConnection()
throws ClassNotFoundException, SQLException{
// String connectionString = "jdbc:postgresql://"+dbHost+":"+dbPort+"/"+dbName;
Class.forName("com.postgresql.jdbc.Driver");
// dbConnection = DriverManager.getConnection(connectionString,
// dbUser,dbPass);
dbConnection = DriverManager.getConnection(url, user, password);
return dbConnection;
}
public void signUpUser(String user_id, String firstname, String lastname, String login, String password, String email)
throws SQLException, ClassNotFoundException {
String insert = "INSERT INTO" + Constants.USER_TABLE + "(" + Constants.USER_ID + "," +
Constants.FIRSTNAME + "," + Constants.LASTNAME + "," +
Constants.LOGIN + "," +Constants.PASSWORD + "," + Constants.EMAIL + ")" + "VALUES(?,?,?,?,?,?)";
try {
PreparedStatement preparedStatement = getDbConnection().prepareStatement(insert);
preparedStatement.setString(1, user_id);
preparedStatement.setString(2, firstname);
preparedStatement.setString(3, lastname);
preparedStatement.setString(4, login);
preparedStatement.setString(5, password);
preparedStatement.setString(6, email);
preparedStatement.executeUpdate();
}
catch (SQLException | ClassNotFoundException e){
System.out.println("problem is in database handler "+e);
}
}
}
注册控制器
package com.example.esginterface.controller;
import java.net.URL;
import java.sql.SQLException;
import java.util.Date;
import java.util.ResourceBundle;
import com.example.esginterface.database.DatabaseHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class SignUpController {
@FXML
private TextField login_field;
@FXML
private TextField password_field;
@FXML
private Button sign_in_button;
@FXML
private TextField sign_up_email;
@FXML
private TextField sign_up_firstname;
@FXML
private TextField sign_up_lastname;
@FXML
void initialize() {
DatabaseHandler databaseHandler = new DatabaseHandler();
sign_in_button.setOnAction(actionEvent -> {
try {
databaseHandler.signUpUser(sign_in_button.getId(), sign_up_firstname.getText(),sign_up_lastname.getText(),login_field.getText(),password_field.getText(),sign_up_email.getText());
} catch (SQLException | ClassNotFoundException e) {
System.out.println("problem in sign up controller " + e);
}
});
}
}
登录控制器
package com.example.esginterface.controller;
import java.io.IOException;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class SignInController {
@FXML
private TextField login_field;
@FXML
private TextField password_field;
@FXML
private Button sign_in_button;
@FXML
private Button sign_up_button;
@FXML
void initialize() {
// sign_in.setOnAction(actionEvent -> {
// sign_in.getScene().getWindow().hide();
//
// FXMLLoader loader = new FXMLLoader();
// loader.setLocation(getClass().getResource("/com/example/esginterface/calculator.fxml"));
//
// try{
// loader.load();
// }
// catch (IOException e){
// e.printStackTrace();
// }
// Parent root = loader.getRoot();
// Stage stage = new Stage();
// stage.setScene(new Scene(root));
// stage.showAndWait();
//
//
// });
sign_in_button.setOnAction(actionEvent -> {
String loginText = login_field.getText().trim();
String passwordText = password_field.getText().trim();
if (!loginText.equals("")&&!passwordText.equals(""))
loginUser(loginText, passwordText);
else
System.out.println("err");
});
sign_up_button.setOnAction(actionEvent -> {
sign_up_button.getScene().getWindow().hide();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/com/example/esginterface/sign-up.fxml"));
try {
loader.load();
} catch (IOException e) {
e.printStackTrace();
}
Parent root = loader.getRoot();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.showAndWait();
});
}
private void loginUser(String loginText, String passwordText) {
}
}
注册.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="800.0" style="-fx-background-color: DAE3E2;" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.esginterface.controller.SignUpController">
<children>
<AnchorPane prefHeight="146.0" prefWidth="800.0" style="-fx-background-color: #2F4F4F;">
<children>
<Text fill="WHITE" layoutX="315.0" layoutY="85.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Trust Me">
<font>
<Font name="Times New Roman" size="48.0" />
</font>
</Text>
</children>
</AnchorPane>
<TextField fx:id="sign_up_firstname" layoutX="20.0" layoutY="222.0" prefHeight="60.0" prefWidth="370.0" promptText="Имя">
<font>
<Font name="Times New Roman Bold" size="20.0" />
</font>
</TextField>
<Button fx:id="sign_in_button" layoutX="319.0" layoutY="535.0" mnemonicParsing="false" prefHeight="46.0" prefWidth="177.0" text="Войти">
<font>
<Font name="Times New Roman" size="25.0" />
</font>
</Button>
<TextField fx:id="login_field" layoutX="408.0" layoutY="222.0" prefHeight="60.0" prefWidth="370.0" promptText="Логин">
<font>
<Font name="Times New Roman Bold" size="20.0" />
</font>
</TextField>
<TextField fx:id="sign_up_lastname" layoutX="20.0" layoutY="320.0" prefHeight="60.0" prefWidth="370.0" promptText="Фамилия">
<font>
<Font name="Times New Roman Bold" size="20.0" />
</font>
</TextField>
<TextField fx:id="password_field" layoutX="408.0" layoutY="320.0" prefHeight="60.0" prefWidth="370.0" promptText="Пароль">
<font>
<Font name="Times New Roman Bold" size="20.0" />
</font>
</TextField>
<TextField fx:id="sign_up_email" layoutX="215.0" layoutY="406.0" prefHeight="60.0" prefWidth="370.0" promptText="почта">
<font>
<Font name="Times New Roman Bold" size="20.0" />
</font>
</TextField>
</children>
</AnchorPane>
签到
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="800.0" style="-fx-background-color: DAE3E2;" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.esginterface.controller.SignInController">
<children>
<AnchorPane prefHeight="146.0" prefWidth="800.0" style="-fx-background-color: #2F4F4F;">
<children>
<Text fill="WHITE" layoutX="315.0" layoutY="85.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Trust Me">
<font>
<Font name="Times New Roman" size="48.0" />
</font>
</Text>
</children>
</AnchorPane>
<TextField fx:id="password_field" layoutX="235.0" layoutY="389.0" prefHeight="60.0" prefWidth="370.0" promptText="Пароль">
<font>
<Font name="Times New Roman Bold" size="20.0" />
</font>
</TextField>
<TextField fx:id="login_field" layoutX="233.0" layoutY="278.0" prefHeight="60.0" prefWidth="370.0" promptText="Логин">
<font>
<Font name="Times New Roman Bold" size="20.0" />
</font>
</TextField>
<Label layoutX="326.0" layoutY="214.0" text="Авторизация">
<font>
<Font name="Times New Roman Bold" size="28.0" />
</font>
</Label>
<Button fx:id="sign_in_button" layoutX="359.0" layoutY="508.0" mnemonicParsing="false" text="Войти">
<font>
<Font name="Times New Roman" size="25.0" />
</font>
</Button>
<Button fx:id="sign_up_button" layoutX="325.0" layoutY="604.0" mnemonicParsing="false" text="Регистрация">
<font>
<Font size="25.0" />
</font>
</Button>
</children>
</AnchorPane>