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

閱讀全文

Csharp使用ADO.NET操作SQLite

SQLite是一個簡易的資料庫系統,開放原始碼,可以直接把SQLite綁在程式裡使用,FireFox及Android等軟體也都有內建SQLite。SQLite不需要安裝,看起來就只是一個檔案而已,也可以使用memory模式,讓它存在記憶體中而不需要建立一個檔案存放。

SQLite支援的SQL指令:http://www.sqlite.org/lang_corefunc.html

C#要連接SQLite可以使用open source的System.Data.SQLite,它是一個基於ADO.Net所做與SQLite的溝通介面,目前支援到.net framework 3.5。可於sourceforge下載其dll來使用,就可以了。

下方的範例是介紹如何在Csharp下使用ADO.NET連接與操作SQLite,包含了自動產生SQLite檔案與DataGridView使用class binding datasource方法。

一、建立一個專案,並先將其儲存起來

(需要先建置專案才能使用專案裡的class當做datasource)

01.jpg
02.jpg

二、改變專案使用的Framework

閱讀全文

Java use JDBC connect SQLite

補充:上面每個方法都改成 try-with-resources,Statement/PreparedStatement/ResultSet/Connection 會在區塊結束時自動關閉,就算中途丟例外也不會漏。另外在 JDK 24 之後執行 sqlite-jdbc 會看到 restricted method 警告,加上 --enable-native-access=ALL-UNNAMED 即可消除(不影響執行)。

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

閱讀全文

csharp筆記

一

在DataSource元件要使用Transaction,可以增加一個參考dll(system.transactions.dll),此dll只支援.Net 2.0以上,然後使用TransactionScope來在指定區域內做Transaction

參考資料網址:http://msdn.microsoft.com/zh-tw/library/system.transactions.transactionscope(VS.80).aspx

using System.Transactions;


using (TransactionScope scope = new TransactionScope())
{
    SqlDataSource2.SelectParameters.Clear();

    SqlDataSource2.DeleteCommand = "delete from table_name ";

                   
    SqlDataSource2.Update();
    scope.Complete();
}

閱讀全文

Csharp WebRequest實作POST來取得網頁內容(及檔案下載)

CSharp要取得網頁內容可以利用內建的WebBrowser來取用IE Container進行連線與資料的取得,WebBrowser實作了很多內容,像是Javascript、HTML parse等等,功能十分強大。

這個範例不使用WebBrowser,而是使用HttpWebRequest來取得遠端的html內容。

Web Server說穿了也就是一個Socket Server,只是Browser在與其溝通時使用了規定的規則而已,以下範例會模擬此規則傳送header來告知Web Server,假裝自己是Browser,而此也會利用cookieContainer來記錄傳輸過程的Session情況,如此登入後的Session就可以在往後的需求連線中被使用。

程式說明:

Server端

閱讀全文