Java JDBC, don't know anymore

I can generate an SQL query from user input.

Problem is, how do I take that string, throw it at the database and get the response?

Examples online explain fuck all and I am completly lost. HALP!

private static void dbComm(DBConnection con, String command) throws Exception{
        Statement state = con.connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        //Statement stmt = con.createStatement();

        try{
            //??? ↓
            state.executeQuery(command);
            ResultSet response = state.getResultSet();
            response.next();
            //??? ↑
            System.out.println("Success");
            System.out.println(response);
        } catch(Exception e){
            System.out.println("Error");
            e.printStackTrace();
        }
    }

Read this guide https://www.baeldung.com/java-jdbc

1 Like

That helped a bit, thanks.

1 Like

I also got a “query-response handler” of sorts down

    private static void dbCommResp(DBConnection con, String command, String op) throws Exception{
        Statement state = con.connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        try {
            state.executeQuery(command);
            ResultSet response = state.getResultSet();
            switch (op) {
                case "list":
                    while(response.next()){
                        System.out.print(" | CustomerNr: " + response.getString("CustomerNr"));
                        System.out.print(" | Name: " + response.getString("Name"));
                        System.out.print(" | Registration: " + response.getString("Registration") + System.lineSeparator());
                    }
                    break;
                case "edit":
                    while(response.next()){

                    }
                    break;
                default:
                    System.err.println("dbCommResp: unkown operation");
                    break;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }