• java 连接数据库测试类


    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    
    public class ConnectionTest {
        
        public static Connection getConnection() {
            // 定义连接
            Connection connection = null;
            
            try {
                // 加载驱动
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return connection;
        }
        
        public static List<HashMap<String, Object>> getMysqlData() {
            Connection connection = null;
            // 预执行加载
            PreparedStatement preparedStatement = null;
            // 结果集
            ResultSet resultSet = null;
            
            connection = getConnection();
            
            String sqlString = "select * from user";
            
            List<HashMap<String, Object>> list = new ArrayList<HashMap<String,Object>>();
            
            try {
                preparedStatement = connection.prepareStatement(sqlString);
                resultSet = preparedStatement.executeQuery();
                HashMap<String, Object> map = null;
                while (resultSet.next()) {
                    map = new HashMap<String, Object>();
                    map.put("name", resultSet.getString("user_name"));
                    list.add(map);
                }
             } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (resultSet != null) {
                        resultSet.close();
                    }
                    if (preparedStatement != null) {
                        preparedStatement.close();
                    }
                    if (connection != null) {
                        connection.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            return list;
        }
    
        public static void main(String[] args) {
            List<HashMap<String, Object>> mysqlData = getMysqlData();
            for(HashMap<String, Object> map : mysqlData) {
                System.out.println(map.get("name"));
            }
        }
    }
  • 相关阅读:
    2022321内部群每日三题清辉PMP
    PMP内部群每日错题回顾(一周目)
    2022225内部群每日三题清辉PMP
    202233内部群每日三题清辉PMP
    2022214内部群每日三题清辉PMP
    2022223内部群每日三题清辉PMP
    Qt6以上安装速度慢解决国内镜像加速
    2022.3.9内部群每日三题清辉PMP
    2022228内部群每日三题清辉PMP
    2022215内部群每日三题清辉PMP
  • 原文地址:https://www.cnblogs.com/xu-dong/p/6428180.html
Copyright © 2020-2023  润新知