2021-03-03

package com.cyber.jdbc;

import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

/**
 * Author liumz
 * Date 2021/3/2 21:20
 * DESC
 */
public class Insert2Oracle {
    private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
    private static String userName = "scott";
    private static String passWord = "tiger";
    private static String driverName = "oracle.jdbc.OracleDriver";

    static{
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static int execUpdate(String sql, Object[] params){
        int rlines = 0;
        Connection conn = null;
        PreparedStatement ps = null;

        try {
            conn = DriverManager.getConnection(url, userName, passWord);
            conn.setAutoCommit(false);

            ps = conn.prepareStatement(sql);
            if (params != null && params.length > 0){
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i+1, params[i]);
                }
            }

            rlines = ps.executeUpdate();
            conn.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return rlines;
    }

    public static void sql2test(){
        String id = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        String sql = "INSERT INTO count_table(interfacename,cdate,tablename,realcount)VALUES(?,?,?,?)";

        Object[] params = new Object[4];
        params[0] = id;
        params[1] = "2021-03-03 00:00:00";
        params[2] = "liumz";
        params[3] = 5;
        execUpdate(sql,params);
    }
    public static void main(String[] args) throws Exception {
        sql2test();
        Thread.sleep(1000);


        /*Connection conn = null;
        PreparedStatement ps = null;
        Statement st = null;
        try {
            conn = DriverManager.getConnection(url, userName, passWord);
            st = conn.createStatement();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }

        while (true){
            String sql1 = "insert into test1 values(id,?)";
            String sql2 = "insert into test2 values(seq_test.nextval,?)";
            List<String> list = new ArrayList<>();
            list.add(sql1);
            list.add(sql2);

            Iterator<String> iter = list.iterator();
            while (iter.hasNext()){
                String strSql = iter.next();
                System.out.println(strSql);
                try {
                    int i = st.executeUpdate(strSql);
                    if(i>0) {
                        System.out.println("添加成功");
                    }
                    else {
                        System.out.println("添加失败");
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }*/
    }
}

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页