1. 创建一个普通的Java项目
主类
public class Demo {
public static void main(String[] args) {
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);
}
}
}
JDBC工具类
public class JDBCUtil {
private static final ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
private static Properties prop = new Properties();
private static DruidDataSource dataSource = new DruidDataSource();
private static Connection conn = null;
static {
//数据库连接初始化
try{
prop.load(JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
}catch(IOException e){
System.out.println("数据库参数文件读取错误");
e.printStackTrace();
}
dataSource.setUsername(prop.getProperty("jdbc.username"));
dataSource.setUrl(prop.getProperty("jdbc.url"));
dataSource.setPassword(prop.getProperty("jdbc.password"));
try{
dataSource.setDriverClassName(prop.getProperty("jdbc.driverClass"));
}catch(Exception e){
System.out.println("连接池构造异常1!");
e.printStackTrace();
}
dataSource.setInitialSize(Integer.valueOf(prop.getProperty("jdbc.initialSize")));
dataSource.setMinIdle(Integer.valueOf(prop.getProperty("jdbc.minPoolSize")));
dataSource.setMaxActive(Integer.valueOf(prop.getProperty("jdbc.maxPoolSize")));
dataSource.setMaxWait(Integer.valueOf(prop.getProperty("jdbc.maxWait")));
dataSource.setTimeBetweenEvictionRunsMillis(Integer.valueOf(prop.getProperty("jdbc.timeBetweenEvictionRunsMillis")));
dataSource.setMinEvictableIdleTimeMillis(Integer.valueOf(prop.getProperty("jdbc.minEvictableIdleTimeMillis")));
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.setDriverClassName(prop.getProperty("jdbc.driverClass"));
}catch(Exception 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();
}
}
}
JDBC操作类
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();
}
}
}
}
配置文件
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.227.122:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2b8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
jdbc.username=root
jdbc.password=root
jdbc.initialSize=20
jdbc.minPoolSize=10
jdbc.maxPoolSize=100
jdbc.maxWait=60000
jdbc.timeBetweenEvictionRunsMillis=60000
jdbc.minEvictableIdleTimeMillis=300000
2. 引入依赖jar包
3. 构建包设置
4. 构建项目jar包
这里我们用解压缩工具打开jar包看看,可以看到一个jar包里面我们需要的东西都在
编写shell脚本
demo3.sh
#!/bin/bash
JAVA_HOME=/usr/local/src/jdk1.8
LANG=zh_CN.UTF-8
BATCHROOT=/app/demoBatch
${JAVA_HOME}/bin/java -Xms1024M -Xmx1024M -Dfile.encoding=UTF-8 -cp ${BATCHROOT}/jamy-demo.jar com.jamy.song.Demo
测试
将刚才生成的jamy-demo.jar包上传到linux服务器上,存放位置就是shell脚本配置的位置(/app/demoBatch/)
执行shell脚本之前查询sql
执行shell脚本
执行shell脚本之后我们再来查询sql看看