使用JDBC訪問MySql的一個公共方法的解決方案
本來項目都是用到例如Hibernate這些工具的,可是因為現(xiàn)在項目要求現(xiàn)在又丫的回到基于JDK的解決方案了。
這個方法很簡單,但是對于數(shù)據(jù)的連接使用連接池,連接池也是直接初始化的。
- package com;
- import java.sql.*;
- import java.util.*;
- import org.apache.commons.dbcp.BasicDataSource;
- /**
- * @說明 數(shù)據(jù)庫連接管理
- * @author cuisuqiang
- */
- public class ConnectionManager {
- /**
- * @說明 執(zhí)行一條SQL
- */
- @SuppressWarnings("unchecked")
- public static List<Object[]> excuteQuery(String sql) {
- Connection conn = null;
- PreparedStatement psta = null;
- ResultSet resultSet = null;
- List<Object[]> relist = new ArrayList<Object[]>(); // 總數(shù)據(jù)
- Object[] objects = null; // 每行數(shù)據(jù)
- try {
- conn = ConnectionManager.getConn(); // 得到鏈接
- if(null != conn){
- psta = conn.prepareStatement(sql);
- resultSet = psta.executeQuery(); // 執(zhí)行查詢,返回結(jié)果接集合
- int count = resultSet.getMetaData().getColumnCount(); // 一共有多少列數(shù)據(jù)
- // 循環(huán)行
- while (resultSet.next()) {
- objects = new Object[count];
- // 數(shù)據(jù)集索引從 1 開始,而數(shù)組存放時是從 0 開始
- for (int i = 1; i <= count; i++) {
- objects[i - 1] = resultSet.getObject(i);
- }
- relist.add(objects);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- relist = null;
- } finally {
- try {
- if(null != resultSet)
- resultSet.close();
- if(null != psta)
- psta.close();
- if(null != conn)
- conn.close();
- } catch (Exception e2) {
- }
- }
- return relist;
- }
- private static org.apache.commons.dbcp.BasicDataSource ds = null;
- static {
- ds = new BasicDataSource(); // 組建數(shù)據(jù)源對象
- int initialSize = 1; // 連接池啟動時的初始值
- int maxActive = 10; // 連接池的最大值
- int maxIdle = 1; // 最大空閑值
- int minIdle = 1; // 最小空閑值
- ds.setDriverClassName("com.mysql.jdbc.Driver");
- ds.setUrl("jdbc:mysql://192.168.154.128:3306/t2?useUnicode=true&characterEncoding=gbk");
- ds.setUsername("root");
- ds.setPassword("123456");
- ds.setInitialSize(initialSize);
- ds.setMaxActive(maxActive);
- ds.setMaxIdle(maxIdle);
- ds.setMinIdle(minIdle);
- }
- /**
- * 從數(shù)據(jù)源中取得數(shù)據(jù)庫連接
- */
- public static Connection getConn() {
- try {
- return ds.getConnection();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
我使用的是BasicDataSource,你可能需要兩個Jar包,commons-pool.jar,commons-dbcp.jar,連接mysql的話需要mysql-connector-java-3.1.13-bin.jar。
寫一個mian方法直接測試:
- package com;
- import java.util.List;
- public class T {
- @SuppressWarnings("unchecked")
- public static void main(String[] args) {
- try {
- List<Object[]> list = ConnectionManager.excuteQuery("select * from t");
- for (int i = 0; i < list.size(); i++) {
- Object[] os = list.get(i);
- for(Object o : os){
- if (o instanceof String) {
- String s = (String) o;
- String newStr = new String(s.getBytes("ISO-8859-1"),"GBK");
- System.out.print("字符串:" + newStr + "\t\t");
- }else if(o instanceof Long){
- Long s = (Long) o;
- System.out.print("浮點值:" + s + "\t\t");
- }else if(o instanceof Integer){
- Integer s = (Integer) o;
- System.out.print("整形值:" + s + "\t\t");
- }else{
- System.out.print("未知型:" + o + "\t\t");
- }
- }
- System.out.println();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
我返回的是一個List集合,里面包含了一個Object數(shù)組。調(diào)用者在收到這個返回集合后可以根據(jù)實際情況進行解析,公共方法只是執(zhí)行SQL,然后得到數(shù)據(jù)連接進行數(shù)據(jù)訪問。
原文鏈接:http://cuisuqiang.iteye.com/blog/1458557
【編輯推薦】