How to store false or true in MySQL

How to store false or true in MySQL

MySQL Boolean value, stores false or true

In short, the Boolean values ​​stored in the database are 0 and 1, which is equivalent to a one-byte INT integer.

Specific operations

If it is a visual interface, you can directly select Boolean and set the length to 1. The final display effect is equivalent to TINYINT(1)

So, zero == false; nonzero == true

Storing Boolean values ​​in MySQL database

In Java programming, we often encounter situations where boolean values ​​are written to MySQL databases. However, the MySQL database does not have a Boolean type. Its Boolean values ​​are represented by the numbers 0 and 1.

Next, we use Java to demonstrate how to save Boolean values ​​to MySQL database

package database;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.qs.database.DBConnection;
public class JavaMysqlBoolean {
 
 public static void main(String[] args) {
  if(DBConnection.conn==null){
   DBConnection.openConn();
  }
  PreparedStatement ps = null;
  try {
   String sql = "insert into testboolean(name,password,isAdult) values(?,?,?)";
   ps = DBConnection.conn.prepareStatement(sql);
   
   ps.setString(1, "lisi");
   ps.setString(2, "1");
   ps.setBoolean(3, false);
   
   ps.executeUpdate();
   
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
}
package com.qs.database;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DBConnection {
 public static Connection conn = null;
 public static String driverClass = "";
 public static String dbURL = "";
 public static String dbUser = "";
 public static String dbPwd = "";
 static {
  loadProperty();
 }
 // Read the configuration file public static boolean loadProperty() {
  Properties properties = new Properties();
  try {
   properties.load(DBConnection.class.getResourceAsStream("db.properties"));
   driverClass = properties.getProperty("drivername");
   dbURL = properties.getProperty("dburl");
   dbUser = properties.getProperty("username");
   dbPwd = properties.getProperty("password");
  } catch (IOException e) {
   System.out.println("Failed to read configuration file");
   e.printStackTrace();
   return false;
  }
  return true;
 }
 public static void openConn() {
  // Load the driver class try {
   Class.forName(driverClass).newInstance();
  } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
   System.out.println("Driver class not found, loading failed");
   return;
  }
  // Get a connection to the specified database try {
   conn = DriverManager.getConnection(dbURL, dbUser, dbPwd);
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
 public void closeConn() {
  if (conn != null) {
   try {
    conn.close();
    conn = null;
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }
}

Properties file db.properties

drivername=org.gjt.mm.mysql.Driver
dburl=jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=utf-8
username=root
password=root 

Write the picture description here

Write the picture description here

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • How to operate Boolean fields in MySQL database with JDBC
  • Mybatis connects to MySQL database Tinyint is a boolean type detailed explanation
  • Mysql NULL caused the pit

<<:  Summary of the application of decorative elements in web design

>>:  Detailed explanation of Axios asynchronous communication in Vue

Recommend

hr horizontal line style example code

Copy code The code is as follows: <hr style=&q...

The difference between useEffect and useLayoutEffect in React

Table of contents Prerequisites useEffect commitB...

Detailed tutorial on installing JDK1.8 on Linux

1. Cleaning before installation rpm -qa | grep jd...

Use Shell scripts to batch start and stop Docker services

Table of contents Start Docker Stop Docker Python...

When modifying a record in MySQL, the update operation field = field + string

In some scenarios, we need to modify our varchar ...

JavaScript to implement random roll call web page

JavaScript writes a random roll call webpage for ...

Analysis of the Principle and Method of Implementing Linux Disk Partition

remember: IDE disk: the first disk is hda, the se...

Docker installation Nginx tutorial implementation illustration

Let’s install Nginx and try it out. Please note t...

Summary of English names of Chinese fonts

When using the font-family property in CSS to ref...

Database query which object contains which field method statement

The database queries which object contains which ...

Summary of MySql import and export methods using mysqldump

Export database data: First open cmd and enter th...

TypeScript namespace merging explained

Table of contents Merge namespaces with the same ...

Specific usage of textarea's disabled and readonly attributes

disabled definition and usage The disabled attrib...