package com.whty.common.utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;
/**
* JDBC的批处理
* @author 丑哥最风骚
* @date 2014-04-23
*/
public class JdbcUtil {
private static Properties jdbcProperties = null;
static {
InputStream is = JdbcUtil.class.getResourceAsStream("/properties/jdbc.properties");
jdbcProperties = new Properties();
try {
jdbcProperties.load(is);
} catch (IOException e1) {
e1.printStackTrace();
}
}
/**
* 获取数据库连接
* @return
* @throws ClassNotFoundException
*/
public static Connection getConnection() {
Connection conn = null;
try {
String className = jdbcProperties.getProperty("jdbc.driverClassName");
String url = jdbcProperties.getProperty("jdbc.url");
String username = jdbcProperties.getProperty("jdbc.username");
String password = jdbcProperties.getProperty("jdbc.password");
Class.forName(className);
conn = DriverManager.getConnection(url, username, password);
} catch(Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 批量向Vip表中插入数据
*/
public static void insertVipBatch() {
String sql = "insert into vip (vip_id, vip_extid, vip_name) values (VIP_SEQ.nextval, ?, ?)";
Connection conn = null;
try {
conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "001");
pstmt.setString(2, "阿大");
pstmt.addBatch();
pstmt.setString(1, "002");
pstmt.setString(2, "阿二");
pstmt.addBatch();
pstmt.setString(1, "003");
pstmt.setString(2, "阿三");
pstmt.addBatch();
pstmt.executeBatch();
conn.commit();
conn.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
JDBC批处理
最新推荐文章于 2022-11-04 11:31:23 发布