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<String> getList() throws Exception {
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = con.createStatement();
            rs = stmt.executeQuery(" SELECT * FROM crazy_user ORDER BY id DESC LIMIT 100");
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
        }
        return null;
    }

    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();
            }
        }
    }

}

猜你喜欢

转载自blog.csdn.net/qq_39723363/article/details/84754660