本文實(shí)例講述了JDBC對(duì)MySQL數(shù)據(jù)庫(kù)布爾字段的操作方法。分享給大家供大家參考。具體分析如下:
在Mysql數(shù)據(jù)庫(kù)如果要使用布爾字段,而應(yīng)該設(shè)置為BIT(1)類型
此類型在Mysql中不能通過(guò)MySQLQueryBrowser下方的Edit與Apply Changed去編輯
只能通過(guò)語(yǔ)句修改,比如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è)字段值
希望本文所述對(duì)大家的Java程序設(shè)計(jì)有所幫助。