java - 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;
}
}
解决方案
推荐阅读
- ios - 在 api 类之外显示 alamofire 相关警报
- javascript - 访问javascript对象的奇怪结果
- visual-studio - 在 Visual Code 上插入带有宏的新行
- php - Include和Require的位置是否有编码标准?
- filter - 量角器和减少 .all 元素列表
- python - Python:从多个 csv.gz 创建熊猫数据框,仅提取 von CSV 指定的列
- powershell - 如何将此用于 Active Directory 的 power shell 命令转换为批处理文件?
- dockerfile - 可以在 Dockerfile 中使用 pushd/popd 吗?
- r - 从列表项对中查找向量的组合
- codeception - Codeception - 处理使用子域前缀的测试环境