MySQL Boolean value, stores false or trueIn short, the Boolean values stored in the database are 0 and 1, which is equivalent to a one-byte INT integer. Specific operationsIf 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 databaseIn 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 databasepackage 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 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:
|
<<: Summary of the application of decorative elements in web design
>>: Detailed explanation of Axios asynchronous communication in Vue
Copy code The code is as follows: <object clas...
Previously, we all used files with the suffix .ms...
In the MySQL database, when we need fuzzy query, ...
Table of contents 1. Preparation 2. Define the gl...
This article records the installation and configu...
MySQL 5.7 MySQL command line client using command...
Table of contents 1. Where to write JavaScript 2....
border-radius:10px; /* All corners are rounded wi...
We all know that the performance of applications ...
Basic network configuration Although Docker can &...
Detailed explanation of tinyMCE usage initializat...
Table of contents 1. Steps to use Jquery: (1) Imp...
Table of contents Preface 1. Image Optimization 2...
How to use the MySQL authorization command grant:...
This article uses the gearman+mysql method to imp...