遇到“数据库被锁定”错误

时间:2017-11-16 03:08:02

标签: java netbeans netbeans-8

我在2天前一直在搜索修复此错误弹出我试图在jComboBox上显示数据库值(变量名为“nama_supplier”)。在jComboBox上出现值之后,我想捕获数据库中选择“nama_supplier”数据的其他列,代码为:

private void pilih_supplier(){
    java.sql.Connection koneksi = new DBKoneksi().konek();

    try {
                String sqlSupplier = "SELECT * FROM tabel_supplier ORDER BY nama_supplier asc";
                PreparedStatement stmt = koneksi.prepareStatement(sqlSupplier);
                ResultSet rqs=stmt.executeQuery();
                while (rqs.next()) {
                    String pat = rqs.getString("nama_supplier");
                    nama_supplier.addItem(pat);
                    }
                    String select_supplier = (String) nama_supplier.getSelectedItem();

                   String sqlSelectSupplier = "SELECT * FROM tabel_supplier where nama_supplier = '"+select_supplier+"'";
                   PreparedStatement stmt1 = koneksi.prepareStatement(sqlSupplier);
                   ResultSet res=stmt1.executeQuery();
                   while (res.next()) {
                    String add1 = res.getString("kode_barang");
                    kode_barang.setText(add1);
                    String add2 = res.getString("nama_barang");
                    nama_barang.setText(add2);
                    String add3 = res.getString("harga");
                    harga.setText(add3);
                    }
                //rqs.close();
                //res.close();       
                //stmt1.close();
                //koneksi.close();
        }   

        catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }

}

rqs.close();
res.close();        
stmt1.close();
koneksi.close();   //these 4 codes should remove the error, but it doesn't work   

此外,我尝试在try {}之后在finally {}方法中使用了这4个代码但是它没有用到

任何建议都会被接受

1 个答案:

答案 0 :(得分:1)

找到这个解决方案,仍然使用2个连接,但是我将它们分成2个类,这是修复代码。该男子http://rahmadfaisal.blogspot.co.id/2015/03/tutorial-menampilkan-isi-database.html

的所有学分
public void tampil()
{
    try {
    java.sql.Connection koneksi = new DBKoneksi().konek();
    Statement stt = koneksi.createStatement();
    String sql = "select nama_supplier, kode_barang, nama_barang, harga from tabel_supplier where nama_supplier='"+nama_select_supplier.getSelectedItem()+"'";  
    ResultSet res = stt.executeQuery(sql);

    while(res.next()){
        Object[] ob = new Object[3];
        ob[0]=  res.getString(2);
        ob[1]= res.getString(3);
        ob[2]= res.getString(4);

        kode_barang.setText((String) ob[0]);
        nama_barang.setText((String) ob[1]);
        harga.setText((String) ob[2]);
    }
    res.close(); stt.close();

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }              
}

public void tampil_combo()
{
    try {
    java.sql.Connection koneksi = new DBKoneksi().konek();
    Statement stt = koneksi.createStatement();
    String sql = "select nama_supplier from tabel_supplier order by nama_supplier asc";      
    ResultSet res = stt.executeQuery(sql);                                

    while(res.next()){
        Object[] ob = new Object[3];
        ob[0] = res.getString(1);

        nama_select_supplier.addItem((String) ob[0]);                                      
    }
    res.close(); stt.close();

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}