1.首先需要一个配置文件:jdbc.properties

我用的是MySQL数据库,用户名和密码均为root

driver=com.mysql.jdbc.Driver      
url=jdbc:mysql://localhost:3306/lssrc      
user=root     
password=root  

2.下面才是要封装的类。这里我命名为DBUtils

package cloud.vimo.db;      
     
import java.sql.*;      
import java.util.*;      
     
public class DBUtils {      
    private static String driver, url, user, password;      
     
    static {      
        ResourceBundle rb = ResourceBundle.getBundle("jdbc");// 通过配置文件名字获取配置文件中内容     
        driver = rb.getString("driver");      
        url = rb.getString("url");      
        user = rb.getString("user");      
        password = rb.getString("password");    
     
    }      
     
    private static Connection getConnection() throws SQLException {// 获取Connection     
        Connection conn = null;      
        try {      
            Class.forName(driver);      
            conn = DriverManager.getConnection(url, user, password);      
        } catch (ClassNotFoundException e) {      
            e.printStackTrace();      
        }      
        return conn;      
    }      
     
    public static Object[][] queryForArray(String sql, Object[] args) {// 做查询用。返回的是二维表     
        Connection conn = null;      
        PreparedStatement stmt = null;      
        ResultSet rs = null;      
        Object[][] result = null;      
        try {      
            conn = getConnection();      
            stmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);      
            if (args != null) {      
                for (int i = 0; i < args.length; i++) {      
                    stmt.setObject(i + 1, args[i]);      
                }      
            }      
            rs = stmt.executeQuery();      
            rs.last();      
            int rows = rs.getRow();      
            rs.beforeFirst();      
            ResultSetMetaData rsmd = rs.getMetaData();      
            int cols = rsmd.getColumnCount();      
            result = new Object[rows][cols];      
            int currentRow = 0;      
            while (rs.next()) {      
                for (int i = 0; i < cols; i++) {      
                    result[currentRow][i] = rs.getObject(i + 1);      
                }      
                currentRow++;      
            }      
        } catch (SQLException e) {      
            e.printStackTrace();      
        } finally {      
            try {      
                if (rs != null)      
                    rs.close();      
                if (stmt != null)      
                    stmt.close();      
                if (conn != null)      
                    conn.close();      
            } catch (SQLException e) {      
                e.printStackTrace();      
            }      
        }      
        return result;      
    }      
     
    public static List queryForList(String sql, Object[] args) {// 做查询用。返回List,List中用到了Map     
        Connection conn = null;      
        PreparedStatement stmt = null;      
        ResultSet rs = null;      
        List result = new ArrayList();      
        try {      
            conn = getConnection();      
            stmt = conn.prepareStatement(sql);      
            if (args != null) {      
                for (int i = 0; i < args.length; i++) {      
                    stmt.setObject(i + 1, args[i]);      
                }      
            }      
            rs = stmt.executeQuery();      
            ResultSetMetaData rsmd = rs.getMetaData();      
            int cols = rsmd.getColumnCount();      
            String[] colNames = new String[cols];      
            for (int i = 0; i < cols; i++) {      
                colNames[i] = rsmd.getColumnName(i + 1);      
            }      
            while (rs.next()) {      
                Map row = new HashMap();      
                for (int i = 0; i < cols; i++) {      
                    row.put(colNames[i], rs.getObject(i + 1));      
                }      
                result.add(row);      
            }      
        } catch (SQLException e) {      
            e.printStackTrace();      
        } finally {      
            try {      
                if (rs != null)      
                    rs.close();      
                if (stmt != null)      
                    stmt.close();      
                if (conn != null)      
                    conn.close();      
            } catch (SQLException e) {      
                e.printStackTrace();      
            }      
        }      
        return result;      
    }      
     
    public static Map queryForMap(String sql, Object[] args) throws Exception {// 做查询用。返回的是Map     
        List result = queryForList(sql, args);      
        Map row = null;      
        if (result.size() == 1) {      
            row = (Map) result.get(0);      
        } else {      
            throw new Exception("期望得到一行数据,但实际得到" + result.size() + "行");      
        }      
        return row;      
    }      
     
    public static boolean update(String sql, Object[] args) {// 修改,添加,删除都可以用到。返回boolean     
        Connection conn = null;      
        PreparedStatement stmt = null;      
        boolean result = true;      
        try {      
            conn = getConnection();      
            stmt = conn.prepareStatement(sql);      
            if (args != null) {      
                for (int i = 0; i < args.length; i++) {      
                    stmt.setObject(i + 1, args[i]);      
                }      
            }      
            stmt.executeUpdate();      
        } catch (SQLException e) {      
            e.printStackTrace();      
            result = false;      
        } finally {      
            try {      
                if (stmt != null)      
                    stmt.close();      
                if (conn != null)      
                    conn.close();      
            } catch (SQLException e) {      
                e.printStackTrace();      
            }      
        }      
        return result;      
    }      
}  

个人觉得挺实用的,所以将其分享出来。。。共同学习!!!


感谢大家的阅读, 如有疑问可以加我微信