PostgreSQL 影片

Loading...
Google

2008-10-01

Java Type vs PostgreSQL Type (二) 字元型別之對應

char and Character:
測試Java Types: char and Character 時,首先在PostgreSQL建置資料表
資料表語法:
CREATE TABLE chartype
(
"char" "char",
char1 "char",
id serial NOT NULL,
CONSTRAINT chartype_pkey PRIMARY KEY (id)
)


程式原始碼:
JavaCharTypes:


package zasax.type.chartypes;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.logging.Level;
import java.util.logging.Logger;
import zasax.conn.PostgreConnection;

/**
*
* @author YiCheng, Hsiao
*/
public class JavaCharType {

private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private String setSQL = "Insert into chartype (char,char1) values (?,?)";
private String getSQL = "Select * from chartype";

public static void main(String[] args) {
char ch1 = '1';
Character ch2 = new Character('1');
JavaCharType jct = new JavaCharType();
jct.setCharValue('a', 'b');
ResultSet rs = jct.getCharValue();
try {
while (rs.next()) {
ch1 = rs.getString("char").charAt(0);
ch2 = rs.getString("char1").charAt(0);
System.out.println(ch1 + " " + ch2);
}
} catch (SQLException ex) {
Logger.getLogger(JavaCharType.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void setCharValue(char ch1, Character ch2) {
PostgreConnection pc = new PostgreConnection();
try {
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(setSQL);
pstmt.setString(1, String.valueOf(ch1));
pstmt.setString(2, String.valueOf(ch2));
pstmt.execute();
pstmt.close();
conn.close();
} catch (IOException ex) {
Logger.getLogger(JavaCharType.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(JavaCharType.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(JavaCharType.class.getName()).log(Level.SEVERE, null, ex);
}
}
public ResultSet getCharValue() {
PostgreConnection pc = new PostgreConnection();
try {
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(getSQL);
this.rs = pstmt.executeQuery();
} catch (IOException ex) {
Logger.getLogger(JavaCharType.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(JavaCharType.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(JavaCharType.class.getName()).log(Level.SEVERE, null, ex);
}
return rs;
}
}

實際測試程序:
儲存Character值的方式:
在java.sql.PreparedStatement裡,並無存入字元的方法,所以要存取字元值,
得在存取之前將字元值,透過String.valueOf(char ch)的方法轉為字串,再透過
setString()將字串儲存到資料庫。


讀取字元值的方式:
經getBooleanValue()的方法,先透過executeQuery(),
將SQL語法"select * from chartypes"傳入資料庫查詢,回傳ResultSet指派
給rs,再將rs回傳指派給main()區塊的ResultSet,而ResultSet保存著SQL
查詢完後的資料表內容,再分別透過ResultSet將值傳回給char ch1,以及
Character ch2,最後在印出結果a,b,為了確定印出值為資料庫所傳回的,
在初始值上,將char ch1 = '1' , Character ch2 = new Character('1'),以作
為驗證其結果。而由於ResultSet並無讀取字元值的方法,所以得先透過讀
取字串再利用charAt(0)將回傳第一個字元,所以若要進行字元進出資料庫
的處理,得先轉型為字串,才得以進出資料庫。

整理表格如下:
Java Types PostgreSQL Types 特性
String(char , Character) character(n) 長度固定的不可變字元陣列
character varying(n) 長度固定的可變字元陣列
text 長度無限制的可變字元陣列

沒有留言:

網誌存檔

PostgreSQL & Google-Analytics Running...

::Planet PostgreSQL::

PostgreSQL Information Page

PostgreSQL日記(日本 石井達夫先生Blog)

PostgreSQL News

黑喵的家 - 資料庫相關

Google 網上論壇
PostgreSQL 8 DBA 專業指南中文版
書籍內容討論與更多下載區(造訪此群組)
目錄下載: PostgreSQL_8 _DBA_Index_zh_TW.pdf (更新:2007-05-18)

全球訪客分佈圖(Google)

全球訪客分佈圖(Google)