I passed a value to be recorded but the db recorded an empty value

I have tried to refresh the table after I inserted the value, but the passed value isn't there; just the blank record is showing.

public void insertQuery(String in) throws SQLException, ClassNotFoundException{

        try{
            Class.forName("com.mysql.cj.jdbc.Driver");

             String USERNAME = "root";
             String PASSWORD = "root";
             String CONN_STRING = "jdbc:mysql://localhost:3306/javaddy?serverTimezone=UTC";
             Connection connection = DriverManager.getConnection(CONN_STRING,USERNAME,PASSWORD);

            try{
                int incrementID = increment();
                String pass = in;
                System.out.println(pass);
                PreparedStatement pstmt = connection.prepareStatement("INSERT INTO inquiries (LearningMat_ID,Inquiry_ID,Inquiry_question,Inquiry_answer)VALUES (?,?,?,?)");
                pstmt.setInt(1,0);
                pstmt.setInt(2, incrementID);
                pstmt.setString(3, pass);
                pstmt.setString(4, null);
                pstmt.execute();
                //
                bot("Your query has been forwarded to the database. An admin will answer it later.");
                connection.close();
            }catch(SQLException e){
                System.out.println(e);
            }
        }catch(ClassNotFoundException | SQLException e){
            System.out.println(e);
        }
    }
     public int increment() throws SQLException
     {
            int id = 1;
            Statement stmt = null;
            ResultSet rs = null;

            try{
             Class.forName("com.mysql.cj.jdbc.Driver");

             String USERNAME = "root";
             String PASSWORD = "root";
             String CONN_STRING = "jdbc:mysql://localhost:3306/javaddy?serverTimezone=UTC";
             Connection connection = DriverManager.getConnection(CONN_STRING,USERNAME,PASSWORD);

                try{
                    String query = "SELECT MAX(Inquiry_id) from inquiries";
                    stmt = connection.createStatement();
                    rs = stmt.executeQuery(query);
                    while(rs.next())
                    {
                        id = rs.getInt(1) +1;
                    }
                }catch(SQLException e){
                    System.out.println(e);
                }finally{
                    connection.close();
                }
            }catch(ClassNotFoundException | SQLException e){
                System.out.println(e);
            }
            return id;
     }

I wanted to insert a value in the db to be used later. There is no record showing. The application just records empty values

0 Answers