首页 > 解决方案 > javafx 和数据库

问题描述

我正在使用 javafx 和 mysql-connector-java-5.1.45-bin 库。问题是我无法将新数据插入我的列。没有声明错误的红色下划线。它已执行,但没有向我的列中插入任何带有患者姓名的数据。但在患者中有 8 列。顺便说一下,教程中告诉它会将单个数据插入所有列。什么是问题?

这是我的代码(FXMLDocumentController.java):

package avicenna;


import connectivity.ConnectionClass;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class FXMLDocumentController implements Initializable {

    @FXML
    private Button signinbtn;

    @FXML
    private Button backto;

     @FXML
    private Button patients;

      @FXML
    private Button insert;

    @FXML
    private Label lpass;

    @FXML
    private Label lname;

     @FXML
    private Label lage;

    @FXML
    private Label lgender;

     @FXML
    private Label lregion;

    @FXML
    private Label ltest;

    @FXML
    private Label lresult;

    @FXML
    private Label lblood;

    @FXML
    private TextField ipass;

    @FXML
    private TextField iname;

     @FXML
    private TextField iage;

    @FXML
    private TextField igender;

     @FXML
    private TextField iregion;

    @FXML
    private TextField itest;

    @FXML
    private TextField iresult;

    @FXML
    private TextField iblood;

     @FXML
    void go1btn(ActionEvent event) throws Exception {
    try{
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("signin.fxml"));
            Parent root1 = (Parent) fxmlLoader.load();
            Stage stage = new Stage();
            stage.setTitle("Sign In");
            stage.setScene(new Scene(root1));  
            stage.show();
            Stage stageclose = (Stage) signinbtn.getScene().getWindow();
            stageclose.close();
          }
    catch(Exception e) {
           e.printStackTrace();
          }
    }

    @FXML
    void backtomain(ActionEvent event) throws Exception {
    try{
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
            Parent root1 = (Parent) fxmlLoader.load();
            Stage stage = new Stage();
            stage.setTitle("Main window");
            stage.setScene(new Scene(root1));  
            stage.show();
            Stage stageclose = (Stage) backto.getScene().getWindow();
            stageclose.close();
          }
    catch(Exception e) {
           e.printStackTrace();
          }
    }

    @FXML
    void patients(ActionEvent event) throws Exception {
    try{
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXML.fxml"));
            Parent root1 = (Parent) fxmlLoader.load();
            Stage stage = new Stage();
            stage.setTitle("Patients");
            stage.setScene(new Scene(root1));  
            stage.show();
            Stage stageclose = (Stage) patients.getScene().getWindow();
            stageclose.close();
          }
    catch(Exception e) {
           e.printStackTrace();
          }
    }

    @FXML
    void insert(ActionEvent event) throws SQLException {

        ConnectionClass connectionClass = new ConnectionClass();
        Connection connection = connectionClass.getConnection();

        String sql = "INSERT INTO PATIENTS VALUES('"+iname.getText()+"')";
        Statement statement = connection.createStatement();
        statement.executeUpdate(sql);
            //lname.setText(iname.getText());


    }


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

这是我来自同一个项目(ConnectionClass.java)的连接包:

package connectivity;


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

public class ConnectionClass {
    public Connection connection;
    public Connection getConnection(){

        String dbName = "avicenna2";
        String userName = "root";
        String password = "";

        try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost/"+dbName,userName,password);
        }

        catch (Exception e){
        e.printStackTrace();
        }

        return connection;
    } 

}

标签: javasqljavafxmysql-connector

解决方案


推荐阅读