zoukankan      html  css  js  c++  java
  • mysql jdbc操作

    import java.sql.*;
    import java.util.Map;
    
    public class Mysql {
        public static void main(String[] args) {
            Connection conn = getConn();
            PreparedStatement pstmt;
            try {
                pstmt = conn.prepareStatement("select * from lw_area where area_id like '35%' and area_level <=3");
                ResultSet rs = pstmt.executeQuery();
                //Map<String, Map<String, Object>>  
                while (rs.next()) {
                    System.out.println(rs.getLong("area_id"));
                    System.out.print(rs.getLong("area_pid"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        private static Connection getConn() {
            String driver = "com.mysql.cj.jdbc.Driver";
            String url = "jdbc:mysql://localhost:3306/linewell_assets_mgt_es?serverTimezone=UTC";
            String username = "root";
            String password = "123456";
            Connection conn = null;
            try {
                Class.forName(driver); //classLoader,加载对应驱动
                conn = DriverManager.getConnection(url, username, password);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return conn;
        }
    }

    把结果转化成map形式:

    ResultSet rs = stmt.executeQuery(sql);
    ResultSetMetaData md = rs.getMetaData();
    int columnCount = md.getColumnCount();
    while (rs.next()) {
    Map<String, Object> rowData = new HashMap<String, Object>();
    for (int i = 1; i <= columnCount; i++) {
    rowData.put(md.getColumnName(i), rs.getObject(i));
    }
    }
  • 相关阅读:
    Spring Boot中的JSON技术
    Spring Boot中编写单元测试
    如何保证事务方法的幂等
    定时重试线程池
    多线程导致事务失效-记一次性能优化
    自己实现一个简单的数据库事务
    服务器错误码国际化
    spring自定义自动配置注解
    springboot中如何启动tomcat
    用grep来查询日志
  • 原文地址:https://www.cnblogs.com/chenmz1995/p/10261501.html
Copyright © 2011-2022 走看看