Java [SQLITE_ERROR] SQL 错误或缺少数据库(“resi”附近:语法错误)

时间:2020-12-21 10:53:35

标签: java sqlite

我在运行程序时遇到了一些问题。它说“[SQLITE_ERROR] SQL 错误或缺少数据库(接近“resi”:语法错误)”和“ada yang salah:java.sql.SQLException:ResultSet is TYPE_FORWARD_ONLY”。我通过了什么吗?

连接代码

public void koneksiDatabase(){
    try{
        Class.forName("org.sqlite.JDBC");
        con = DriverManager.getConnection("jdbc:sqlite:C:/Users/sqlite3/cekresi.db","root","");
        System.out.println("Koneksi berhasil!");
    }catch(ClassNotFoundException eclass){
        System.out.println("Koneksi Gagal!");
    }catch(SQLException esql){
        System.out.println(esql.getMessage());
    }
}

保存数据代码

public void simpanData(){
    try {
        String sql = "Insert into data resi = \"" + txtResi.getText() + "\","
                + "nama = \"" + txtNama.getText() + "\","
                + "tujuan = \"" + (String)cmbTujuan.getSelectedItem() + "\","
                + "tarif = \"" + txtTarif.getText() + "\","
                + "berat = \"" + txtBerat.getText() + "\","
                + "jumlah = \"" + txtJumlah.getText() + "\"";
        Statement stmt = con.createStatement();
        stmt.executeUpdate(sql);
        System.out.println("berhasil!");
    }catch (Exception e){
        System.out.println(e);
    }
    tampilDataKeTabel();
}

可显示代码

public void tampilDataKeTabel(){
    ResultSet rs = null;
    try{
        Statement stmt = con.createStatement();
        rs = stmt.executeQuery("select * from data");
        
        ResultSetMetaData meta = rs.getMetaData();
        int col = meta.getColumnCount();
        int baris = 0;
        while (rs.next()){
            baris = rs.getRow();
        }
        dataTable = new String[baris][col];
        int x = 0;
        rs.beforeFirst();
        while(rs.next()){
            dataTable[x][0] = rs.getString("resi");
            dataTable[x][1] = rs.getString("nama");
            dataTable[x][2] = rs.getString("tujuan");
            dataTable[x][3] = rs.getString("tarif");
            dataTable[x][4] = rs.getString("berat");
            dataTable[x][5] = rs.getString("jumlah");
            x++;
        }
        tabelDisplay.setModel(new DefaultTableModel(dataTable,header));
    }catch(Exception e){
        System.err.println("ada yang salah:"+e);
    }
}

1 个答案:

答案 0 :(得分:0)

插入语句中存在语法问题。语法应该是:

INSERT INTO table (column1,column2 ,..)
VALUES( value1, value2 ,...);

所以你的插入语句应该是这样的:

String sql = "Insert into data(resi,nama,tujuan,tarif,berat,jumlah)
              values(\"" + txtResi.getText() + "\","
                + \"" + txtNama.getText() + "\","
                + \"" + (String)cmbTujuan.getSelectedItem() + "\","
                + \"" + txtTarif.getText() + "\","
                + \"" + txtBerat.getText() + "\","
                + \"" + txtJumlah.getText() + "\")";

此外,显示数据的代码存在问题。

while (rs.next()){
    baris = rs.getRow();
}

这个循环遍历结果集一次。所以下一个循环会失败,因为 rs 已经到达结果的末尾。

这导致异常:ResultSet is TYPE_FORWARD_ONLY

不是创建一个二维字符串数组,而是创建一个与您的 db 表对应的类,然后创建一个 List。假设将创建一个名为 Data 的类,第二个 while 循环将是:

List<Data> dataList = new ArrayList<>();
while(rs.next()){
    Data data = new Data();
    data.setResi(rs.getString("resi"));
    data.setNama(rs.getString("nama"));
    data.setTujuan(rs.getString("tujuan"));
    data.setTarif(rs.getString("tarif"));
    data.setBerat(rs.getString("berat"));
    data.setJumlah(rs.getString("jumlah"));
    dataList.add(data);
}