mysql增加布尔字段_JDBC对MySQL数据库布尔字段的操作方法
本文實例講述了JDBC對MySQL數(shù)據(jù)庫布爾字段的操作方法。分享給大家供大家參考。具體分析如下:
在Mysql數(shù)據(jù)庫如果要使用布爾字段,而應(yīng)該設(shè)置為BIT(1)類型
此類型在Mysql中不能通過MySQLQueryBrowser下方的Edit與Apply Changed去編輯
只能通過語句修改,比如update A set enabled=true where id=1
把A表的id為1的這一行為BIT(1)類型的enabled字段設(shè)置為真
在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,再輸出這個字段值
希望本文所述對大家的Java程序設(shè)計有所幫助。
總結(jié)
以上是生活随笔為你收集整理的mysql增加布尔字段_JDBC对MySQL数据库布尔字段的操作方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux防火墙配置管理,Linux之I
- 下一篇: Java 注解原理