mysql布尔值,存储false或true
简明说,数据库存储的布尔值是0和1,相当于一个字节的int型整数
具体操作
如果是可视化界面,可以直接选择boolean,长度设置为1。其最终显示的效果等价于tinyint(1)
所以,zero == false;nonzero == true
mysql数据库存储布尔值
在java编程中,我们经常会遇到将boolean值写入到mysql数据库的情况。但是,mysql数据库是没有boolean类型的,它的boolean值是用数字0和1来表示的。
下面,我们用java演示一下怎样将boolean值保存到mysql数据库
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(); } // 读取配置文件 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("读取配置文件失败"); e.printstacktrace(); return false; } return true; } public static void openconn() { // 加载驱动类 try { class.forname(driverclass).newinstance(); } catch (instantiationexception | illegalaccessexception | classnotfoundexception e) { system.out.println("驱动类没有找到,加载失败"); return; } // 获得与指定数据库的连接 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(); } } } }
属性文件db.properties
drivername=org.gjt.mm.mysql.driver dburl=jdbc:mysql://localhost:3306/mydatabase?useunicode=true&characterencoding=utf-8 username=root password=root
以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。