JDBC简单CRUD

package com.gcyh.common.controller;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ScalpMigration {

    private static Connection con = null;

    private static Map<Long, Integer> map = new HashMap<>();

    static {
        try {
            String user = "root";
            String password = "123456_gcyh";
            String url = "jdbc:mysql://172.18.228.110:3306/KSHongBao?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8";
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, user, password);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = con.createStatement();

            System.out.println("==========插入完成==========");
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (con != null) {
                con.close();
            }
        }
    }

    private static Boolean insertDetail(BigDecimal coin ,String userId) throws Exception {
        Statement stmt = null;
        ResultSet rs = null;

        // 展示表
        int type = 50;
        int IsIncome = 1;
        String crazyId = "4";
//        java.sql.Date time = new java.sql.Date(date.getTime());

        try {
            stmt = con.createStatement();

             // INSERT INTO wallet_detail (user_account_uuid,funds_change_uuid,coin,is_income,funds_type,create_date) VALUES (10,174,50,1,50,NOW())
            Boolean execute = stmt.execute("INSERT INTO `wallet_detail` (user_account_uuid,funds_change_uuid,coin,is_income,funds_type,create_date)" +
                                  " VALUES ( " + userId + "," + crazyId + "," + coin + "," + IsIncome + "," + type + ", NOW() )");
            return  execute ;
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
        }
    }

    private static Boolean updateCoin(BigDecimal coin ,String userId) throws Exception {
        Statement stmt = null;
        try {
            stmt = con.createStatement();
            int  execute = stmt.executeUpdate(" update ob_userinfo_wallet  set walletPilesNumber = walletPilesNumber + " + coin + "   where " +
                    "userInfo_id = " + userId);
            return  execute > 0;
        } finally {
            if (stmt != null) {
                stmt.close();
            }
        }
    }

  private static List<CrazyUser> getList() throws Exception {
        Statement stmt = null;
        ResultSet rs = null;
        List<CrazyUser> list = new ArrayList<>();
        try {
            stmt = con.createStatement();
            rs = stmt.executeQuery(" SELECT * FROM crazy_user ORDER BY id DESC LIMIT 100");
            while (rs.next()) {
                CrazyUser crazyUser = new CrazyUser();
                crazyUser.setId(rs.getLong("id"));
                crazyUser.setCrazyId(rs.getLong("crazy_id"));
                crazyUser.setUserAccountUuid(rs.getString("user_account_uuid"));
                crazyUser.setIsParticipate(StaticConstant.STATUS_ONE);
                list.add(crazyUser);
            }
            return list;
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
        }
    }

    private static long getParentId(long userInfoId) throws Exception {
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT parentUserInfo_id FROM KSHongBao.ob_userinfo_to_userinfo u where userInfo_id = " + userInfoId);
            if (rs.next()) {
                return rs.getLong("parentUserInfo_id");
            }
            return 0;
        }
        finally {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
        }
    }

}
https://www.cnblogs.com/wuyuegb2312/p/3872607.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值