Java Execution SQL file

Source: Internet
Author: User

# background

The use case is complete and the data is expected to be rolled back, so you want to execute the SQL rollback data

# step

Direct show code, with the help of MyBatis's Scriptrunner

/*** Execute the table backup script under XX library * *@paramTableName*/     Public Static voidRunsqlinstat (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=NewScriptrunner (conn); Runner.setautocommit (true); String FileName= String.Format ("Src/main/resources/db/%s.sql", TableName); File File=NewFile (fileName); Try {                if(File.getname (). EndsWith (". sql") {runner.setfulllinedelimiter (false); Runner.setdelimiter (";");//statement end symbol Settings Runner.setlogwriter (NULL)///log data output so that no process runner.setsendfullscript is output (false); Runner.setautocommit (true); Runner.setstoponerror (true); Runner.runscript (NewInputStreamReader (NewFileInputStream (fileName), "UTF8")); Logger.info (String.Format (""%s "Rollback succeeded", TableName)); }            } Catch(Exception e) {e.printstacktrace ();        } conn.close (); } Catch(SQLException e) {e.printstacktrace (); } Catch(ClassNotFoundException e) {e.printstacktrace (); }    }

Java Execution SQL file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.