• 数据库操作类util


    package util;
    
    import java.sql.Connection;
    import java.sql.Driver;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class DateExecute {
    
        public static Connection getConnection(String name, String password)
                throws InstantiationException, IllegalAccessException,
                ClassNotFoundException, SQLException {
            Connection con;
            String driverName = "com.mysql.jdbc.Driver";
            Driver d = (Driver) Class.forName(driverName).newInstance();
            con = DriverManager.getConnection("jdbc:mysql://localhost:3307/nona",
                    name, password);
            return con;
        }
    
        public static List<Map<String, Object>> getDateList(String sql)
                throws InstantiationException, IllegalAccessException,
                ClassNotFoundException, SQLException {
            Connection conn = getConnection("root", "root");
            PreparedStatement stmt;
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            try {
                stmt = conn.prepareStatement(sql);
                ResultSet rs = stmt.executeQuery(sql);
                list = convertList(rs);
            } catch (SQLException e) {
                System.out.println("数据库连接失败");
                e.printStackTrace();
            }
            return list;
        }
    
        private static List convertList(ResultSet rs)
                throws SQLException {
    
            List list = new ArrayList();
            ResultSetMetaData md = rs.getMetaData();
            int columnCount = md.getColumnCount(); // Map rowData;
            while (rs.next()) { // rowData = new HashMap(columnCount);
    
                Map<String, Object> rowData = new HashMap<String, Object>();
    
                for (int i = 1; i <= columnCount; i++) {
    
                    rowData.put(md.getColumnName(i), rs.getObject(i));
                }
                list.add(rowData);
            }
            return list;
        }
    
        public static int executeUpdate(String sql)
                throws InstantiationException, IllegalAccessException,
                ClassNotFoundException, SQLException {
            Connection conn = getConnection("root", "root");
            PreparedStatement stmt;
            int success = 0 ;
            try {
                stmt = conn.prepareStatement(sql);
                success = stmt.executeUpdate(sql);
            } catch (SQLException e) {
                System.out.println("数据库连接失败");
                e.printStackTrace();
            }
            return success;
        }
    }
  • 相关阅读:
    IIS大量出现Connections_Refused的错误提示
    SQL分类汇总统计聚合查询
    PHP_保留两位小数并且四舍五入_保留两位小数并且不四舍五入
    忘记Ucenter创始人密码的最快速解决方法
    NetBeans无法使用编码GBK安全地打开该文件
    PHP连续输出字母AZ
    C# 和 Linux 时间戳转换
    php中使用header在下载时乱码问题解决
    PHP获取当前时间、时间戳的各种格式写法汇总[日期时间]
    一些PHP性能优化
  • 原文地址:https://www.cnblogs.com/mynona/p/3574318.html
Copyright © 2020-2023  润新知