1、编辑shell脚本
#!/bin/bash
JAVA_HOME=/usr/local/src/jdk1.8
LANG=zh_CN.UTF-8
BATCHLIB=/app/demoBatch/bin/lib
BATCHCLASSES=/app/demoBatch/bin/classes
#export JAVA_HOME
${JAVA_HOME}/bin/java -Xms1024M -Xmx1024M -Dfile.encoding=UTF-8 -Djava.ext.dirs=${BATCHLIB} -classpath .:${BATCHCLASSES} com.jamy.song.Demo
2 创建java项目(非maven项目)
数据库参数配置文件jdbc.properties
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.227.121:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8&nullCatalogMeansCurrent=true
jdbc.username=root
jdbc.password=root
jdbc.initialSize=20
jdbc.minPoolSize=10
jdbc.maxPoolSize=100
jdbc.MaxStatements=5
jdbc.MaxIdleTime=600
jdbc工具类
public class JDBCUtil {
private static final ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
private static Properties prop = new Properties();
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
private static Connection conn = null;
static {
//数据库连接初始化
try{
prop.load(JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
}catch(IOException e){
System.out.println("数据库参数文件读取错误");
e.printStackTrace();
}
dataSource.setUser(prop.getProperty("jdbc.username"));
dataSource.setJdbcUrl(prop.getProperty("jdbc.url"));
dataSource.setPassword(prop.getProperty("jdbc.password"));
try{
dataSource.setDriverClass(prop.getProperty("jdbc.driverClass"));
}catch(PropertyVetoException e){
System.out.println("连接池构造异常1!");
e.printStackTrace();
}
dataSource.setInitialPoolSize(Integer.valueOf(prop.getProperty("jdbc.initialSize")));
dataSource.setMinPoolSize(Integer.valueOf(prop.getProperty("jdbc.minPoolSize")));
dataSource.setMaxPoolSize(Integer.valueOf(prop.getProperty("jdbc.maxPoolSize")));
dataSource.setMaxStatements(Integer.valueOf(prop.getProperty("jdbc.MaxStatements")));
dataSource.setMaxIdleTime(Integer.valueOf(prop.getProperty("jdbc.MaxIdleTime")));
try {
threadLocal.set(dataSource.getConnection());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//获取连接
public static Connection getConn(){
conn = threadLocal.get();
System.out.println("threadLocal获取连接:" + conn);
try{
if (conn == null || conn.isClosed()) {
Class.forName(prop.getProperty("jdbc.driverClass")).toString();
conn = DriverManager.getConnection(prop.getProperty("jdbc.url").toString(),
prop.getProperty("jdbc.username").toString(),
prop.getProperty("jdbc.password").toString());
try{
dataSource.setDriverClass(prop.getProperty("jdbc.driverClass"));
}catch(PropertyVetoException e){
System.out.println("连接池构造异常2!");
e.printStackTrace();
throw new IllegalStateException();
}
}
System.out.println("conn:"+conn);
}catch(Exception e){
System.out.println("获取数据库连接异常!");
e.printStackTrace();
throw new IllegalStateException();
}
return conn;
}
//关闭连接
public static void closeConn(){
try{
if(threadLocal.get() != null && !threadLocal.get().isClosed()){
threadLocal.get().close();
threadLocal.remove();
}
}catch(Exception e){
System.out.println("关闭数据库连接异常!");
}
}
//事务开始
public static void beginTransaction(){
try{
getConn().setAutoCommit(false);
}catch(SQLException e){
System.out.println("开始事务异常");
throw new IllegalStateException();
}
}
//事务提交
public static void commit(){
try{
getConn().commit();
}catch(SQLException e){
System.out.println("提交事务异常");
throw new IllegalStateException();
}
}
//事务回滚
public static void rollback(){
try{
Connection conn = getConn();
conn.setAutoCommit(false);
conn.rollback();
conn.setAutoCommit(true);
}catch(SQLException e){
System.out.println("事务回滚异常");
throw new IllegalStateException();
}
}
}
数据批量插入类
public class UserDao {
private Connection conn;
private JSONObject json;
PreparedStatement ps = null;
public UserDao(Connection conn, JSONObject json) {
this.conn = conn;
this.json = json;
}
public void insertData() throws SQLException {
String sql = "insert into tb_user(name,age,address) values(?,?,?)";
ps = conn.prepareStatement(sql);
try{
for (int i=0; i<100; i++) {
ps.setString(1, "张三"+i);
ps.setInt(2, 23);
ps.setString(3, "星颂家园24号2302室");
ps.addBatch();
}
ps.executeBatch();
}catch(SQLException e){
System.out.println("执行插入数据异常!");
}finally {
if (ps != null) {
ps.close();
}
if (conn != null){
conn.close();
}
}
}
}
主类(就是批处理的执行类,shell脚本里面配置的执行类)
public class Demo {
public static void main(String[] args) {
System.out.println("hello world");
System.out.println("hello world");
System.out.println("hello world");
System.out.println("你好,世界!");
UserDao dao = new UserDao(JDBCUtil.getConn(), null);
try {
dao.insertData();
System.out.println("执行dao方法成功");
} catch (SQLException e) {
System.out.println("执行dao方法报错!!!");
throw new RuntimeException(e);
}
}
}
批处理依赖的jar包(存放位置在shell脚本里面已配置好)
3、构建项目,主要是为了编译我们写的Java类
依次选择菜单栏的Bulid>Build Project,项目就会编译构建,然后出现的out输出包的com文件下,编译好的.class文件就在里面
直接把com文件夹和jdbc.properties配置文件复制到执行目录下,即可
4、执行shell脚本(Linux一定要安装jdk)
可以配置定时任务执行shell脚本,这里就手动执行了