Search code examples
javadatabaseh2statements

How to get a String from h2 database (Java, DBUtil)


This is the code from DBUtil

public class DBUtil {
    static Connection conn = null;
    public static Connection getConnected() {
        try {
            Class.forName("org.h2.Driver");
            conn = DriverManager.getConnection("jdbc:h2:tcp://localhost/~/test", "sa", "");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }//end method
}//end DBUtil

and here is the code from the method I'm trying to use:

private class SearchForEdit implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e) {
            String clientNumber = tfieldMiniSearchNumber.getText();
            int number;

            conn = DBUtil.getConnected();
            if(conn == null) return;

            String names=null, address=null;


            String sql ="select names from clients where client_number = ?;";
            String sql2 ="select address from clients where client_number = ?;";

            try {
                state = conn.prepareStatement(sql);
                state.setInt(1, number);
                state.execute();

                state = conn.prepareStatement(sql2);
                state.setInt(1, number);
                state.execute();

                labelWarning.setForeground(new Color(22, 205, 5));
                labelWarning.setText("<html>TEXT SUCCESS</html>");

                //names = select names from clients where client_number = number;
                //address = select address from clients where client_number = number;

                //if(names!=null && address!=null)

                //tfieldEditClientNumber to be number
                tfieldEditClientNumber.setText(""+number);
                //tfieldEditNames to be names, where names = select names from clients where client_number = number;
                //tfieldEditAddressда to be address, where address = select address from clients where client_number = number;
            } catch (SQLException e1) {
                labelWarning.setForeground(Color.RED);
                labelWarning.setText("<html>Try again later</html>");
                e1.printStackTrace();
            }
        }   
    }//end

I want to take the value of column NAME from the database and then put in in the string name. Then the same for address. I know only how to execute statements. Any help is very apreciated!


Solution

  • You need to store the results of the query in a ResultSet, and then iterate over that ResultSet. You can assign the values from the ResultSet into variables as you iterate over them.

    Below code is an example of iterating over a ResultSet, as mentioned in my comment below.

    while (rs.next()) {
    // ResultSet columns: 1 = UserName 2 = FirstName 3 = LastName  4 = Organization
                userInfo[0] = rs.getString(1); // assign UserName result to variable
                userInfo[1] = rs.getString(2); // assign First_Name result to variable
                userInfo[2] = rs.getString(3); // assign Last_Name result to variable
                userInfo[3] = rs.getString(4); // assign Organization result to variable
    }
    

    The while loop will end when there are no ResultSet rows left to iterate over.