zoukankan      html  css  js  c++  java
  • javaweb 测试数据库连接是否正常

    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/数据库名称", "用户名", "密码");
            } 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();
    
            //准备的sql
            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("nickName"));   //获取字段名称nickName的值,
                    list.add(map);//将nickName的值添加到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"));  //通过key获取Value
            }
        }
    }
  • 相关阅读:
    经典数字信号处理图书的个人评述
    信号与系统
    FFT结果的物理意义
    如何选导师,如何做好研究生,如何做好同行评审
    Google学术指数2015版
    2015影响因子Excel版
    VHDL MOD和REM(转)
    面向对象的三大特征
    【数据结构与算法】内部排序之一:插入排序和希尔排序的N中实现(不断优化,附完整源码)
    JAVA字符串String、StringBuffer、StringBuilder、基本数据类型包装
  • 原文地址:https://www.cnblogs.com/jamal/p/10057357.html
Copyright © 2011-2022 走看看