首页 文章 精选 留言 我的

精选列表

搜索[java],共10000篇文章
优秀的个人博客,低调大师

java 执行sql文件

# 背景 用例执行完毕,期望回滚数据,因此希望执行sql来回滚数据 # 步骤 直接show代码,借助的是mybatis的ScriptRunner /** * 执行xx库下的表备份脚本 * * @param tableName */ public static void runSqlInStat(String tableName) { String className = Configurations.INSTANCE.get("jdbc.xx.driver"); String dbUrl = Configurations.INSTANCE.get("jdbc.xx.url"); String dbUsername = Configurations.INSTANCE.get("jdbc.xx.username"); String dbPassword = Configurations.INSTANCE.get("jdbc.xx.password"); try { Class.forName(className); Connection conn = DriverManager.getConnection(dbUrl, dbUsername, dbPassword); ScriptRunner runner = new ScriptRunner(conn); runner.setAutoCommit(true); String fileName = String.format("src/main/resources/db/%s.sql", tableName); File file = new File(fileName); try { if (file.getName().endsWith(".sql")) { runner.setFullLineDelimiter(false); runner.setDelimiter(";");//语句结束符号设置 runner.setLogWriter(null);//日志数据输出,这样就不会输出过程 runner.setSendFullScript(false); runner.setAutoCommit(true); runner.setStopOnError(true); runner.runScript(new InputStreamReader(new FileInputStream(fileName), "utf8")); logger.info(String.format("【%s】回滚成功", tableName)); } } catch (Exception e) { e.printStackTrace(); } conn.close(); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } 虽千万人,吾往矣!

优秀的个人博客,低调大师

Java多线程实例

Thread类的构造方法:public Thread( );public Thread(Runnable target);public Thread(String name);public Thread(Runnable target, String name);public Thread(ThreadGroup group, Runnable target);public Thread(ThreadGroup group, String name);public Thread(ThreadGroup group, Runnable target, String name);public Thread(ThreadGroup group, Runnable target, String name, long stackSize); Runnable target实现了Runnable接口的类的实例。要注意的是Thread类也实现了Runnable接口,因此,从Thread类继承的类的实例也可以作为target传入这个构造方法。 String name线程的名子。这个名子可以在建立Thread实例后通过Thread类的setName方法设置。如果不设置线程的名子,线程就使用默认的线程名:Thread-N,N是线程建立的顺序,是一个不重复的正整数。 ThreadGroup group当前建立的线程所属的线程组。如果不指定线程组,所有的线程都被加到一个默认的线程组中。关于线程组的细节将在后面的章节详细讨论。 long stackSize线程栈的大小,这个值一般是CPU页面的整数倍。如x86的页面大小是4KB。在x86平台下,默认的线程栈大小是12KB。 ================================================ public class MultiThreadsTest{ public static void main(String args[]){ Resource res = new Resource(); ProducerThread p1 = new ProducerThread(res); ProducerThread p2 = new ProducerThread(res); ProducerThread p3 = new ProducerThread(res); p1.start(); p2.start(); p3.start(); new Thread(p1,"p1"); ConsumerThread c1 = new ConsumerThread(res); c1.start(); } } /** 资源类,拥有product,以及生产和消费product的方法 @author hp* */class Resource { private int product = 0; public Resource(){ this.product = 0; } public int getProduct() { return product; } public void setProduct(int product) { this.product = product; } public synchronized void produce(){ if(this.product > 100){ try{ wait(); System.out.println("产品已满,请稍后再生产。"); }catch(Exception e){ e.printStackTrace(); } return; } this.product++; System.out.println("生产一个产品,当前有产品:" + this.product); notifyAll(); } public synchronized void consume(){ if(this.product <= 0){ try{ wait(); System.out.println("缺货,请稍等。"); }catch(Exception e ){ e.printStackTrace(); } return; } this.product--; System.out.println("消费一个产品,当前有产品:" + this.product); notifyAll(); } } /** 生产者,可实例化多个生产者线程实例。调用Resource的生成方法生产product @author hp* */class ProducerThread extends Thread{ private Resource r; public ProducerThread(Resource r){ this.r = r; } public void run(){ while(true){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } r.produce(); } } } /** 消费者,可实例化多个消费者线程实例。调用Resource的消费方法消费product @author hp* */class ConsumerThread extends Thread{ private Resource r; public ConsumerThread(Resource r){ this.r = r; } public void run(){ while(true){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } r.consume(); } } }

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册