2008-10-03

Java Type vs PostgreSQL Type (六) 整數型別之對應 -short

short and Short:
測試Java Types:short and Short時,先建置一張資料表。
資料表語法:
CREATE TABLE shorttypes
(
short smallint,
short1 smallint,
id serial NOT NULL,
CONSTRAINT shorttypes_pkey PRIMARY KEY (id)
)

程式原始碼:
JavaShortType.java:
package zasax.type.numbertypes.integertypes;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import zasax.conn.PostgreConnection;
/**
*
* @author YiCheng,Hsiao
*/
public class JavaShortType {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private String setSQL = "Insert into shorttypes (short,short1) values (?,?)";
private String getSQL = "select * from shorttypes";
public static void main(String[] args)
throws IOException, ClassNotFoundException, SQLException {
JavaShortType jst = new JavaShortType();
short s = 32767;
short s1 = 0;
Short s2 = 0;
jst.setShortValue(s, s);
ResultSet rs = jst.getShortValue();
while(rs.next()){
s1 = rs.getShort("short");
s2 = rs.getShort("short1");
System.out.println(s1+" "+s2);
}
}
public void setShortValue(short s1, Short s2)
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(setSQL);
pstmt.setShort(1, s1);
pstmt.setShort(2, s2);
pstmt.execute();
pstmt.close();
conn.close();
}
public ResultSet getShortValue()
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(getSQL);
this.rs = pstmt.executeQuery();
return rs;
}
}
實際測試流程:
儲存Short值方式:
透過setShortValue(short , Short ) 這個方法,將傳入值byte b = 127分別指派給short(基本型別),Short(外覆類別)。
再將這兩個變數分別存入PostgreSQL Type : smallint 的兩個欄位(short , short1)。傳入值在資料表上有呈現資料,表示傳入成功。
讀取Short值方式:
經getShortValue()的方法,先透過executeQuery(),將SQL語法"select * from shorttypes"傳入資料庫查詢,回傳ResultSet指派給rs,再將rs回傳指派給main()區塊的ResultSet,而ResultSet保存著SQL查詢完後的資料表內容,再分別透過ResultSet將值傳回給short s1,以及Short s2,最後在印出結果32767,32767,為了確定印出值為資料庫所傳回的,在初始值上,將short s1 = 0 , Short s2 = 0,以作為
驗證其結果。
整理表格如下:
Java Types PostgreSQL Types 特性
short
smallint 2 bytes,小範圍整數,-32768 到 +32767
Short

沒有留言:

網誌存檔

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)