1. 程式人生 > >postgresql從入門到菜鳥(五)JDBC連線postgresql資料庫

postgresql從入門到菜鳥(五)JDBC連線postgresql資料庫

之前都是通過psql命令進行資料庫的操作,從這一期開始準備寫一些如何通過LIBPQ,JDBC,ODBC等方式來postgresql並進行相關的操作,這一期準備先說說JDBC。

這裡分為三個模組來講:
一.獲取連線
二.執行select語句
三.執行insert,delete,update語句

第一部分:獲取連線
要通過jdbc連線postgresql首先需要載入JDBC驅動,語法如下

Class.forName("org.postgresql.Driver").newInstance();

然後通過DriverManager獲取連線

connection = DriverManager.getConnection("jdbc
:postgresql://xxx.xxx.xxx.xxxx:5432/testdb", "postgres", "postgres");

注意這裡的getConnection函式的使用方式,getConnection函式一共提供了三種實現

1.通過配置檔案的形式連線資料庫
getConnection(String paramString, Properties paramProperties)

2.通過關鍵字形式連線資料
getConnection(String paramString1, String paramString2, String paramString3)

3.通過連線字串形式連線資料庫
getConnection(String paramString)

比較常用的是第一種,使用配置檔案的方式進行連線,這裡為了方便,就採用第二種了。
注意:載入驅動和獲取連線操作均需要進行異常捕獲。
到這裡,我們就已經能獲取一個connection物件了。

第二部分:執行select語句
在獲取到connection物件後,我們就可利用connection物件進行對資料庫的操作了。
要進行查詢操作,需要兩個物件Statement和ResultSet。
JDBC一共提供了三種Statement,PreparedStatement,Statement和CallableStatement,個人對這三種方式的理解

Statement:適合只執行一次或極少執行的sql文。

PreparedStatement:適合執行需要傳參並且會多次執行的sql文,並且一定程度上防止了sql注入。

CallableStatement:適合執行儲存過程。

這裡採用比較常用的PreparedStatement進行舉例:

    public ResultSet query(Connection conn, String sql) {
        PreparedStatement pStatement = null;
        ResultSet rs = null;
        try {
            pStatement = conn.prepareStatement(sql);//準備PreparedStatement 
            rs = pStatement.executeQuery();//執行查詢
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return rs;
    }

通過以上方式,我們可以獲取一個ResultSet物件,即結果集,並且預設遊標指在該結果集的第一行,可以通過rs.next()方法獲取下一條資料,rs的解析需要結合實際表結構,下面貼出一段以供參考

while(rs.next()){                       //當該結果集的下一條資料不為空,讀取下一條資料
        int id = rs.getInt("id");       //讀取當前行名為id的列的資料
        String name = rs.getString("name");
        String gender = rs.getString("gender");
        System.out.println("id:"+id+" 姓名:"+name+" 性別:"+gender);
    }

第三部分:執行insert,delete,update語句
insert,delete,update的執行和select操作類似,但是不同的地方在於,insert,delete,update操作並不會返回ResultSet,而是返回受影響的行數,具體程式碼如下

public boolean queryUpdate(Connection conn, String sql) {
        PreparedStatement pStatement = null; //建立PreparedStatement 
        int rs = 0;  //用於接收返回的受影響行數
        try {
            pStatement = conn.prepareStatement(sql);
            rs = pStatement.executeUpdate();//執行sql操作,獲取受影響行數
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs > 0) {
            return true; //如果受影響行數超過1行,則認為操作成功
        }
        return false; //預設返回失敗,只有受影響函式大於0時才返回true
        /*實際環境中並不一定必須要受影響行數大於0才算執行成功,需根據實際情況判斷*,這裡的方法只是例子*/
    }

以上程式碼通過函式返回值判斷insert,delete,update操作是否成功。

完整程式碼等上傳後會貼出。