import java.io.IOException; import java.io.BufferedReader; import java.io.FileReader; import java.sql.Connection; import java.sql.SQLException; import java.sql.DriverManager; import java.sql.Statement; public class Demo { private String mFile; private String mHost; private String mUser; private String mPasswd; private String mDB; private String mQuery; private Demo(String file, String host, String user, String passwd, String db) { this.mFile = file; /* read query from file */ this.mHost = host; this.mUser = user; this.mPasswd = passwd; this.mDB = db; } public void doIt() throws IOException, SQLException, ClassNotFoundException { mQuery = readQuery(); execQuery(); } private String readQuery() throws IOException { BufferedReader r = new BufferedReader(new FileReader(mFile)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = r.readLine()) != null) { sb.append(line); } r.close(); return sb.toString(); } private void execQuery() throws SQLException, ClassNotFoundException { System.out.println("loading driver"); Class.forName("com.mysql.jdbc.Driver"); System.out.println("making connection"); Connection conn = DriverManager.getConnection(makeURL()); System.out.println("executing update"); Statement st = conn.createStatement(); st.executeUpdate(mQuery); System.out.println("closing connection"); st.close(); conn.close(); } private String makeURL() { return "jdbc:mysql://" + mHost + "/" + mDB + "?user=" + mUser + "&password=" + mPasswd; } public static void main(String argv[]) { if (argv.length != 5) { System.err.println("usage: java Demo FILENAME HOST USER PASSWD DBNAME"); System.exit(1); } Demo demo = new Demo(argv[0], argv[1], argv[2], argv[3], argv[4]); try { demo.doIt(); } catch (Throwable t) { t.printStackTrace(); System.exit(1); } System.exit(0); } }