0

I am working with javafx and mysql-connector-java-5.1.45-bin library. The problem is i cannot insert new data into my column. There are not red underlines which are declaring errors. It is executed but is not inserting any data to my column with the name of patients. But in patients there are 8 columns. By the way in tutorial is told that it will insert the single data to the all columns. What is a problem?

Here is my code (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
    }    

}

Here is my connectivity package from the same project(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;
    } 

}
Ganz Alex
  • 47
  • 1
  • 8
  • Doesn't this just throw a null pointer exception? `getConnection()` returns null... – James_D May 06 '18 at 16:38
  • Yes.. i have changed as you told. But still dont get expected result – Ganz Alex May 06 '18 at 16:53
  • So what are you getting? If you have an exception, you should include the complete stack trace in the question, and indicate which line is throwing it. – James_D May 06 '18 at 16:54
  • 1
    "in tutorial is told that it will insert the single data to the all columns": this is inconsistent with the actual [documentation](https://dev.mysql.com/doc/refman/8.0/en/insert.html) – James_D May 06 '18 at 17:01
  • I am getting nothing. What do u mean by the complete stack trace? How about documentation idk.. In the video was showed only with one column, but i have 8 of them. How to indicate the insertion to insert only into desired columns? – Ganz Alex May 06 '18 at 17:53
  • 1
    What video? The documentation is the definitive source. You can specify the name of the column(s) as shown in the documentation. But, again, you haven't even said if you are getting errors here. If you are, post the complete [stack trace](https://stackoverflow.com/questions/3988788/). – James_D May 06 '18 at 18:01
  • 1
    Furthermore I suggest you use `PreparedStatement` to put the string into the query. This avoids problems with quotes in the `TextField`... – fabian May 06 '18 at 18:42

0 Answers0