•   欢迎来到21NN网.
  •   请记住本站网址www.21nn.cn

数据库jdbc封装【MySQL教程】,jdbc,数据库,封装

摘要: 经由几天的勤奋终究搞好了,这个类能够不必管数据库字段,不必写dao类,直接map增添,增添删除和修正,jdbc链接,分享给人人,用的话记得加上衔接池,尊敬原创,转载请说明packagejdb...


经由几天的勤奋终究搞好了,这个类能够不必管数据库字段,不必写dao类,直接map增添,增添删除和修正,jdbc链接,分享给人人,用的话记得加上衔接池,尊敬原创,转载请说明

package jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/**
 * 操纵数据库东西类
 *
 *
 */public class domain {

    /**
     * 衔接数据
     *
     * @return conn
     */
    public static Connection getConnection(String url, String username, String password) {
        Connection conn = null;        
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }        return conn;
    }    /**
     * 封闭衔接对象
     *
     * @param conn
     *            衔接对象
     * @param pstmt
     *            预编译对象
     * @param rs
     *            效果集
     */
    public static void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) {        
    try {            
    if (rs != null) {
                rs.close();
            }            
            if (pstmt != null) {
                pstmt.close();
            }            
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }    /**
     * 增编削操纵
     *
     * @param sql
     *            SQL敕令
     * @param param
     *            参数
     * @return
     */
    public static int executUpdate(Connection conn, String sql, Object[] param) {        
    int result = 0;
        PreparedStatement pstmt = null;        
        try {
            System.out.println(sql);
            pstmt = conn.prepareStatement(sql);            
            if (param != null) {                
            for (int i = 0; i < param.length; i++) {
                    pstmt.setObject(i + 1, param[i]);
                }
            }
            result = pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, pstmt, null);
        }        return result;
    }    /**
     * 查询
     *
     * @return int
     * @date 2015-7-25 上午11:10:06
     */
    public static ResultSet executQuery(Connection conn, String sql, String[] param) {
        PreparedStatement pstmt = null;
        ResultSet result = null;        
        try {
            pstmt = conn.prepareStatement(sql);            
            if (param != null) {                
            for (int i = 0; i < param.length; i++) {
                    pstmt.setString(i + 1, param[i]);
                }
            }
            result = pstmt.executeQuery();
        } catch (Exception e) {
            e.printStackTrace();
        }        return result;
    }    /**
     * 简朴增编削SQL语句实行,庞杂sql实行executUpdate要领
     * 字符串范例须要加单引号
     * @param url
     *            数据库
     * @param username
     *            数据库用户名
     * @param password
     *            数据库暗码
     * @param map
     *            参数列,值
     * @param tableName
     *            表名
     * @param typeSql
     *            SQL范例 insert,dell,update
     * @param oldLine
     *            删除时为要删除的列名,更新的时刻为where前的前提列名(仅当且运用dell和update时填写,insert时不作用)
     * @param newLine
     *            更新时where候的前提列名(仅当且运用update时填写,dell,insert时不作用)
     * @param oldCondition
     *            删除时为要删除的前提,更新的时刻为where前的前提(仅当且运用dell和update时填写,insert时不作用)
     * @param newCondition
     *            更新的时刻为where后的前提(仅当且运用update时填写,dell,insert时不作用)
     */

    public static void sql(String url, String username, String password, Map<String, Object> map, String tableName,
            String typeSql, String oldLine, String oldCondition, String newLine, String newCondition) {
        String sql = "";
        Connection conn = getConnection(url, username, password);
        Object[] valueArray = null;        if (typeSql.equals("insert")) {
            List<Object> key = new ArrayList<Object>();
            List<Object> value = new ArrayList<Object>();
            StringBuffer sb = new StringBuffer();
            StringBuffer wen = new StringBuffer();            for (Object string : map.keySet()) {
                key.add(string);
                value.add(map.get(string));
            }

            Object[] keyArray = key.toArray();
            valueArray = value.toArray();            for (int i = 0; i < keyArray.length; i++) {
                sb.append(keyArray[i] + ",");
                wen.append("?,");
            }
            String string = sb.toString();
            string = string.substring(0, string.length() - 1);
            String wenStr = wen.toString();
            wenStr = wenStr.substring(0, wenStr.length() - 1);

            sql = "INSERT INTO " + tableName + "(" + string + ") VALUES (" + wenStr + ")";
        } else if (typeSql.equals("dell")) {
            sql = "DELETE FROM " + tableName + " WHERE " + oldLine + " = " + oldCondition;
        } else if (typeSql.equals("update")) {
            sql = "UPDATE " + tableName + " SET " + oldLine + "= " + oldCondition + " WHERE " + newLine + " = "
                    + newCondition;
        }        int executUpdate = executUpdate(conn, sql + ";", valueArray);
        System.out.println(executUpdate);
    }//测试
    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("appid", "lisi");         for (int i = 0; i < 100; i++) {
         System.out.println(i);
         map.put("id",i);         // 增添
          sql("test", "root", "", map, "user", "insert", "", "", "", "");          // 删除
          sql("test", "root", "", map, "user", "dell", "id", i+"","", "");          // 修正//        
          sql("test", "root", "", map, "user", "update", "appid", "'zhang'", "id", "0");

         }        // map.put("password", "123");

    }
}

以上就是数据库jdbc封装的细致内容,更多请关注ki4网别的相干文章!

分享到:

发表评论

评论列表

还没有评论,快来说点什么吧~

公众号二维码

微信公众号