首页 文章 精选 留言 我的

精选列表

搜索[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(); } } }

资源下载

更多资源
Mario

Mario

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

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册