在Oracle和PG中都可以使用int類型模擬布爾類型,但通過JDBC接口(JDBC驅(qū)動,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代碼
創(chuàng)新互聯(lián)公司自成立以來,一直致力于為企業(yè)提供從網(wǎng)站策劃、網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計、做網(wǎng)站、電子商務(wù)、網(wǎng)站推廣、網(wǎng)站優(yōu)化到為企業(yè)提供個性化軟件開發(fā)等基于互聯(lián)網(wǎng)的全面整合營銷服務(wù)。公司擁有豐富的網(wǎng)站建設(shè)和互聯(lián)網(wǎng)應(yīng)用系統(tǒng)開發(fā)管理經(jīng)驗、成熟的應(yīng)用系統(tǒng)解決方案、優(yōu)秀的網(wǎng)站開發(fā)工程師團(tuán)隊及專業(yè)的網(wǎng)站設(shè)計師團(tuán)隊。
/*
*
*/
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
執(zhí)行結(jié)果
---------- PG -----------
id:1,c1:1,b1:true
id:2,c1:-1,b1:false
id:3,c1:2,b1:false
id:4,c1:0,b1:false
---------- Oracle -----------
id:1,c1:1,b1:true
id:2,c1:-1,b1:true
id:3,c1:2,b1:true
id:4,c1:0,b1:false