JDBC

学习存储过程,顺便复习JDBC知识

 随风行云的博客:JDBC知识点 https://www.cnblogs.com/progor/p/9096463.html

使用statement查询

public static void main(String[] args)throws Exception {
        // ?加上后缀为了对应上海时区,防止时间误差
        String URL = "jdbc:mysql://localhost:3306/xxxx?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai"; // 路径
        String USER = "root"; // 用户名
        String PASSWORD = "123456"; // 密码
        Class.forName("com.mysql.cj.jdbc.Driver"); // 驱动,不同版本的mysql有不同的驱动方式。
        Connection conn = DriverManager.getConnection(URL,USER,PASSWORD); // 获取连接
        Statement sm = conn.createStatement();
        String sql = "select A.*,B.* from SYS_USER A LEFT JOIN USER_BALANCE B ON A.CARDID = B.POSSESSOR_CARDID";
        ResultSet rs = sm.executeQuery(sql);
        while(rs.next()){
      // rs.next()用于判断是否还有下一条数据
      // BigDecimal适用于小数精确加减用的,
            BigDecimal bigDecimal  = new BigDecimal(0.00);
            if(rs.getBigDecimal(9) != null || rs.getBigDecimal(10) != null){
        //rs.getBigDecimal(index) index是对应数据库的列,列的类型是vacher等字符就需要用getxxxx的方法对应
                bigDecimal = bigDecimal.add(rs.getBigDecimal(9));
                bigDecimal = bigDecimal.add(rs.getBigDecimal(10));
            }
            System.out.println(
                   " id号" + rs.getInt(1) +
                   " 姓名" + rs.getString(3) +
                   " 性别" + rs.getString(4) +
                   " 电话号码" + rs.getString(5) +
                   " 创建时间" + rs.getDate(6) + rs.getTime(6) +
                   " 余额" + bigDecimal.toString()
            );
        }
    }

存储过程call

 

posted @ 2022-03-21 21:03  LoveDonkey  阅读(28)  评论(0编辑  收藏  举报