Java Type vs PostgreSQL Type (三) 布林型別之對應
boolean and Boolean:
測試Java Types: boolean and Boolean 時,首先在PostgreSQL上建置一張資料表。
資料表語法:
CREATE TABLE booleantypes
(
"boolean" boolean,
boolean1 boolean,
id serial NOT NULL,
CONSTRAINT booleantypes_pkey PRIMARY KEY (id)
)
測試Java Types: boolean and Boolean 時,首先在PostgreSQL上建置一張資料表。
資料表語法:
CREATE TABLE booleantypes
(
"boolean" boolean,
boolean1 boolean,
id serial NOT NULL,
CONSTRAINT booleantypes_pkey PRIMARY KEY (id)
)
程式原始碼:
JavaBooleanType.java:
package zasax.type.booleantypes;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import zasax.conn.PostgreConnection;
/**
*
* @author YiCheng,Hsiao
*/
public class JavaBooleanType {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private String setSQL = "Insert into booleantypes (boolean,boolean1) values (?,?)";
private String getSQL = "select * from booleantypes";
public static void main(String[] args) {
JavaBooleanType jbt = new JavaBooleanType();
boolean b1 = false;
Boolean b2 = true;
jbt.setBooleanValue(true,false);
ResultSet rs = jbt.getBooleanValue();
try {
while (rs.next()) {
b1 = rs.getBoolean("boolean");
b2 = rs.getBoolean("boolean1");
System.out.println(b1+" "+b2);
}
} catch (SQLException ex) {
Logger.getLogger(JavaBooleanType.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void setBooleanValue(boolean b1,Boolean b2) {
PostgreConnection pc = new PostgreConnection();
try {
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(setSQL);
pstmt.setBoolean(1, b1);
pstmt.setBoolean(2, b2);
pstmt.execute();
pstmt.close();
conn.close();
} catch (IOException ex) {
Logger.getLogger(JavaBooleanType.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(JavaBooleanType.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(JavaBooleanType.class.getName()).log(Level.SEVERE, null, ex);
}
}
public ResultSet getBooleanValue() {
PostgreConnection pc = new PostgreConnection();
try {
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(getSQL);
this.rs = pstmt.executeQuery();
} catch (IOException ex) {
Logger.getLogger(JavaBooleanType.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(JavaBooleanType.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(JavaBooleanType.class.getName()).log(Level.SEVERE, null, ex);
}
return rs;
}
}
實際測試程序:
儲存Boolean值方式:
透過setBooleanValue(boolean , Boolean ) 這個方法,將傳入值true,false分別指派給boolean(基本型別),Boolean(外覆類別)。再將這兩個變數分別存入PostgreSQL Type : boolean 的兩個欄位(boolean , boolean1)。傳入值在資料表上有呈現資料,表示傳入成功。
讀取Boolean值方式:
經getBooleanValue()的方法,先透過executeQuery(),將SQL語法"select * from booleantypes"傳入資料庫查詢,回傳ResultSet指派給rs,再將rs回傳指派給main()區塊的ResultSet,而ResultSet保存著SQL查詢完後的資料表內容,再分別透過ResultSet將值傳回給boolean b1,以及Boolean b2,最後在印出結果true,false,為了確定印出值為資料庫所傳回的,在初始值上,將boolean b1 = false , Boolean b2 = true,以作為驗證其結果。
整理表格如下:
Java 型別 | PostgreSQL 型別 | 特性 |
boolean | boolean | 只有true 或者 false兩種狀態,而第三種"Unknown"以null表示。 |
Boolean |
沒有留言:
張貼留言