Is it possible to store and retrieve a boolean in a varchar field using Java JDBC?

Quick question: my client has a situation where he has his own database with the varchar field, and the corresponding jdbc code stores / retrieves a boolean value.

I assume that the boolean values ​​false and true will be translated to "0" and "1", but I would like to receive confirmation of this (I can not find the exact specifications of the behavior online, it may depend on each driver, Oracle in this case) .

I know that I can experiment myself, but I want to try on stackoverflow.com!

Thanks for your reply,

Eric

+3
source share
5 answers

, , , . JDBC - .

, JDBC, ('true' 'false' ), VARCHAR. VARCHAR , , String , .

+2

MySQL, 0 false 1 true.

:

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));
      }  
   }
}
+1

,

, Oracle 0 1 boolean varchar2 - JDBC.

, , , "". , .

, , , stackoverflow Joel/Jeff !

!

+1

, BOOLEAN . , Oracle VARCHAR "true" "false", 0 1, .

0

VARCHAR (2) Java Boolean Oracle. , NUMBER (1). Boolean , WHERE, Oracle to_number, "0" 0 "1" 1. .

0

Source: https://habr.com/ru/post/1697091/


All Articles