-2

I keep getting the error "error in setting data!", please could you see if there is any obvious problem. I am running the database through sqlite. Please could you help to see what the problem is, thanks.

The sql database strusture:

package parts_system;

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
/**
 *
 * @author bened_000
 */
public class FXMLDocumentController implements Initializable {


    @FXML
    private Label label;
    @FXML
    private TextArea AddPartCostStock;
    @FXML
    private TextArea AddPartDescriptionStock;
    @FXML
    private TableView<?> StockTable;
    @FXML
    private Button AddToStock;
    @FXML
    private Button RemoveFromStock;
    @FXML
    private Button EditVehicle;
    @FXML
    private TextArea VPAddVehicleReg;
    @FXML
    private Button AddToVehicle;
    @FXML
    private TextArea AddPartAmountStock;
    @FXML
    private TextArea SearchStock;
    @FXML
    private TextArea VPAddCustomerName;
    @FXML
    private TableView<?> VehiclePartTable;
    @FXML
    private TextArea VPAddPartName;
    @FXML
    private TextArea VPAddPartID;
    @FXML
    private TextArea SearchVehicle;
    @FXML
    private ChoiceBox<?> SearchOptionsVehicle;
    @FXML
    private TextArea VPAddDateInstall;
    @FXML
    private Button Exit;

   private ObservableList<stock> stocklist=FXCollections.observableArrayList();
   private  ObservableList<part> list2=FXCollections.observableArrayList();    
   private final ObservableList comboOption =FXCollections.observableArrayList();

   @FXML
    private TextArea AddPartNameStock;
    @FXML
    private TableColumn<?, ?> sName;
    @FXML
    private TableColumn<?, ?> sAmount;
    @FXML
    private TableColumn<?, ?> sCost;
    @FXML
    private TableColumn<?, ?> sDescription;
    @FXML
    private TableColumn<?, ?> vPartID;
    @FXML
    private TableColumn<?, ?> vName;
    @FXML
    private TableColumn<?, ?> vCost;
    @FXML
    private TableColumn<?, ?> vDescription;
    @FXML
    private TableColumn<?, ?> vVehicleReg;
    @FXML
    private TableColumn<?, ?> vCustomerName;
    @FXML
    private TableColumn<?, ?> vInstallationDate;
    @FXML
    private TableColumn<?, ?> vWarrantyExpires;
    @FXML
    private TableColumn<?, ?> vRepairID;
    @FXML
    private TextArea VPAddRepairID;


    @FXML void addStock(ActionEvent event) throws IOException{
        try{
               Class.forName("org.sqlite.JDBC");
               Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\bened_000\\Documents\\NetBeansProjects\\garage\\SE35-master\\scratch\\Benedict\\parts_System\\src\\parts_system\\new.sqlite");

 String sql= "INSERT INTO stock(name,numberofparts,cost,description)VALUES(?,?,?,?)";

        PreparedStatement stmt1 =conn.prepareStatement(sql);

       stmt1.setString(2, AddPartNameStock.getText());
       stmt1.setDouble(3, Double.parseDouble(AddPartAmountStock.getText()));
       stmt1.setDouble(4, Double.parseDouble(AddPartCostStock.getText()));
       stmt1.setString(5, AddPartDescriptionStock.getText());

        stmt1.execute(); 
        stmt1.close();
        conn.close();

    } catch (Exception e) {
                System.out.println("error in setting data!");
            }

    }
mkl
  • 90,588
  • 15
  • 125
  • 265
Brio
  • 11
  • 1
  • Welcome to Stack Overflow! Please take the [tour](http://stackoverflow.com/tour), have a look around, and read through the [help center](http://stackoverflow.com/help), in particular [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) and [What topics can I ask about here?](http://stackoverflow.com/help/on-topic). - please [add](http://stackoverflow.com/help/editing) the *complete* error message. – Timothy Truckle Mar 30 '17 at 09:50
  • 1
    Then maybe you shouldn't just throw away the real error message, i.e. `e`. Perhaps if you **print the error** using **`e.printStackTrace(System.out)`**, you'd know what you did wrong, e.g. fixing the extremely bad SQL syntax. What is `num][1]berofparts` supposed to be? And **why set parameters 2-5**, when you only have four `?` markers, meaning that parameters are numbered 1-4? – Andreas Mar 30 '17 at 09:51
  • apologies the num][1] was a copying error , I have fixed it – Brio Mar 30 '17 at 09:52
  • Thank you @Andreas , apologies , I had a problem inserting the code as I am new to stackoverflow. I added the e.printStackTrace(System.out) you suggested and I am currently getting the error :java.lang.ClassNotFoundException: org.sqlite.JDBC , Thanks for your help. – Brio Mar 30 '17 at 10:05

1 Answers1

1

You have to start setting your fields from 1 and not 2:

stmt1.setString(1, AddPartNameStock.getText());
stmt1.setDouble(2, Double.parseDouble(AddPartAmountStock.getText()));
stmt1.setDouble(3, Double.parseDouble(AddPartCostStock.getText()));
stmt1.setString(4, AddPartDescriptionStock.getText());

For the second problem :

error :java.lang.ClassNotFoundException: 

This already answerd here java.lang.ClassNotFoundException: org.sqlite.JDBC error in Sample.java program from xerial

Community
  • 1
  • 1
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • Thanks , however I initially tried that but I would still the error, I will do the suggested changes and use e.printStackTrace(System.out) – Brio Mar 30 '17 at 09:55
  • After fixing the start setting number, I currently get the error :java.lang.ClassNotFoundException: org.sqlite.JDBC – Brio Mar 30 '17 at 10:02
  • in this case make sure that you put your sqlite-jdbc jar in your classpath @BenedictRodrigues – Youcef LAIDANI Mar 30 '17 at 10:06
  • @YCFL , Thanks , However I do currrently have a jdbc jar file within the classpath .as seen here -> https://gyazo.com/39ce27be4434b700491aba6b68c7f385?token=021ca45ad444b229f0637b026e7ec416 – Brio Mar 30 '17 at 10:16
  • learn this @BenedictRodrigues http://stackoverflow.com/questions/18089952/java-lang-classnotfoundexception-org-sqlite-jdbc-error-in-sample-java-program-f – Youcef LAIDANI Mar 30 '17 at 10:19
  • Thanks @YCF_L , I believe there has been progress, I now get this error org.sqlite.SQLiteException: [SQLITE_CONSTRAINT_PRIMARYKEY] A PRIMARY KEY constraint failed (UNIQUE constraint failed: stock.name) I set the primary key as a varchar(string) name , is this a problem , or should it work fine? – Brio Mar 30 '17 at 10:28
  • Actually ignore that comment , IT WORKED , Thanks so much everyone :) – Brio Mar 30 '17 at 10:41