使用JDBC處理Postgresql資料庫
不得不吐槽的是Java的配置真心的繁瑣,如果沒有有IDE的話,手動處理這些內容真心會累死人。
今天簡單的來說明下,如何使用JDBC來連線資料庫,只能說是1個拋磚引玉的過程。
這裡,我們選擇的Java版本為8,使用的資料庫是Postgresql。首先我們使用maven下載對應的依賴:
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.5</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.49</version> </dependency> ...
接著我們編寫程式碼,引入如下的幾個包:
import java.util.*; import java.io.*; import java.sql.*; import com.alibaba.fastjson.*;
然後定義1個公共類:
public class hello { public static void main(String[] args) { Properties properties = new Properties(); try { Class.forName("org.postgresql.Driver"); InputStream stream = new FileInputStream("db.properties"); properties.load(stream); String url = properties.getProperty("url"); String user = properties.getProperty("user"); String pwd = properties.getProperty("pwd"); stream.close(); Connection conn = DriverManager.getConnection(url, user, pwd); Statement smt = conn.createStatement(); smt.setFetchSize(50); smt.setMaxRows(10); ResultSet rs = smt.executeQuery("select * from fund"); ResultSetMetaData metadata = rs.getMetaData(); JSONArray arr = new JSONArray(); while(rs.next()){ HashMap<String, Object> map = new HashMap<>(); int count = metadata.getColumnCount(); for(int i=0;i<count;i++){ String label = metadata.getColumnLabel(i+1); Object val = rs.getObject(label); map.put(label, val); } arr.add(map); } rs.close(); String response = JSON.toJSONString(arr); System.out.println(response); } catch (Exception e){ e.printStackTrace(); } } }
在這個類,我們可以將程式劃分為如下3個部分:
- 讀取配置檔案
- 進行資料查詢
- 進行資料序列化
對於配置檔案的讀取,我們選擇使用FileInputStream來讀取1個檔案,這個檔案中的內容類似如下:
url=jdbc:postgresql://localhost:5432/dog user=dog pwd=123456
這裡我們通過Class.forName
匯入對應包中的類,然後通過Properties例項的load方法將之前檔案輸入流傳遞來進來,從而得到1個鍵值對的物件。之後我們就可以通過Properties例項的getProperty
進行鍵值的獲取。
對於資料的查詢,我們使用了setMaxRows
方法設定其最大查詢的行數,類似於如下的操作:
select * from fund limit 10
而對於資料的序列化,我們通過ResultSet例項的getMetaData
方法得到1個ResultSetMetaData例項,之後我們呼叫其getColumnCount
方法得到列的數量。之後,我們進行迭代,通過其getColumnLabel
得到列的標籤名稱,而對應的值通過ResultSet例項的getObject方法來得到。
在這裡,我們使用阿里巴巴公司fastjson來進行JSON的序列化處理,我們將對應的值新增到JSONArray中,最後呼叫JSON.toJSONString
將對應的物件序列化為JSON字串。其結果為:
[{"fund_type":8,"fund_id":"000001","fund_name":"華夏成長","fund_code":"HXCZ"},{"fund_type":8,"fund_id":"000002","fund_name":"華夏成長(後端)","fund_code":"HXCZ"},{"fund_type":6,"fund_id":"000003","fund_name":"中海可轉債A","fund_code":"ZHKZZA"},{"fund_type":6,"fund_id":"000004","fund_name":"中海可轉債C","fund_code":"ZHKZZC"},{"fund_type":7,"fund_id":"000005","fund_name":"嘉實增強信用定期債券","fund_code":"JSZQXYDQZQ"},{"fund_type":6,"fund_id":"000007","fund_name":"鵬華國企債債券","fund_code":"PHGQZZQ"},{"fund_type":3,"fund_id":"000008","fund_name":"嘉實中證500ETF聯接","fund_code":"JSZZ500ETFLJ"},{"fund_type":10,"fund_id":"000009","fund_name":"易方達天天理財貨幣A","fund_code":"YFDTTLCHBA"},{"fund_type":10,"fund_id":"000010","fund_name":"易方達天天理財貨幣B","fund_code":"YFDTTLCHBB"},{"fund_type":8,"fund_id":"000011","fund_name":"華夏大盤精選","fund_code":"HXDPJX"}]
當然,在這個過程中我們甚至還可以進行一些鍵值的獲取及剔除,再返回給前端。