2008-10-18
2008-10-09
2008-10-05
Java Type - 時間型別之間之差異 以PostgreSQL對應
測試平台:JDK SE 6
測試目標:
java.util.Date
java.util.Calendar
java.sql.Date
java.sql.Time
java.sql.Timestamp
測試目的:
對於五種都是所謂的時間類別,有什麼區別。
1.是否能自己產生時間:
java.util.Date:
在java.util.Date建構子上,預設的情況,public Date() { this(System.currentTimeMillis()); } ,預設會調用System.currentTimeMillis()的方法。
System.currentTimeMillis()在API的內容:
currentTimeMillis
public static long currentTimeMillis()
- 返回以毫秒為單位的當前時間。注意,當返回值的時間單位是毫秒時,值的粒度取決於底層作業系統,並且粒度可能更大。例如,許多作業系統以幾十毫秒為單位測量時間。
請參閱
Date類別的描述,瞭解可能發生在“計算機時間”和協調世界時(UTC)之間的細微差異的討論。 -
- 返回:
- 當前時間與協調世界時 1970 年 1 月 1 日午夜之間的時間差(以毫秒為單位測量)。
- 另請參見:
Date
java.util.Calendar:
java.util.Calendar為抽象類別,建構方式透過Calendar.getInstance()初始, public static Calendar getInstance() { Calendar cal = createCalendar(TimeZone.getDefaultRef(), Locale.getDefault()); cal.sharedZone = true; return cal; } ,在預設的情況下會順便invoked TimeZone.getDefault(),以及Loale.getDefault()這兩個方法。
TimeZone.getDefault()在API的內容:
getDefault
public static TimeZone getDefault()
- 獲取此主機的預設
TimeZone。預設TimeZone的來源可能隨實作的變化而變化。 -
- 返回:
- 預設的
TimeZone。 - 另請參見:
setDefault(java.util.TimeZone)
getDefault
public static Locale getDefault()
- 獲得此 Java 虛擬機器實例的當前預設語言環境值。
Java 虛擬機器根據主機的環境在啟動期間設置預設語言環境。如果沒有明確地指定語言環境,則很多語言環境敏感的方法都使用該方法。可使用
setDefault方法更改該值。 -
- 返回:
- 此 Java 虛擬機器實例的預設語言環境。
java.sql.Date:
在API的內容:
public class Date
- extends Date
一個套件裝了毫秒值的瘦外覆類別 (thin wrapper),它允許 JDBC 將毫秒值標識為 SQL DATE 值。毫秒值表示自 1970 年 1 月 1 日 00:00:00 GMT 以來經過的毫秒數。
為了與 SQL DATE 的定義一致,由 java.sql.Date 實例包裹的毫秒值必須通過將小時、分鐘、秒和毫秒設置為與該實例相關的特定時區中的零來“規範化”。
可以得知,java.sql.Date屬於一個wrapper class,專門用來承接時間數據,轉為與資料庫中的date型態相符合的型態。
所以java.sql.Date,並無法自己產生出時間。
java.sql.Time:
在API的內容:
public class Time
- extends Date
一個與 java.util.Date 類別有關的瘦外覆類別 (thin wrapper),它允許 JDBC 將該類別標識為 SQL TIME 值。Time 類別添加格式化和解析操作以支持時間值的 JDBC 轉義語法。
應該將日期元件設置為 1970 年 1 月 1 日的 "zero epoch" 值並且不應存取該值。
可以得知,java.sql.Time屬於一個wrapper class,專門用來承接時間數據,轉為與資料庫中的time型態相符合的型態。
所以java.sql.Time,並無法自己產生出時間。
java.sql.Timestamp:
在API的內容:
public class Timestamp
- extends Date
一個與 java.util.Date 類別有關的瘦外覆類別 (thin wrapper),它允許 JDBC API 將該類別標識為 SQL TIMESTAMP 值。它通過允許小數秒到納秒級精度的規範來添加保存 SQL TIMESTAMP 小數秒值的能力。Timestamp 也提供支持時間戳值的 JDBC 轉義語法的格式化和解析操作的能力。
計算 Timestamp 物件的精度為二者之一:
19,其為 yyyy-mm-dd hh:mm:ss 格式下的字元數20 + s,其為 yyyy-mm-dd hh:mm:ss.[fff...] 格式下的字元數,s表示給定 Timestamp 的標度(其小數秒精度)。
註:此型別由 java.util.Date 和單獨的毫微秒值組成。只有整數秒才會存儲在 java.util.Date 元件中。小數秒(毫微秒)是獨立存在的。傳遞不是 java.sql.Timestamp 實例的物件時,Timestamp.equals(Object) 方法永遠不會返回 true,因為日期的毫微秒元件是未知的。因此,相對於 java.util.Date.equals(Object) 方法而言,Timestamp.equals(Object) 方法是不對稱的。此外,hashcode 方法使用底層 java.util.Date 實作並因此在其計算中不包括毫微秒。
鑒於 Timestamp 類別和上述 java.util.Date 類別之間的不同,建議程式碼一般不要將 Timestamp 值視為 java.util.Date 的實例。Timestamp 和 java.util.Date 之間的繼承關係實際上指的是實作繼承,而不是型別繼承。
可以得知,java.sql.Timestamp屬於一個wrapper class,專門用來承接時間數據,轉為與資料庫中的timestamp型態相符合的型態。所以java.sql.Timestamp,並無法自己產生出時間。
2.對應資料庫存取方法區別:
以下為API內容,選擇java.sql.PreparedStatement內的方法:
setDate
void setDate(int parameterIndex,
Date x)
throws SQLException
- 使用運行應用程序的虛擬機器的預設時區將指定參數設置為給定
java.sql.Date值。在將此值發送到資料庫時,驅動程序將它轉換成一個 SQLDATE值。 -
- 參數:
parameterIndex- 第一個參數是 1,第二個參數是 2,……x- 參數值- 拋出:
SQLException- 如果 parameterIndex 不對應於 SQL 語句中的參數標記;如果發生資料庫存取錯誤,或者在關閉的PreparedStatement上調用此方法
setTimestamp
void setTimestamp(int parameterIndex,
Timestamp x)
throws SQLException
- 將指定參數設置為給定
java.sql.Timestamp值。在將此值發送到資料庫時,驅動程序將它轉換成一個 SQLTIMESTAMP值。 -
- 參數:
parameterIndex- 第一個參數是 1,第二個參數是 2,……x- 參數值- 拋出:
SQLException- 如果 parameterIndex 不對應於 SQL 語句中的參數標記;如果發生資料庫存取錯誤,或者在關閉的PreparedStatement上調用此方法
setTime
void setTime(int parameterIndex,
Time x)
throws SQLException
- 將指定參數設置為給定
java.sql.Time值。在將此值發送到資料庫時,驅動程序將它轉換成一個 SQLTIME值。 -
- 參數:
parameterIndex- 第一個參數是 1,第二個參數是 2,……x- 參數值- 拋出:
SQLException- 如果 parameterIndex 不對應於 SQL 語句中的參數標記;如果發生資料庫存取錯誤,或者在關閉的PreparedStatement上調用此方法
由上面三個主要存取給資料庫型別date, time, timestamp 的方法,可以明確了解,在JDBC的要求上,透過java.sql.Date, java.sql.Time, java.sql.Timestamp,最為符合跟資料庫存取的規格,所以java.util.Date, java.util.Calendar,要存進資料庫時,得透過java.sql.Date, java.sql.Time, java.sql.Timestamp 的包覆,才便於存進資料庫。
整理表格如下:
| Java Types | 自己產生時間 | 存入PostgreSQL資料庫的對應型別 |
| java.util.Date | T | 需要包覆 |
| java.util.Calendar | T | 需要包覆 |
| java.sql.Date | F | date |
| java.sql.Time | F | time |
| java.sql.Timestamp | F | timestamp |
2008-10-04
java.sql.PreparedStatement的方法測試-對應PostgreSQL JDBC4
測試類別: java.sql.PreparedStatement
測試目標: setBinaryStream()
測試jdk: jdk1.6.0_10
測試選擇資料庫:PostgreSQL 8.3.3
測試資料庫對應之JDBC: postgresql-8.3-603.jdbc4
測試原始碼:
package zasax.test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import zasax.conn.PostgreConnection;
/**
*
* @author YiCheng,Hsiao
*/
public class PreparedStament_SetBinaryStreamTest {
private Connection conn;
private PreparedStatement pstmt;
private String setSQL = "Insert into checktest (\"binary\") values (?)";
public static void main(String[] args)
throws IOException, ClassNotFoundException, SQLException {
PreparedStament_SetBinaryStreamTest psst =
new PreparedStament_SetBinaryStreamTest();
psst.setBinaryStreamValue();
}
public void setBinaryStreamValue()
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(setSQL);
//pstmt.setBinaryStream(1, new ByteArrayInputStream(new byte[10]));
//pstmt.setBinaryStream(1, new ByteArrayInputStream(new byte[10]), 10);
//pstmt.setBinaryStream(1, new ByteArrayInputStream(new byte[10]), 10l);
pstmt.execute();
pstmt.close();
conn.close();
}
}
setBinaryStream() 在Java SE API 的內容:
setBinaryStream
void setBinaryStream(int parameterIndex,
InputStream x)
throws SQLException
- 將指定參數設置為給定輸入串流。在將一個非常大的二進制值輸入到
LONGVARBINARY參數時,通過java.io.InputStream物件發送它可能更為實際。將根據需要從串流中讀取資料,一直讀取到檔案末尾。註:此串流物件既可以是一個標準 Java 串流物件,也可以是實作標準介面的使用者自己的子類別。
註:查詢 JDBC 驅動程序文檔,以確定使用帶 length 參數的
setBinaryStream是否更有效。 -
- 參數:
parameterIndex- 第一個參數是 1,第二個參數是 2,……x- 套件含二進制參數值的 java 輸入串流- 拋出:
SQLException- 如果 parameterIndex 不對應於 SQL 語句中的參數標記;- 如果發生資料庫存取錯誤,或者在關閉的
PreparedStatement上調用此方法 SQLFeatureNotSupportedException- 如果 JDBC 驅動程序不支持此方法- 從以下版本開始:
- 1.6
setBinaryStream
void setBinaryStream(int parameterIndex,
InputStream x,
int length)
throws SQLException
- 將指定參數設置為給定輸入串流,該輸入串流將具有給定位元組數。
- 在將一個非常大的二進制值輸入到
LONGVARBINARY參數時, - 通過
java.io.InputStream物件發送它可能更為實際。 - 將根據需要從串流中讀取資料,一直讀取到檔案末尾。
註:此串流物件既可以是一個標準 Java 串流物件,
-
也可以是實作標準介面的使用者自己的子類別。
-
- 參數:
parameterIndex- 第一個參數是 1,第二個參數是 2,……x- 套件含二進制參數值的 Java 輸入串流length- 串流中的位元組數- 拋出:
SQLException- 如果 parameterIndex 不對應於 SQL 語句中的參數標記;- 如果發生資料庫存取錯誤,或者在關閉的
PreparedStatement上調用此方法
setBinaryStream
void setBinaryStream(int parameterIndex,
InputStream x,
long length)
throws SQLException
- 將指定參數設置為給定輸入串流,該輸入串流將具有指定位元組數。
- 在將一個非常大的二進制值輸入到
LONGVARBINARY參數時, - 通過
java.io.InputStream物件發送它可能更為實際。 - 將根據需要從串流中讀取資料,一直讀取到檔案末尾。
註:此串流物件既可以是一個標準 Java 串流物件,
-
也可以是實作標準介面的使用者自己的子類別。
-
- 參數:
parameterIndex- 第一個參數是 1,第二個參數是 2,……x- 套件含二進制參數值的 java 輸入串流length- 串流中的位元組數- 拋出:
SQLException- 如果 parameterIndex 不對應於 SQL 語句中的參數標記;- 如果發生資料庫存取錯誤,或者在關閉的
PreparedStatement上調用此方法 - 從以下版本開始:
- 1.6
setBinaryStream()方法所印出結果:
setBinaryStream(int parameterIndex, InputStream x ) throws SQLException
setBinaryStream(int parameterIndex, InputStream x, int length ) throws SQLException
setBinaryStream(int parameterIndex, InputStream x, long length ) throws SQLException
結論:
確認環境為postgresql-8.3-603.jdbc4及jdk1.6.0_10,而setBinaryStream(int, InputStream) 及 setBinaryStream(int, InputStream, long)所拋出的未實作的完成的異常,而拋出異常的位置為org.postgresql.util.PSQLException,表示setBinaryStream(int, InputStream) 及 setBinaryStream(int, InputStream, long)在PostgreSQL JDBC中還未實作出來,所以要使用setBinaryStream()方法時,還是得透過setBinaryStream(int, InputStream, int)。
標籤: JDBC, PG性能與各DBMS差異性
2008-10-03
Java Type vs PostgreSQL Type (零) 總整理
撰寫動機:
面對PostgreSQL 龐大的型別組織,對於JAVA的型別對應上,難免會有所選擇,
若有詳細的比較JAVA型別對應PostgreSQL型別的資料,可以便於撰寫程式。
測試目的:
選擇使用頻率較高的型別做測試,包含基本型別,外覆類別,日期,二進制檔案(img and mp3)
還有序列化檔案等,作為測試要素,以尋求出PostgreSQL所對應型別。
| Java Types | PostgreSQL Types |
| 布林型別 | |
| boolean | boolean |
| Boolean | |
| 字元型別 | |
| String(char , Character) | character |
| character varying | |
| text | |
| 整數型別 | |
| byte | integer(smallint) |
| Byte | |
| short | |
| Short | |
| int | |
| Integer | |
| long | bigint |
| Long | |
| 浮點數型別 | |
| float | double precision (real) |
| Float | |
| double | |
| Double | |
| 時間型別 | |
| java.util.Date | date,time,timestamp |
| java.util.Calendar | |
| 二進制檔案 | |
| .gif | bytea |
| .jpg | |
| .png | |
| .mp3 | |
| 序列化資料 | |
| SerializableData (SampleData(Object)) | bytea |
Java Type vs PostgreSQL Type (十四) 序列化資料之對應
(
serial bytea,
id serial NOT NULL,
CONSTRAINT serialtypes_pkey PRIMARY KEY (id)
)
package zasax.type.binarytypes;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import zasax.conn.PostgreConnection;
import zasax.sample.SampleData;
import zasax.serial.SerializableFlow;
*
* @author YiCheng,Hsiao
*/
public class JavaSerializableType {
private PreparedStatement pstmt;
private ResultSet rs;
private String setSQL = "Insert into serialtypes (serial) values (?)";
private String getSQL = "select * from serialtypes";
throws IOException, ClassNotFoundException, SQLException {
SerializableFlow sf = new SerializableFlow();
JavaSerializableType jst = new JavaSerializableType();
SampleData sd = new SampleData();
sd.setStr("這是個序列化檔案的測試。");
SampleData sd1;
ByteArrayOutputStream baos =
(ByteArrayOutputStream) sf.serializeToByte(sd);
jst.setSerializableValue(
new ByteArrayInputStream(baos.toByteArray()), baos.toByteArray().length);
ResultSet rs = jst.getSerializableValue();
while (rs.next()) {
sd1 = (SampleData) sf.decodeSerializableValue(rs.getBytes("serial"));
System.out.println(sd1.getStr());
}
}
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(setSQL);
pstmt.setBinaryStream(1, is, length);
pstmt.execute();
pstmt.close();
conn.close();
}
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(getSQL);
this.rs = pstmt.executeQuery();
return rs;
}
}
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
*
* @author YiCheng,Hsiao
*/
public class SerializableFlow
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(toSerializableValue);
return baos;
}
throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream(getSerializbleValue));
return (T) ois.readObject();
}
}
*
* @author YiCheng,Hsiao
*/
public class SampleData implements Serializable {
return str;
}
this.str = str;
}
}
| Java Types | PostgreSQL Types | 特性 |
| SerializableData (SampleData(Object)) | bytea | 4 bytes加上實際的二進制字串,變長的二進制字串 |
Java Type vs PostgreSQL Type (十三) 二進制檔案之對應
二進制檔案:
再進行二進制檔案測試時,先在資料庫創建一張資料表。
資料表語法:
CREATE TABLE binarytypes
(
gif bytea,
jpg bytea,
png bytea,
mp3 bytea,
id serial NOT NULL,
CONSTRAINT binarytypes_pkey PRIMARY KEY (id)
)
原始程式碼:
JavaBinaryFileType:
package zasax.type.binarytypes;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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 JavaBinaryFileType {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private String setSQL = "Insert into binarytypes (gif,jpg,png,mp3) values (?,?,?,?)";
private String getSQL = "select * from binarytypes";
public static void main(String[] args)
throws IOException, SQLException, ClassNotFoundException {
JavaBinaryFileType jbft = new JavaBinaryFileType();
String fileroad = "binaryfiles/";
jbft.setBinaryValue(
new File(fileroad + "man.gif"), new File(fileroad + "po.jpg"),
new File(fileroad + "allpass.png"), new File(fileroad + "song.mp3"));
ResultSet rs = jbft.getBinaryValue();
for (int i = 1; rs.next(); i++) {
new FileOutputStream
(fileroad + "man" + i + ".gif").write(rs.getBytes("gif"));
new FileOutputStream
(fileroad + "po" + i + ".jpg").write(rs.getBytes("jpg"));
new FileOutputStream
(fileroad + "allpass" + i + ".png").write(rs.getBytes("png"));
new FileOutputStream
(fileroad + "song" + i + ".mp3").write(rs.getBytes("mp3"));
}
}
public void setBinaryValue(File gif, File jpg, File png, File mp3)
throws IOException, SQLException, ClassNotFoundException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(setSQL);
pstmt.setBinaryStream(1, new FileInputStream(gif), (int) gif.length());
pstmt.setBinaryStream(2, new FileInputStream(jpg), (int) jpg.length());
pstmt.setBinaryStream(3, new FileInputStream(png), (int) png.length());
pstmt.setBinaryStream(4, new FileInputStream(mp3), (int) mp3.length());
pstmt.execute();
pstmt.close();
conn.close();
}
public ResultSet getBinaryValue()
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(getSQL);
this.rs = pstmt.executeQuery();
return rs;
}
}
實際測試程序:
透過setBinaryValue(File, File, File, File) 分別傳入.gif, .jpg, .png. .mp3 等各式檔案。再透過setBinaryStream(int, InputStream, int)的方法,將資料儲存至資料庫。
經getBinaryValue()的方法,先透過executeQuery(),將SQL語法"select * from binarytypes"傳入資料庫查詢,回傳ResultSet指派給rs,再將rs回傳指派給main()區塊的ResultSet,而ResultSet保存著SQL查詢完後的資料表內容,再分別透過ResultSet資料寫入FileOutputStream,再透過FileOutputStream的方法write(),將資料寫出成檔案。
印出的結果:
整理表格如下:
| Java Types | PostgreSQL Types | 特性 |
| .gif | bytea | 4 bytes加上實際的二進制字串,變長的二進制字串 |
| .jpg | ||
| .png | ||
| .mp3 |
Java Type vs PostgreSQL Type (十二) 時間型別之對應 -java.util.Calendar
(
date date,
"time" time without time zone,
"timestamp" timestamp without time zone,
id serial NOT NULL,
CONSTRAINT datetypes_pkey PRIMARY KEY (id)
)
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import zasax.conn.PostgreConnection;
*
* @author YiCheng,Hsiao
*/
public class JavaCalendarType {
private PreparedStatement pstmt;
private ResultSet rs;
private String setSQL = "Insert into calendartypes (date,time,timestamp) values (?,?,?)";
private String getSQL = "select * from calendartypes";
throws IOException, ClassNotFoundException, SQLException {
JavaCalendarType jct = new JavaCalendarType();
jct.setCalendarValue(Calendar.getInstance(), Calendar.getInstance(), Calendar.getInstance());
ResultSet rs = jct.getCalendarValue();
while (rs.next()) {
System.out.println("Date:" + rs.getDate("date"));
System.out.println("Time:" + rs.getTime("time"));
System.out.println("Timestamp:" + rs.getTimestamp("timestamp"));
}
}
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(setSQL);
pstmt.setDate(1, new java.sql.Date(c1.getTimeInMillis()));
pstmt.setTime(2, new java.sql.Time(c2.getTimeInMillis()));
pstmt.setTimestamp(3, new java.sql.Timestamp(c3.getTimeInMillis()));
pstmt.execute();
pstmt.close();
conn.close();
}
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(getSQL);
this.rs = pstmt.executeQuery();
return rs;
}
}
| Java Types | PostgreSQL Types | 特性 |
| java.util.Calendar | date | 4 bytes,只用於日期 |
| time | 8 bytes,只用於一日內時間 | |
| timestamp | 8 bytes,包括日期和時間 |
備註:
| Java Types | 自己產生時間 | 存入PostgreSQL資料庫的對應型別 |
| java.util.Date | T | 需要轉譯 |
| java.util.Calendar | T | 需要轉譯 |
| java.sql.Date | F | date |
| java.sql.Time | F | time |
| java.sql.Timestamp | F | timestamp |
Java Type vs PostgreSQL Type (十一) 時間型別之對應 -java.util.Date
(
date date,
"time" time without time zone,
"timestamp" timestamp without time zone,
id serial NOT NULL,
CONSTRAINT datetypes_pkey PRIMARY KEY (id)
)
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import zasax.conn.PostgreConnection;
*
* @author YiCheng,Hsiao
*/
public class JavaDateType {
private PreparedStatement pstmt;
private ResultSet rs;
private String setSQL = "Insert into datetypes (date,time,timestamp) values (?,?,?)";
private String getSQL = "select * from datetypes";
throws IOException, ClassNotFoundException, SQLException {
JavaDateType jdt = new JavaDateType();
jdt.setDateValue(new Date(), new Date(), new Date());
ResultSet rs = jdt.getDateValue();
while (rs.next()) {
System.out.print("Date:"+rs.getDate("date")+" ");
System.out.print("Time:"+rs.getTime("time")+" ");
System.out.println("Timestamp:"+rs.getTimestamp("timestamp"));
}
}
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(setSQL);
pstmt.setDate(1, new java.sql.Date(d1.getTime()));
pstmt.setTime(2, new java.sql.Time(d2.getTime()));
pstmt.setTimestamp(3, new java.sql.Timestamp(d3.getTime()));
pstmt.execute();
pstmt.close();
conn.close();
}
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(getSQL);
this.rs = pstmt.executeQuery();
return rs;
}
}
| Java Types | PostgreSQL Types | 特性 |
| java.util.Date | date | 4 bytes,只用於日期 |
| time | 8 bytes,只用於一日內時間 | |
| timestamp | 8 bytes,包括日期和時間 |
Java Type vs PostgreSQL Type (十) 浮點數型別之對應 -double
double and Double:
測試Java Types:double and Double時,首先建置一張資料表。
資料表語法:
CREATE TABLE doubletypes
(
"double" double precision,
double1 double precision,
id serial NOT NULL,
CONSTRAINT doubletypes_pkey PRIMARY KEY (id)
)
原始程式碼:
JavaDoubleType.java:
package zasax.type.numbertypes.doubletypes;
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 JavaDoubleType {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private String setSQL = "Insert into doubletypes (double,double1) values (?,?)";
private String getSQL = "select * from doubletypes";
public static void main(String[] args)
throws IOException, ClassNotFoundException, SQLException {
JavaDoubleType jdt = new JavaDoubleType();
double d1 = 0.0;
Double d2 = 0.0;
jdt.setDoubleValue(1000000.333555444, 1000000.333555444);
ResultSet rs = jdt.getDoubleValue();
while (rs.next()) {
d1 = rs.getDouble("double");
d2 = rs.getDouble("double1");
System.out.println(d1 + " " + d2);
}
}
public void setDoubleValue(double d1, Double d2)
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(setSQL);
pstmt.setDouble(1, d1);
pstmt.setDouble(2, d2);
pstmt.execute();
pstmt.close();
conn.close();
}
public ResultSet getDoubleValue()
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(getSQL);
this.rs = pstmt.executeQuery();
return rs;
}
}
實際測試程序:
透過setDoubleValue(double , Double ) 這個方法,將傳入值1000000.333555444,1000000.333555444分別指派給double(基本型別),Double(外覆類別)。再將這兩個變數分別存入PostgreSQL Type : double precision的兩個欄位(double , double1)。傳入值在資料表上有呈現資料,表示傳入成功。
經getDoubleValue()的方法,先透過executeQuery(),將SQL語法"select * from doubletypes"傳入資料庫查詢,回傳ResultSet指派給rs,再將rs回傳指派給main()區塊的ResultSet,而ResultSet保存著SQL查詢完後的資料表內容,再分別透過ResultSet將值傳回給double d1,以及Double d2,最後在印出結1000000.333555444,1000000.333555444,為了確定印出值為資料庫所傳回的,在初始值上,將double d1 = 0.0 ,Double d2 = 0.0,以作為驗證其結果。
印出的結果:
整理表格如下:
| Java Types | PostgreSQL Types | 特性 |
| double | double precision | 8 bytes,變精度,不精確 15 位十進制數字精度 |
| Double |
Java Type vs PostgreSQL Type (九) 浮點數型別之對應 -float
float and Float:
測試Java Types:float and Float時,首先建置一張資料表。
資料表語法:
CREATE TABLE floattypes
(
"float" real,
float1 real,
id serial NOT NULL,
CONSTRAINT floattypes_pkey PRIMARY KEY (id)
)
原始程式碼:
JavaFloatType.java:
package zasax.type.numbertypes.doubletypes;
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 JavaFloatType {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private String setSQL = "Insert into floattypes (float,float1) values (?,?)";
private String getSQL = "select * from floattypes";
public static void main(String[] args)
throws IOException, ClassNotFoundException, SQLException {
JavaFloatType jft = new JavaFloatType();
float f1 = 0f;
Float f2 = 0f;
jft.setFloatValue(1234567.45678f, 7654321.65487f);
ResultSet rs = jft.getFloatValue();
while (rs.next()) {
f1 = rs.getFloat("float");
f2 = rs.getFloat("float1");
System.out.println(f1 + " " + f2);
}
}
public void setFloatValue(float f1, Float f2)
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(setSQL);
pstmt.setFloat(1, f1);
pstmt.setFloat(2, f2);
pstmt.execute();
pstmt.close();
conn.close();
}
public ResultSet getFloatValue()
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(getSQL);
this.rs = pstmt.executeQuery();
return rs;
}
}
實際測試程序:
透過setFloatValue(float , Float ) 這個方法,將傳入值1234567.45678f,7654321.65487f 分別指派給float(基本型別),Float(外覆類別)。再將這兩個變數分別存入PostgreSQL Type : real 的兩個欄位(float , float1)。傳入值在資料表上有呈現資料,表示傳入成功。
經getFloatValue()的方法,先透過executeQuery(),將SQL語法"select * from floattypes"傳入資料庫查詢,回傳ResultSet指派給rs,再將rs回傳指派給main()區塊的ResultSet,而ResultSet保存著SQL查詢完後的資料表內容,再分別透過ResultSet將值傳回給float f1,以及Float f2,最後在印出結果1234567.5,7654321.5,為了確定印出值為資料庫所傳回的,在初始值上,將float f1 = 0f , Float f2 = 0f,以作為驗證其結果。
印出的結果:
整理表格如下:
| Java Types | PostgreSQL Types | 特性 |
| float | real | 4 bytes,變精度,不精確 6 位十進制數字精度 |
| Float |
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;
}
}
實際測試程序:
透過setLongValue(long , Long ) 這個方法,將傳入值10000000000,10000000000分別指派給long(基本型別),Long(外覆類別)。再將這兩個變數分別存入PostgreSQL Type : bigint 的兩個欄位(long , long1)。傳入值在資料表上有呈現資料,表示傳入成功。
經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 |
Java Type vs PostgreSQL Type (七) 整數型別之對應 -int
int and Integer:
測試Java Types: int and Integer時,首先先建立一張資料表。
資料表語法:
CREATE TABLE inttypes
(
"int" integer,
int1 integer,
id serial NOT NULL,
CONSTRAINT inttypes_pkey PRIMARY KEY (id)
)
原始程式碼:
JavaIntegerType.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 JavaIntegerType {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private String setSQL = "Insert into inttypes (int,int1) values (?,?)";
private String getSQL = "select * from inttypes";
public static void main(String[] args)
throws IOException, ClassNotFoundException, SQLException {
JavaIntegerType jit = new JavaIntegerType();
int i1 = 0;
Integer i2 = 0;
jit.setIntegerValue(1234567, 7654321);
ResultSet rs = jit.getIntegerValue();
while (rs.next()) {
i1 = rs.getInt("int");
i2 = rs.getInt("int1");
System.out.println(i1 + " " + i2);
}
}
public void setIntegerValue(int i1, Integer i2)
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(setSQL);
pstmt.setInt(1, i1);
pstmt.setInt(2, i2);
pstmt.execute();
pstmt.close();
conn.close();
}
public ResultSet getIntegerValue()
throws IOException, ClassNotFoundException, SQLException {
PostgreConnection pc = new PostgreConnection();
this.conn = pc.getConnection();
this.pstmt = conn.prepareStatement(getSQL);
this.rs = pstmt.executeQuery();
return rs;
}
}
實際測試程序:
透過setIntegerValue(int , Integer ) 這個方法,將傳入值1234567,7654321分別指派給int(基本型別),Integer(外覆類別)。再將這兩個變數分別存入PostgreSQL Type : integer 的兩個欄位(int , int1)。傳入值在資料表上有呈現資料,表示傳入成功。
經getIntegerValue()的方法,先透過executeQuery(),將SQL語法"select * from inttypes"傳入資料庫查詢,回傳ResultSet指派給rs,再將rs回傳指派給main()區塊的ResultSet,而ResultSet保存著SQL查詢完後的資料表內容,再分別透過ResultSet將值傳回給int i1,以及Integer i2,最後在印出結果1234567,7654321,為了確定印出值為資料庫所傳回的,在初始值上,將int i1 = 0 , Integer i2 = 0,以作為驗證其結果。
印出的結果:
| Java Types | PostgreSQL Types | 特性 |
| int | integer | 4 bytes,常用的整數,-2147483648 到 +2147483647 |
| Integer |
::Planet PostgreSQL::
PostgreSQL Information Page
PostgreSQL日記(日本 石井達夫先生Blog)
PostgreSQL News
黑喵的家 - 資料庫相關
|
|
| PostgreSQL 8 DBA 專業指南中文版 |
| 書籍內容討論與更多下載區(造訪此群組)
目錄下載: PostgreSQL_8 _DBA_Index_zh_TW.pdf (更新:2007-05-18) |
全球訪客分佈圖(Google)
