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"));