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: <hr style=&q...
Table of contents Prerequisites useEffect commitB...
1. Cleaning before installation rpm -qa | grep jd...
I recently started learning about database knowle...
Table of contents Start Docker Stop Docker Python...
In some scenarios, we need to modify our varchar ...
JavaScript writes a random roll call webpage for ...
remember: IDE disk: the first disk is hda, the se...
Let’s install Nginx and try it out. Please note t...
When using the font-family property in CSS to ref...
The database queries which object contains which ...
Export database data: First open cmd and enter th...
question When I was writing a project function to...
Table of contents Merge namespaces with the same ...
disabled definition and usage The disabled attrib...