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

How to write object and param to play flash in firefox

Copy code The code is as follows: <object clas...

MySQL 5.6 zip package installation tutorial detailed

Previously, we all used files with the suffix .ms...

A brief discussion on the implementation of fuzzy query using wildcards in MySQL

In the MySQL database, when we need fuzzy query, ...

Vue uses vue-quill-editor rich text editor and uploads pictures to the server

Table of contents 1. Preparation 2. Define the gl...

MySQL 8.0.14 installation and configuration method graphic tutorial

This article records the installation and configu...

MySQL 5.7 mysql command line client usage command details

MySQL 5.7 MySQL command line client using command...

Getting started with JavaScript basics

Table of contents 1. Where to write JavaScript 2....

border-radius is a method for adding rounded borders to elements

border-radius:10px; /* All corners are rounded wi...

Detailed explanation of tinyMCE usage and experience

Detailed explanation of tinyMCE usage initializat...

The whole process of Vue page first load optimization

Table of contents Preface 1. Image Optimization 2...

Summary of how to use the MySQL authorization command grant

How to use the MySQL authorization command grant:...

Gearman + MySQL to achieve persistence operation example

This article uses the gearman+mysql method to imp...