c3p0 ConnectionPools設置與使用

c3p0是一個基於JNDI-bindable DataSources(使用DriverManager-based)的很容易使用的JDBC驅動函數庫。

所以在使用c3p0時,還需要一個JDBC的Driver,才能使用,而c3p0的作用只是控制Database的Connection使用,舉個簡單的例子,當Connection被DataBase Server timeout斷線後,c3p0會自動多次去重新連線,避免程式就直接丟出SQLException。

這次剛好遇到Microsoft SQL Server 2005不知為何一直丟出以下訊息,才去找到c3p0來使用的,發生的原因似乎是SQL Server本身對JDBC的Connection TimeOut斷線、或是Connection數不夠。

I/O Error: Connection reset
I/O Error: Software caused connection abort: recv failed

下面是一個範例,結果如下圖:

c3p0.png

Read More

Java use JDBC connect SQLite

SQLite is a lightweight database system that can be used without installation and can be easily embedded in the system.

SQLite is written in C language, and it can cross Linux and Windows platforms. You can use JDBC to connect to SQLite for Java access and operation.

On JDBC connection to SQLite, it is roughly divided into two ways. One is to connect the data by Pure-Java, and the other is to call the function library written in C directly from Java. The method should be faster, but in platforms where a suitable C function library cannot be found, the Pure-Java version can be used.

SQLite JDBC github , This Example support create Table、drop Table、Query table、insert、delete and Update…

https://github.com/catyku/SQLiteExample

Read More

jsp簡單留言板

這個範例有新增留言、查詢留言及留言分頁顯示等功能,後台的管理還沒有建置完成,留言者需要填入標題、顯示名稱、電話、信箱及留言內容,其中電話與電子信箱不顯示在留言列表中,電話及信箱可以為之後後台回覆寄信連絡使用。

當然這還有需多可以增加的功能,像是留言通知、隱私留言、留言刪除等,之後會再慢慢增加。

資料庫使用MySQL資料庫,使用JDBC來連接資料庫(DataSource方式),記錄檔則是使用log4j。

在新增留言部份,submit之前會先檢查是否有未填欄位,如果檢查通過則會自動建立IFrame來給這個form的target使用,新增完成後會自動回到留言列表的頁面。

留言版留言

查詢留言會對資料的標題及留言內容進行比對,有相同資料就會顯示查詢的結果,如果想進階做一個Search-Engine則可以參考建立自己的搜尋引擎

留言板查詢

Read More

Java讀取檔案匯入MySQL資料庫/取得MySQL資料存入檔案

範例是讀取一個csv檔案,內容每一行為一筆資料,每筆資料不同欄位以","做切割,使用split()方法來分離成String陣列/另一個範例則是相反的,從資料庫取select出資料,寫入檔案。

連MySQL接資料庫使用JDBC-Driver,方法可以參考Eclipse設定JDBC連接MySQL資料庫,這裡另外再提供一個使用DataSource的設定方法,使用起來也比較簡單。

JDBC連接資料庫,取得Connection的方法:

在需要連線時都會要求載入Driver Class,然後要求連線。

Read More