2008-10-03

Java Type vs PostgreSQL Type (八) 整數型別之對應 -long

long and Long:
測試Java Types:long and Long時,首先建置一張資料表。
資料表語法:
CREATE TABLE longtypes
(
"long" bigint,
long1 bigint,
id serial NOT NULL,
CONSTRAINT longtypes_pkey PRIMARY KEY (id)
)


原始程式碼:
JavaLongType.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 JavaLongType {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private String setSQL = "Insert into longtypes (long,long1) values (?,?)";
private String getSQL = "select * from longtypes";

public static void main(String[] args)
throws IOException, ClassNotFoundException, SQLException {
JavaLongType jlt = new JavaLongType();
long l1 = 0l;
Long l2 = 0l;
jlt.setLongValue(1000000000000l, 10000000000000l);
ResultSet rs = jlt.getLongValue();
while (rs.next()) {
l1 = rs.getLong("long");
l2 = rs.getLong("long1");
System.out.println(l1 + " " + l2);
}
}

public void setLongValue(long l1, Long l2)
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(setSQL);
pstmt.setLong(1, l1);
pstmt.setLong(2, l2);
pstmt.execute();
pstmt.close();
conn.close();
}

public ResultSet getLongValue()
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(getSQL);
this.rs = pstmt.executeQuery();
return rs;
}
}

實際測試程序:
儲存Long值方式:
透過setLongValue(long , Long ) 這個方法,將傳入值10000000000,10000000000分別指派給long(基本型別),Long(外覆類別)。
再將這兩個變數分別存入PostgreSQL Type : bigint 的兩個欄位(long , long1)。傳入值在資料表上有呈現資料,表示傳入成功。


讀取Long值方式:
經getLongValue()的方法,先透過executeQuery(),將SQL語法"select * from longtypes"傳入資料庫查詢,回傳ResultSet指派給rs,再將rs回傳指派給main()區塊的ResultSet,而ResultSet保存著SQL查詢完後的資料表內容,再分別透過ResultSet將值傳回給long l1,以及Long l2,最後在印出結果10000000000,10000000000,為了確定印出值為資料庫所傳回的,在初始值上,將long l1 = 0 , Long l2 = 0,以作為
驗證其結果。

印出的結果:

整理表格如下:
Java Types PostgreSQL Types 特性
long
bigint 8bytes,大範圍的整數,-9223372036854775808 到 9223372036854775807
Long

沒有留言:

網誌存檔

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)