关于从IntelliJ将项目保存到数据库的问题

huangapple 未分类评论51阅读模式
英文:

A Problem regarding saving items into a database from intelliJ

问题

以下是翻译好的部分:

package com.Ashmal;

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

public class DBConnection {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/genius",
                "root", "$Can$123");
        System.out.println("已连接到数据库");
    }
}
package DBConnection;

public class Configs {

    protected static String dbhost = "localhost";
    protected static String dbport = "3306";
    protected static String dbuser = "root";
    protected static String dbpass = "$Can$123";
    protected static String dbname = "genius";
}
package DBConnection;

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

public class DBHandler extends Configs{

    Connection dbconnection;
    public Connection getConnection() {
        String connectionString = "jdbc:mysql://" + Configs.dbhost + ":" + Configs.dbport + "/"
                + dbname + "?autoReconnect=true&useSSL=false";
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            dbconnection = DriverManager.getConnection(connectionString, Configs.dbuser, dbpass);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dbconnection;
    }
}
package sample;

import javafx.animation.PauseTransition;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.util.Duration;
import DBConnection.DBHandler;

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

public class SignUpController {

    @FXML
    private AnchorPane parentPane;
    @FXML
    private Button login;
    @FXML
    private TextField name;
    @FXML
    private Button signUp;
    @FXML
    private RadioButton male;
    @FXML
    private ToggleGroup gender;
    @FXML
    private RadioButton female;
    @FXML
    private RadioButton other;
    @FXML
    private TextField locationField;
    @FXML
    private ImageView progress;
    @FXML
    private PasswordField password;
    private Connection connection;
    private DBHandler handler;
    private PreparedStatement pst;

    public void initialize() {
        progress.setVisible(false);
        handler = new DBHandler();
    }

    @FXML
    public void signUP() {
        progress.setVisible(true);
        PauseTransition pt = new PauseTransition();
        pt.setDuration(Duration.seconds(3));
        pt.setOnFinished(e -> System.out.println("注册成功!"));
        pt.play();

        // Saving Data
        String insert = "INSERT INTO youtubers(names,password,gender,locationField)"
                + "VALUES (?,?,?,?)";
        connection = handler.getConnection();
        try {
            pst = connection.prepareStatement(insert);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            pst.setString(1, name.getText());
            pst.setString(2, password.getText());
            pst.setString(3, getGender());
            pst.setString(4, locationField.getText());
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public String getGender() {
        String gen = "";
        if (male.isSelected()) {
            gen = "男";
        } else if (female.isSelected()) {
            gen = "女";
        } else if (other.isSelected()) {
            gen = "其他";
        }
        return gen;
    }
}
module GaveUp {

    requires javafx.fxml;
    requires javafx.controls;
    requires jlfgr;
    requires java.sql;
    requires mysql.connector.java;
    opens sample;
    opens img;
    opens DBConnection;

}
英文:

Ok, so I have been making an GUI application that should take the information from the User and save it into the sql database. This happens when I click the sign up button on the GUI but for some reason everything is working fine but the data is not showing up in the database. Here's the problem:

My Database Connection file:

package com.Ashmal;

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

public class DBConnection {

public static void main(String[] args) throws ClassNotFoundException, SQLException {

    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/genius",
            "root", "$Can$123");
    System.out.println("Connected to DataBase");
}
}

Here's my Configs class:

package DBConnection;

public class Configs {

protected static String dbhost = "localhost";
protected static String dbport = "3306";
protected static String dbuser = "root";
protected static String dbpass = "$Can$123";
protected static String dbname = "genius";
}

Here's my DataBase Handler class:

package DBConnection;

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

public class DBHandler extends Configs{

Connection dbconnection;
public Connection getConnection() {
    String connectionString = "jdbc:mysql://" + Configs.dbhost + ":" + Configs.dbport + "/"
            + dbname + "?autoReconnect=true&useSSL=false";
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    try {
        dbconnection = DriverManager.getConnection(connectionString, Configs.dbuser, dbpass);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return dbconnection;
}
}

Here's my Sign up controller class:

package sample;

import javafx.animation.PauseTransition;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.util.Duration;
import DBConnection.DBHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class SignUpController {

@FXML
private AnchorPane parentPane;
@FXML
private Button login;
@FXML
private TextField name;
@FXML
private Button signUp;
@FXML
private RadioButton male;
@FXML
private ToggleGroup gender;
@FXML
private RadioButton female;
@FXML
private RadioButton other;
@FXML
private TextField locationField;
@FXML
private ImageView progress;
@FXML
private PasswordField password;
private Connection connection;
private DBHandler handler;
private PreparedStatement pst;

public void initialize() {
    progress.setVisible(false);
    handler = new DBHandler();
}

@FXML
public void signUP() {
    progress.setVisible(true);
    PauseTransition pt = new PauseTransition();
    pt.setDuration(Duration.seconds(3));
    pt.setOnFinished(e -> System.out.println("Sign Up Successful!"));
    pt.play();

    // Saving Data
    String insert = "INSERT INTO youtubers(names,password,gender,locationField)"
            + "VALUES (?,?,?,?)";
    connection = handler.getConnection();
    try {
        pst = connection.prepareStatement(insert);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    try {
        pst.setString(1, name.getText());
        pst.setString(2, password.getText());
        pst.setString(3, getGender());
        pst.setString(4, locationField.getText());
        pst.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public String getGender() {
    String gen = "";
    if (male.isSelected()) {
        gen = "Male";
    } else if (female.isSelected()) {
        gen = "Female";
    } else if (other.isSelected()) {
        gen = "Other";
    }
    return gen;
}
}

Here's the module-info just in case:

module GaveUp {

requires javafx.fxml;
requires javafx.controls;
requires jlfgr;
requires java.sql;
requires mysql.connector.java;
opens sample;
opens img;
opens DBConnection;

}

huangapple
  • 本文由 发表于 2020年4月9日 21:33:03
  • 转载请务必保留本文链接:https://java.coder-hub.com/61122369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定