Oracle vs PostgreSQL Develop(18) - Boolean
发表于:2024-10-27 作者:千家信息网编辑
千家信息网最后更新 2024年10月27日,在Oracle和PG中都可以使用int类型模拟布尔类型,但通过JDBC接口(JDBC驱动,Oracle为11.2.0.4,PG为9.3)获取出来的值却不一致,这一点需要注意。测试脚本drop tabl
千家信息网最后更新 2024年10月27日Oracle vs PostgreSQL Develop(18) - Boolean
在Oracle和PG中都可以使用int类型模拟布尔类型,但通过JDBC接口(JDBC驱动,Oracle为11.2.0.4,PG为9.3)获取出来的值却不一致,这一点需要注意。
测试脚本
drop table tbl1;create table tbl1(id int,c1 int);insert into tbl1 values(1,1);insert into tbl1 values(2,-1);insert into tbl1 values(3,2);insert into tbl1 values(4,0);
Java代码
/* * */package testPG;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class TestBoolean { public static void main(String[] args) { System.out.println("---------- PG -----------"); try (Connection conn = DriverManager.getConnection("jdbc:postgresql://192.168.26.28:5432/testdb", "pg12", "pg12")) { TestBool(conn); } catch (SQLException se) { System.out.println(se.getMessage()); } catch (Exception e) { e.printStackTrace(); } finally { } // end try System.out.println("---------- Oracle -----------"); try (Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.18:1521:orcl", "test", "test")) { TestBool(conn); } catch (SQLException se) { System.out.println(se.getMessage()); } catch (Exception e) { e.printStackTrace(); } finally { } // end try } public static void TestBool(Connection conn) { try (PreparedStatement pstmt = conn.prepareStatement("SELECT id,c1 from tbl1"); ResultSet rs = pstmt.executeQuery();) { conn.setAutoCommit(true); while (rs.next()) { int id = rs.getInt("id"); int c1 = rs.getInt("c1"); boolean b1 = rs.getBoolean("c1"); System.out.println("id:" + id + ",c1:" + c1 + ",b1:" + b1); } } catch (SQLException se) { System.out.println(se.getMessage()); } catch (Exception e) { e.printStackTrace(); } finally { } // end try } // end} // end Class
执行结果
---------- PG -----------id:1,c1:1,b1:trueid:2,c1:-1,b1:falseid:3,c1:2,b1:falseid:4,c1:0,b1:false---------- Oracle -----------id:1,c1:1,b1:trueid:2,c1:-1,b1:trueid:3,c1:2,b1:trueid:4,c1:0,b1:false
使用JDBC,在PG中,只有1视为T,其他为F;而在Oracle中,只有0视为F,其他视为T。
但在PG中,如把int转换为boolean,则行为与Oracle一致。
[local]:5432 pg12@testdb=# create table tbl2(id int,c1 int);CREATE TABLETime: 13.911 ms[local]:5432 pg12@testdb=# insert into tbl2 values(1,1);INSERT 0 1Time: 5.091 ms[local]:5432 pg12@testdb=# insert into tbl2 values(2,-1);INSERT 0 1Time: 2.653 ms[local]:5432 pg12@testdb=# insert into tbl2 values(3,2);INSERT 0 1Time: 2.716 ms[local]:5432 pg12@testdb=# insert into tbl2 values(4,0);INSERT 0 1Time: 2.625 ms[local]:5432 pg12@testdb=# [local]:5432 pg12@testdb=# select id,c1 from tbl2; id | c1 ----+---- 1 | 1 2 | -1 3 | 2 4 | 0(4 rows)Time: 3.183 ms[local]:5432 pg12@testdb=# alter table tbl2 alter column c1 type boolean using c1::boolean;ALTER TABLETime: 30.581 ms[local]:5432 pg12@testdb=# select id,c1 from tbl2; id | c1 ----+---- 1 | t 2 | t 3 | t 4 | f(4 rows)Time: 2.566 ms[local]:5432 pg12@testdb=#
一致
只有
类型
代码
布尔
接口
结果
脚本
行为
测试
驱动
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全黑板报 设计
nosql数据库设计的技术
hp服务器移动硬盘恢复系统
实习内容怎么写网络技术
河南信睿网络技术有限公司
淘宝网多少台服务器
web服务器开发软件有哪些
服务器域名注册商一样吗
oracle数据库安全基线
飓风网络安全科技馆
计算机网络安全包括
复制服务器文件安全提示
怀旧服如何取消连接服务器
福州网龙软件开发待遇
网络安全从我做起小报
河南农业职业学院网络安全
db2数据库安装linux
关于网络安全的资料手抄报
第三方登录服务器
哪些大学买了人民日报数据库
网络技术 简介
安徽软件网络安全
软件开发项目编程
服务器域名注册商一样吗
网络安全手抄报的写字内容
软件开发人员基本要求
华为清除应用市场数据库
淘宝软件开发怎么问卖家
网络安全意识培训的目标
松江区品牌软件开发销售价格