try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT active FROM users WHERE user_id='123'"); if (rs.next()) { boolean isActive = rs.getBoolean("active"); System.out.println("Is user active? " + isActive); } } catch (SQLException e) { e.printStackTrace(); }
import java.sql.*; import java.util.*; public class Main { public static void main(String[] args) { Connection con = null; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT flag FROM mytable"); while (rs.next()) { boolean flag = rs.getBoolean("flag"); if (flag) { System.out.println("The flag is true."); } else { System.out.println("The flag is false."); } } } catch(Exception e) { e.printStackTrace(); } finally { try { con.close(); } catch(Exception e) { e.printStackTrace(); } } } }In the above example, we connect to a MySQL database using the JDBC driver. We execute a select query to retrieve the value of the 'flag' column from the 'mytable' table. The getBoolean() method is used to retrieve the boolean value of the 'flag' column and store it in a variable named flag. We then check the value of the flag variable using an if-else statement and print the appropriate message to the console. The java.util.ResultSet.getBoolean() method is part of the java.sql package.