现在的位置: 首页 > 综合 > 正文

【Mysql】JDBC对数据库布尔字段的操作

2017年11月27日 ⁄ 综合 ⁄ 共 746字 ⁄ 字号 评论关闭

在Mysql数据库如果要使用布尔字段,而应该设置为BIT(1)类型

此类型在Mysql中不能通过MySQLQueryBrowser下方的Edit与Apply Changed去编辑

只能通过语句修改,比如update A set enabled=true where id=1

把A表的id为1的这一行为BIT(1)类型的enabled字段设置为真

在JAVA中,使用JDBC操作这个字段的代码如下:

class testGo {
	public static void IsReg(String username, String openid) {
		Connection con = new Dbcon().getCon();
		ResultSet rs = null;
		String sql = null;

		try {
			
			sql = "select * from A where id=1";
			rs = con.prepareStatement(sql).executeQuery();
			while (rs.next()) {
				System.out.println(rs.getBoolean("enabled"));
			}
			
			sql="update A set enabled=true where id=1";
			con.createStatement().execute(sql);
			
			sql = "select * from A where id=1";
			rs = con.prepareStatement(sql).executeQuery();
			while (rs.next()) {
				System.out.println(rs.getBoolean("enabled"));
			}
			
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

先输出这个字段值,再把其改为true,再输出这个字段值

抱歉!评论已关闭.