本文實(shí)例講述了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操作這個(gè)字段的代碼如下:
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(); } } }
先輸出這個(gè)字段值,再把其改為true,再輸出這個(gè)字段值
希望本文所述對大家的Java程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選