When working on the project, considering that many modules are public and databases are public, all databases of Public modules are used for batch production, which increases efficiency more or less.
The solution is:
1: Use a TXT file to save the database name of the project, so that you can directly change it to the required database name when creating a project.
2: Use an SQL file to save and create a public module database statement
3: Use the BAT file to run SQL and generate DB
The details are as follows:
//example.sql
CREATE DATABASE databaseName;USE databaseName;CREATE TABLE Register ( userID varchar(255) primary key, pass_word varchar(255) not null, register_time datetime);CREATE TABLE Login(userID varchar(255) primary key,nick_name varchar(255) );alter table Register add column aaa varchar(255);
// test.bat
set path=C:\Program Files\MySQL\MySQL Server 5.0\bincd ./set CURR_PATH=%cd%mysql -h localhost -u root --password=123456 < %~dp0%test.sqlpause
Note that c: \ Program Files \ mysql \ MySQL Server 5.0 \ bin is the binfile directory of MySQL. In addition, to reference example. SQL, You need to specify the directory of the file (it has been suffering for a long time)
The following is the implementation of Java.
Package CN. eden. gamesever. createdatabase; import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. file; import Java. io. filenotfoundexception; import Java. io. filereader; import Java. io. filewriter; import Java. io. ioexception;/*** <br> In the operate.txt file, enter the game database to be created, obtain the SQL file generated by dbname </BR>. </BR> modify the SQL statement of the BAT file according to dbname </BR> * @ author lujian */public class runbat {Private Static string sqlpath = ". \ SQL \ test. SQL "; Private Static string batpath = ". \ SQL \ create. bat "; Private Static string databasename =" "; Private Static string filename = ". \ SQL \ operate.txt "; Private Static string examplesqlpath = ". \ SQL \ example. SQL ";/*** get the name */public static string databasename () {file = new file (filename) from the operate.txt file under the project ); try {bufferedreader in = new bufferedreader (New filereader (File); If (( Databasename = in. Readline ())! = NULL) {If (databasename. endswith (";") {databasename = databasename. substring (0, databasename. length ()-1) ;}} in. close ();} catch (exception e) {system. out. println (E);} return databasename;}/*** modify SQL file content */public static void writesql (string databasename) {file examplefile = new file (examplesqlpath ); file curfile = new file (sqlpath); string content = read (examplefile); system. out. println (content); cont Ent = content. replaceall ("databasename", databasename); write (content, curfile); system. out. println (content);}/*** execute the BAT file * @ Param databasename */public static void runbat () {try {Process = runtime.getruntime(cmd.exe C (batpath ); worker Process finished process = new worker process (process. getinputstream (); processing process. start ();} catch (ioexception e) {system. err. println ("the BAT file does not exist or the path is incorrect"); E. printstacktrace () ;}} P Ublic static string read (File SRC) {stringbuffer res = new stringbuffer (); string line = NULL; // int linenum = 1; try {bufferedreader reader = new bufferedreader (New filereader (SRC); While (line = reader. readline ())! = NULL) {// If (linenum = 1) {// line = line. replaceall (// line. substring (4, line. length ()-1), databasename); //} // ++ linenum; Res. append (LINE + "\ n");} reader. close ();} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} return res. tostring ();} public static void write (string cont, file Dist) {try {bufferedwriter writer = new bufferedwriter (New filewriter (Dist); writer. write (cont); writer. flush (); writer. close ();} catch (ioexception e) {e. printstacktrace () ;}// public static string runbatfile () {// string reslut = ""; // string command = "D: \ j2eeworkspace \ testsql \ SQL \ mysqlbattest. bat "; // string command =" D: \ test. bat "; // try {// Process = runtime.getruntime(cmd.exe C (command); // specify process worker process = new worker process (process. getinputstream ()); // Complete process. Start (); // reslut = "operation successful! "; //} Catch (ioexception e) {// reslut =" operation failed! "; // E. printstacktrace (); //} // return reslut; //} public static void main (string ARGs []) {databasename (); writesql (databasename); runbat ();} public static string getdatabasename () {return databasename;} public static void setdatabasename (string databasename) {runbat. databasename = databasename ;}}
Package CN. eden. gamesever. createdatabase; import Java. io. bufferedreader; import Java. io. inputstream; import Java. io. inputstreamreader; public class extends process extends thread {inputstream; Public inputstream getinputstream () {return inputstream;} public void setinputstream (inputstream) {This. inputstream = inputstream;} public writable process () {super (); // todo auto-generated constructor s Tub} public writable process (inputstream) {super (); this. inputstream = inputstream;} public void run () {// todo auto-generated method stubtry {inputstreamreader = new inputstreamreader (inputstream); bufferedreader = new bufferedreader (inputstreamreader ); string line = NULL; while (line = bufferedreader. readline ())! = NULL) {system. Out. println (line);} Try {} finally {If (bufferedreader! = NULL) bufferedreader. Close (); If (inputstreamreader! = NULL) inputstreamreader. Close (); If (inputstream! = NULL) inputstream. close () ;}} catch (exception e) {e. printstacktrace () ;}/ *** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubstring command = "C: \ Documents and Settings \ Administrator \ Desktop \ AA. bat "; try {Process = runtime.getruntime(cmd.exe C (command); worker process implements process = new worker process (process. getinputstream (); processing process. start (); process. waitfor ();} catch (exception e) {e. printstacktrace ();}}}
Example. SQL is used as a template for SQL statements, which is easy to expand. Each operation changes the Database Name of example. SQL before copying it to test. SQL. Bat path reference: click here