将程序连接到SQLite数据库

时间:2019-06-13 21:41:46

标签: java sqlite

伙计们,我正在为一个朋友创建一个清单搜索程序,并且因为我不熟悉SQLite而遇到问题,这是我第一次使用数据库进行创建。我没有任何人可以真正展示我的代码,我只是在为您介绍了教程之后将它们放在一起,而实际上没有。如果你们可以检查一下并指出我可能犯的任何明显错误,那将是很棒的。我已经包含了从教程中获得的代码,以及我尝试使用“ addVehicles”将数据输入数据库的第一种方法。

    private static Connection con;
    private static boolean hasData = false;

    public ResultSet displayVehicles() throws ClassNotFoundException, SQLException {
        if(con == null) {
            getConnection();
        }

        Statement state = con.createStatement();
        ResultSet res = state.executeQuery("SELECT sim, wo, description, oilN, oilB, "
                + "airN, airB, hydN, hydB, fuelN, fuelB, misc FROM vehicles");
        return res;
    }

    private void getConnection() throws ClassNotFoundException, SQLException {
        // TODO Auto-generated method stub
        Class.forName("org.sqlite.JDBC");
        con = DriverManager.getConnection("jdbc:sqlite:SQLiteVehicles.db");
        initialise();


    }

    private void initialise() throws SQLException {
        // TODO Auto-generated method stub
        if(!hasData) {
            hasData = true;

            Statement state = con.createStatement();
            ResultSet res = state.executeQuery("SELECT name from sqlite_master WHERE"
                    + " type=' table' AND name=' vehicles'");
            if(!res.next()) {
                System.out.println("Building the Vehicle table with prepopulated values.");
                //build table
                Statement state2 = con.createStatement();
                state2.execute("CREATE TABLE vehicles(sim varchar(100)," + "wo varchar(100)," + "description varchar(500)," +
                        "oilN varchar(100)," + "oilB varchar(100)," + "airN varchar(100)," + "airB varchar(100)," +
                        "hydN varchar(100)," + "hydB varchar(100)," + "fuelN varchar(100)," + "fuelB varchar(100),"
                        + "misc varchar(1000),");

                //insert sample data
                PreparedStatement prep = con.prepareStatement("INSERT INTO vehicles "
                        + "values(?,?,?,?,?,?,?,?,?,?,?,?);");
                prep.setString(1, "101");
                prep.setString(2, "00000");
                prep.setString(3, "VEHICLE NOT FOUND");
                prep.setString(4, "N/A");
                prep.setString(5, "N/A");
                prep.setString(6, "N/A");
                prep.setString(7, "N/A"); 
                prep.setString(8, "N/A");
                prep.setString(9, "N/A");
                prep.setString(10, "N/A");
                prep.setString(11, "N/A");
                prep.setString(12, "TEST WORKED");
                prep.execute();


            }

        }
    }

    public void addVehicle(String sim, String wo, String description,
            String oilN, String oilB, String airN, String airB, String hydN,
            String hydB, String fuelN, String fuelB, String misc) 
                    throws ClassNotFoundException, SQLException {
        if(con == null) {
            getConnection();
        }
        PreparedStatement prep = con.prepareStatement("INSERT INTO vehicles "
                + "values(?,?,?,?,?,?,?,?,?,?,?,?);");
        prep.setString(1, sim);
        prep.setString(2, wo);
        prep.setString(3, description);
        prep.setString(4, oilN);
        prep.setString(5, oilB);
        prep.setString(6, airN);
        prep.setString(7, airB); 
        prep.setString(8, hydN);
        prep.setString(9, hydB);
        prep.setString(10, fuelN);
        prep.setString(11, fuelB);
        prep.setString(12, misc);
        prep.execute();
    }

我收到大量错误消息,例如

Exception in thread "``JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)

刚出现一些新错误

  

java.sql.SQLException:[SQLITE_ERROR]在org.sqlite.core处的org.sqlite.core.DB.newSQLException(DB.java:890)发生SQL错误或缺少数据库(在“,”:语法错误附近)。 org.sqlite.core.DB.throwex(DB.java:868)的org.sqlite.core.NativeDB.prepare(Native Method)的org.sqlite.core.DB的DB.newSQLException(DB.java:901)。在application.SQLite.getConnection(SQLite.java:30)的org.sqlite.jdbc3.JDBC3Statement.execute(JDBC3Statement.java:60)的prepare(DB.java:211)在应用程序的SQLite.initialise(SQLite.java:47) )在application.SQLite.displayVehicles(SQLite.java:17)

1 个答案:

答案 0 :(得分:0)

带您声明并删除所有“代码”元素,然后看一下。甚至进入数据库控制台(或其他数据库应用程序)并尝试执行该语句。

  

创建表格车辆(sim varchar(100),wo varchar(100),说明   varchar(500),oilN varchar(100),oilB varchar(100),airN   varchar(100),airB varchar(100),hydN varchar(100),hydB   varchar(100),fuelN varchar(100),fuelB varchar(100),misc varchar(1000),

立即,我被最后一条语句吸引住了,该语句的末尾有,,但还有其他错误。如果我们删除所有的“其他绒毛”并仔细观察...

  

创建表格车辆(...,misc varchar(1000),

您会发现您缺少括号),该括号应位于语句的末尾