获取错误:java.sql.SQLException:在开始结果集之前

时间:2016-02-05 07:01:35

标签: java jdbc

我有一个函数add_data到数据库mysql,但我得到错误:java.sql.SQLException:在开始结果集之前。这是我的代码:

public void tambah() {
        Connection con = Koneksi.getkoneksi();
        int kodeBarcode = Integer.valueOf(TxtBarang.getText());
        String kodeTransaksi = kodetransaksi.getText();
        int quantity = Integer.valueOf(TxtJbeli.getText());
        String tanggal_transaksi = tanggal.getText();
        Integer harga_barang = Integer.valueOf(TxtHargaBarang.getText());
        Integer total_harga = harga_barang * quantity;
        try {
            Statement st = con.createStatement();
            String sql = "select idtransaksi from tbtransaksi where idtransaksi='" + kodeTransaksi + "'";
            ResultSet rs = st.executeQuery(sql);

            // get idbarang
            Statement st_barang = con.createStatement();
            ResultSet rs_barang = st_barang.executeQuery("select idbarang from tbstok where idstok='26'");
            Integer idbarang = rs_barang.getInt("idbarang");
            // cek apa sudah ada di tabel transaksi
            if (rs.next()) { // jika sudah ada
                Statement st2 = con.createStatement();
                // cek apakah ada diskon
                ResultSet rs2 = st2.executeQuery("select idstok, idbarang, warna, ukuran, stok1, harga, harga_d from tbstok where idstok='" + kodeBarcode + "'");
                if (rs2.next() && rs2.getInt("harga_d") == 0) { // jika tidak ada diskon
                    Statement st4 = con.createStatement();
                    st4.executeUpdate("INSERT INTO `tbdetail`(`iddetail`, `idtransaksi`, `tgl_transaksi`, `idbarang`, `idstok`, `jumlah`, `total`) VALUES ( '', '" + kodeTransaksi + "', '" + tanggal_transaksi + "', '" + idbarang + "', '" + kodeBarcode + "', '" + quantity + "', '" + total_harga + "')");
                } else { // jika ada diskon
                    Statement st4 = con.createStatement();
                    st4.executeUpdate("INSERT INTO `tbdetail`(`iddetail`, `idtransaksi`, `tgl_transaksi`, `idbarang`, `idstok`, `jumlah`, `total`) VALUES ( '', '" + kodeTransaksi + "', '" + tanggal_transaksi + "', '" + idbarang + "', '" + kodeBarcode + "', '" + quantity + "', '" + total_harga + "')");
                }
            } else { // jika belum ada
                Statement st2 = con.createStatement();
                // cek apakah ada diskon          
                ResultSet rs2 = st2.executeQuery("select idstok, idbarang, warna, ukuran, stok1, harga, harga_d from tbstok where idstok='26'");
                if (rs2.next() && rs2.getInt("harga_d") == 0) { // jika tidak ada diskon
                    // insert ke tabel transaksi dan tabel detail
                    Statement st3 = con.createStatement();
                    st3.executeUpdate("INSERT INTO `tbtransaksi`(`idtransaksi`, `iduser`, `totalbayar`, `status`, `dibaca`, `tgl_transaksi`) values ('" + kodeTransaksi + "','0','0','proses','N','" + tanggal_transaksi + "')");
                    Statement st4 = con.createStatement();
                    st4.executeUpdate("INSERT INTO `tbdetail`(`iddetail`, `idtransaksi`, `tgl_transaksi`, `idbarang`, `idstok`, `jumlah`, `total`) VALUES ( '', '" + kodeTransaksi + "', '" + tanggal_transaksi + "', '" + idbarang + "', '" + kodeBarcode + "', '" + quantity + "', '" + total_harga + "')");
                }
                if (rs2.next() && rs2.getInt("harga_d") != 0) { // jika ada diskon
                    // insert ke tabel transaksi dan tabel detail                    
                    Statement st3 = con.createStatement();
                    st3.executeUpdate("INSERT INTO `tbtransaksi`(`idtransaksi`, `iduser`, `totalbayar`, `status`, `dibaca`, `tgl_transaksi`) values ('" + kodeTransaksi + "','0','0','proses','N','" + tanggal_transaksi + "')");
                    Statement st4 = con.createStatement();
                    st4.executeUpdate("INSERT INTO `tbdetail`(`iddetail`, `idtransaksi`, `tgl_transaksi`, `idbarang`, `idstok`, `jumlah`, `total`) VALUES ( '', '" + kodeTransaksi + "', '" + tanggal_transaksi + "', '" + idbarang + "', '" + kodeBarcode + "', '" + quantity + "', '" + total_harga + "')");
                }
            }


        } catch (SQLException ex) {
            System.out.println(ex);
        }

    }

我是使用java的新手。所以需要你的帮助。非常感谢你

function get connection:

 public static Connection getkoneksi() {
        if (koneksi==null) {
            try {
                String url=new String();
                String user=new String();
                String password=new String();
                url="jdbc:mysql://localhost:3306/shafa";
                user="root";
                password="";
                DriverManager.registerDriver(new com.mysql.jdbc.Driver());
                koneksi=DriverManager.getConnection(url,user,password);
                System.out.println("koneksi sukses");
            }catch (SQLException t) {
                System.out.println("Error membuat koneksi");
            }
        }
     return koneksi;
    }

1 个答案:

答案 0 :(得分:1)

您必须在rs_barang.next()之前调用Integer idbarang = rs_barang.getInt("idbarang");将corsor设置为结果集的第一行。

      ResultSet rs_barang = st_barang.executeQuery("select idbarang from tbstok where idstok='26'");
       rs_barang.next();
       Integer idbarang = rs_barang.getInt("idbarang");

了解准备好的陈述。