Java "database locked" SQL exception

I am creating a really rich java DB program, but I ran into a connection error. The code:

try{
        Statement st;
        ResultSet rs;
        conn = DBConnection.dbconnection();
        st = conn.createStatement();
        String s = "select *  from tab2DB order by Scadenza";
        rs = st.executeQuery(s);
        while(rs.next()){
            ResultSet rs2;
            PreparedStatement st2;
            conn2 = DBConnection.connection();
            String s2 = "select * from Prodotti where Nome = ?";
            st2 = conn2.prepareStatement(s2);
            st2.setString(1, rs.getString("Prodotto"));
            rs2 = st2.executeQuery();               
            while(rs2.next()){

                Date localDate = new Date();

                String scadenza =  rs.getString("Scadenza");
                DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ITALIAN);
                Date date = format.parse(scadenza);



                if(localDate.after(date)){
                        conn3 = DBConnection.dbconnection();        
                        String query ="insert into tabDB values ('"+nomeord+"','"+prodottoord+"','"+quantitΓ ord+"')";
                        Statement stmt;
                        stmt = conn3.createStatement();
                        stmt.executeUpdate(query);
                        conn3.close();
                        continue;
                }

                //other building of interface in the while, omitted for clarity purpose
                conn2.close();
                rs2.close();
                }
                conn.close();
                rs.close();
    }
    catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

the error is in if (localDate.after (date)) {...} where stmt.executeUpdate (request); causes an error and program crash. I think the problem here is that the DB already has a connection and it cannot handle conn3, but I need these other connections for the while loop, and at the same time I need to be able to give orders inside the if loop. How can I make the new order not interfere with while loops, but at the same time execute the request?

(what is trying to fulfill the request may not work in the code above, this is a fragment of the larger class path)

Thanks for the help!

+4
2

try-with-resources, , .

:

try (Connection con = ConnectionManager.dbconnection();
    Statement statement = con.createStatement();
    ResultSet resultSet = statement.executeQuery(query)) {
        while(resultSet.next) {
            //Add desired values to data structure
        }
}

, , - List, Set Map. .

+2

close() Connection , .

, conn1 DBConnection.dbconnection(); conn2 DBConnection.connection(); conn3 DBConnection.dbconnection();

. ?

0

Source: https://habr.com/ru/post/1669823/


All Articles