尝试使用java将数据插入SQLite数据库时,我一直收到错误

时间:2017-03-30 09:46:33

标签: java sql database sqlite javafx

我一直收到错误“设置数据时出错!”,请问您是否有任何明显的问题。我通过sqlite运行数据库。请帮忙看看问题所在,谢谢。

sql数据库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!");
            }

    }

1 个答案:

答案 0 :(得分:1)

您必须从1而不是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());

对于第二个问题:

error :java.lang.ClassNotFoundException: 

这已经在这里回答了 java.lang.ClassNotFoundException: org.sqlite.JDBC error in Sample.java program from xerial