JDBC operation method for the Boolean field of the MySQL database

  • 2020-04-01 03:40:17
  • OfStack

This article illustrates how JDBC operates Boolean fields in a MySQL database. Share with you for your reference. Specific analysis is as follows:

If you want to use a Boolean field in a Mysql database, you should set it to BIT (1)

This type cannot be edited in Mysql through Edit and Apply Changed under MySQLQueryBrowser

Can only be modified by statement, such as update A set enabled=true where id=1

Set the enabled field of type BIT(1) for the behavior of id 1 in table A to true

In JAVA, the code to manipulate this field using JDBC is as follows:


class testGo { 
 public static void IsReg(String username, String openid) { 
  Connection con = new Dbcon().getCon(); 
  ResultSet rs = null; 
  String sql = null; 
 
  try { 
    
   sql = "select * from A where id=1"; 
   rs = con.prepareStatement(sql).executeQuery(); 
   while (rs.next()) { 
    System.out.println(rs.getBoolean("enabled")); 
   } 
    
   sql="update A set enabled=true where id=1"; 
   con.createStatement().execute(sql); 
    
   sql = "select * from A where id=1"; 
   rs = con.prepareStatement(sql).executeQuery(); 
   while (rs.next()) { 
    System.out.println(rs.getBoolean("enabled")); 
   } 
    
   con.close(); 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 
 } 
}

Output the value of this field, change it to true, and then output the value of this field

I hope this article has been helpful to your Java programming.


Related articles: