封装BaseDao

package tool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

public class BaseDao {
    static private String user = "root";
    static private String pwd = "root";
    static String classname = "com.mysql.jdbc.Driver";
    static String url = "jdbc:mysql://localhost:3306/exam?characterEncoding=utf8";
    static {
        try {
            Class.forName(classname);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getcon() throws SQLException {
        return DriverManager.getConnection(url, user, pwd);
    }

    public static void close(Connection con, Statement pre, ResultSet re) {
        try {
            if (con != null)
                con.close();
            if (pre != null)
                pre.close();
            if (re != null)
                re.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public int excuteUpdate(String sql, Object... str) {
        Connection con = null;
        PreparedStatement pre = null;
        try {
            con = getcon();
            pre = con.prepareStatement(sql);
            if (str != null) {
                for (int i = 0; i < str.length; i++) {
                    pre.setObject(i + 1, str[i]);
                }
            }
            return pre.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(con, pre, null);
        }
        return -1;
    }

    public static int excuteInsert(String sql, Object... str)
            throws SQLException {
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet re = null;
        try {
            con = getcon();
            pre = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            if (str != null) {
                for (int i = 0; i < str.length; i++) {
                    pre.setObject(i + 1, str[i]);
                }
            }
            pre.executeUpdate();
            re = pre.getGeneratedKeys();
            if (re.next()) {
                return re.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(con, pre, re);
        }
        return -1;
    }

    public List<Map> excuteQuery(String sql, Object... str) {
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet re = null;
        try {
            con = getcon();
            pre = con.prepareStatement(sql);

            if (str != null) {
                for (int i = 0; i < str.length; i++) {
                    pre.setObject(i + 1, str[i]);
                }
            }
//            pre.executeUpdate();
            re = pre.executeQuery();
            ResultSetMetaData mate = re.getMetaData();
            int count = mate.getColumnCount();

            List list = new ArrayList();
            while (re.next()) {
                Map map = new HashMap();
                for (int i = 1; i <= count; i++) {
                    String lie = mate.getColumnName(i);
                    Object obj = re.getObject(i);
                    map.put(lie.toLowerCase(), obj);
                }
                list.add(map);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(con, pre, re);
        }
        return null;
    }
}

扫描二维码关注公众号,回复: 3836414 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_32295383/article/details/82154077