我正在使用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;
}
}