Java program execution SQL script file

Source: Internet
Author: User

First introduce the Ibatis-common-2.jar package

Import Com.ibatis.common.jdbc.ScriptRunner; Import com.ibatis.common.resources.Resources;

The Jpetstore test code is as follows:

Package com.ibatis.jpetstore.test;

Import Java.sql.DriverManager; Import java.util.Properties;

Import Com.ibatis.common.jdbc.ScriptRunner; Import com.ibatis.common.resources.Resources; Import com.mysql.jdbc.Connection;

public class Dbtestsql {

public static void Main (string[] args) {try {Properties props = resources.getresourceasproperties ("Properties/database. Properties "); String url = props.getproperty ("url"); String Driver = props.getproperty ("Driver"); String username = props.getproperty ("username"); String Password = props.getproperty ("password"); System.out.println (URL); if (Url.equals ("Jdbc:mysql://localhost:3306/jpetstore1")) {Class.forName (driver). newinstance (); Connection conn = (Connection) drivermanager.getconnection (URL, username, password); Scriptrunner runner = new Scriptrunner (conn, false, false); Runner.seterrorlogwriter (NULL); Runner.setlogwriter (NULL); Runner.runscript (Resources.getresourceasreader ("Ddl/mysql/jpetstore-mysql-schema.sql")); Runner.runscript (Resources.getresourceasreader ("Ddl/mysql/jpetstore-mysql-dataload.sql")); }} catch (Exception e) {e.printstacktrace ();}

}

}

Scriptrunner (com.ibatis.common.jdbc.*) UsageThe Scriptrunner class is used to execute SQL statements, such as creating a database schema, or passing in a default or test database, and so on. The following example can be used to recognize its ease of use:
Example 1: Using an off-the-shelf database connection
Connection conn=getconnection ();//some method to get a Connection
Scriptrunner runner=new Scriptrunner ();
Runner.runscript (Conn,resources.getresourceasreader ("Com/some/resource/path/initialize.sql"));
Conn.close ();
Example 2: Using a new database connection
Scriptrunner runner=new Scriptrunner ("Com.some.Driver", "jdbc:url://db", "Login", "password");
Runner.runscript (conn,new filereader ("/user/local/db/scripts/initialize-db.sql"));
Example 3: Using a newly created data connection
Properties props= getProperties ();//some Properties Form somewhere
Scriptrunner runner =new Scriptrunner (props);
Runner.runscript (conn,new filereader ("/user/local/db/scripts/initialize-db.sql"));

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.