これは MySQL で動作し、テーブルは false の場合は 0、true の場合は 1 を保持します。
出力:
123 => true
456 => false
ソースコード:
package com.lurz.jdbc;
import java.sql.*;
// Test using Varchar for Boolean
public class BoolTest {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/booltest", "booltest", "booltest");
conn.prepareStatement("create table booltest (id bigint, truefalse varchar(10));").execute();
PreparedStatement stmt = conn.prepareStatement("insert into booltest (id, truefalse) values (?, ?);");
stmt.setLong(1, (long)123);
stmt.setBoolean(2, true);
stmt.execute();
stmt.setLong(1, (long)456);
stmt.setBoolean(2, false);
stmt.execute();
ResultSet rs = conn.createStatement().executeQuery("select id, truefalse from booltest");
while (rs.next()) {
System.out.println(rs.getLong(1)+ " => " + rs.getBoolean(2));
}
}
}