Jump to content

Im trying to insert stuff into my database. When doing so i get this "ResultSet not open. Operation 'getInt' not permitted. Verify that autocommit is off." I have tried turning autocommit off but no diffrence. And also couldnt find any help from elsewhere.

public class NewJFrame extends javax.swing.JFrame {

    String Puh = "";
    String Postinumero = "";
    String Laskutusosoite = "";
    String NIMI = "";
    int asiakasrekisteri = 0;

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
        updateTable();
    }
    private void updateTable()
    {
        DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
        dtm.setRowCount(0);
        
        String url = "jdbc:derby://localhost:1527/asiakasrekisteri";
        String user = "admin1";
        String password = "admin1";
        try {
        con = DriverManager.getConnection(url, user, password);
        st = con.createStatement();
        rs = st.executeQuery("SELECT * FROM ASIAKASREKISTERI");
        int row=0;
        
        while (rs.next()) {  
            
            dtm.setRowCount(row+1);
            
            asiakasrekisteri = rs.getInt("asiakkaannumero");
            NIMI = rs.getString("nimi");
            Laskutusosoite = rs.getString("laskutusosoite");
            Postinumero = rs.getString("postinumero");
            Puh = rs.getString("puh");
            
            jTable1.getModel().setValueAt(asiakasrekisteri, row, 0);         
            jTable1.getModel().setValueAt(NIMI, row, 1);           
            jTable1.getModel().setValueAt(Laskutusosoite, row, 2);
            jTable1.getModel().setValueAt(Postinumero, row, 3);
            jTable1.getModel().setValueAt(Puh, row, 4);
            
            
            row++;
        
        }
        
        } catch (SQLException ex) {
        System.out.println(ex.toString());
        } finally {
        try {
        if (rs != null) {
        rs.close();
        }
        if (st != null) {
        st.close();
        }
        if (con != null) {
        con.close();
        }
        
        } catch (SQLException ex) {System.out.println(ex.toString());
        }
        }
    }
       String url = "jdbc:derby://localhost:1527/asiakasrekisteri";
        String user = "admin1";
        String password = "admin1";

        try {
        con = DriverManager.getConnection(url, user, password);
        st = con.createStatement();
        
            asiakasrekisteri = rs.getInt("asiakkaannumero");
            NIMI = rs.getString("nimi");
            Laskutusosoite = rs.getString("laskutusosoite");
            Postinumero = rs.getString("postinumero");
            Puh = rs.getString("puh");
        
        String update = "INSERT INTO ASIAKASREKISTERI (nimi, laskutusosoite, postinumero, puh)" + "VALUES (" + "'" + NIMI + "'" + ", " + "'" + Laskutusosoite + "'" + ", " + "'" + Postinumero + "'" + ", " + "'" + Puh + "'" + ")";
        st.executeUpdate(update);
        
        updateTable();
        
        } catch (SQLException ex) {
        System.out.println(ex.toString());
        } finally {
        try {
        if (rs != null) {
        rs.close();
        }
        if (st != null) {
        st.close();
        }
        if (con != null) {
        con.close();
        }
        
        } catch (SQLException ex) {
            System.out.println(ex.toString());
        }
        }
                                               

    }  

 

Link to comment
https://linustechtips.com/topic/889639-java-help/
Share on other sites

Link to post
Share on other sites

16 minutes ago, Tomek85 said:

Try to declare rs first.

 

ResultSet rs = new ResultSet();

 

I managed to make some progress so this is not my problem anymore. I tried using preparedstatement that is working "good" but now its giving me this "The column position '1' is out of range.  The number of columns for this ResultSet is '0'."

       
		try {
        
        con = DriverManager.getConnection(url, user, password);
        st = con.createStatement();
        rs = st.executeQuery("SELECT * FROM ASIAKASREKISTERI"); 
        if (rs.next()){
            asiakasrekisteri = rs.getInt("ASIAKKAANNUMERO");
            NIMI = rs.getString("nimi");
            Laskutusosoite = rs.getString("laskutusosoite");
            Postinumero = rs.getString("postinumero");
            Puh = rs.getString("puh");
        
        String update = "INSERT INTO ADMIN1.ASIAKASREKISTERI (NIMI, LASKUTUSOSOITE, POSTINUMERO, PUH) " + "VALUES ('" + NIMI + "'" + ", " + "'" + Laskutusosoite + "'" + ", " + "'" + Postinumero + "'" + ", " + "'" + Puh + "'" + ")";
        PreparedStatement prepared = con.prepareStatement(update);
  
        prepared.setString (1, NIMI);
        prepared.setString (2, Laskutusosoite);
        prepared.setString (3, Postinumero);
        prepared.setString (4, Puh);
        prepared.executeUpdate(update);
        }
        updateTable();
        
        } catch (SQLException ex) {
		// close stuff

Didnt change anything else from the code thats not shown here

Link to comment
https://linustechtips.com/topic/889639-java-help/#findComment-10974777
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×