【原】使用批处理BAT文件处理Mysql数据库

时间:2022-08-16 23:07:52

在做项目的时候,考虑到项目中很多模块是公用,数据库也是公用,所以考虑把公用模块的数据库全部用批处理生产,这样或多或少提高了一些效率。

处理方法是:

1:用一个txt保存该项目数据库名称,方便新建工程时直接修改为所需的DB名

2:用一个sql文件保存创建公用模块数据库语句

3:用bat文件实现运行sql,生成DB

具体如下:

//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

bat文件的路径需要注意:C:\Program Files\MySQL\MySQL Server 5.0\bin是Mysql的bin文件目录。另外引用example.sql需要指定该文件的目录(这里折磨了很久)

下面是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>operate.txt文件中输入需要创建的游戏数据库,获得DBNAME</br>生成Sql文件 </br>根据DBNAME修改Bat文件的Sql执行语句</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";

/**
* 读工程下的operate.txt文件获得Name
*/
public static String DataBaseName() {
File file = new File(FileName);
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;
}

/**
* 修改Sql文件内容
*/
public static void writeSql(String dataBaseName) {

File exampleFile = new File(exampleSqlPath);
File curFile = new File(sqlPath);
String content = read(exampleFile);
System.out.println(content);
content = content.replaceAll("databaseName",dataBaseName);
write(content, curFile);
System.out.println(content);

}

/**
* 执行bat文件
* @param databaseName
*/
public static void runBat(){
try {
Process process = Runtime.getRuntime().exec(batPath);
CmdProcess cmdProcess = new CmdProcess(process.getInputStream());
cmdProcess.start();
} catch (IOException e) {
System.err.println("bat文件不存在或者路径错误");
e.printStackTrace();
}

}


public 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 process = Runtime.getRuntime().exec(command);
//CmdProcess cmdProcess = new CmdProcess(process.getInputStream());
//cmdProcess.start();
//reslut = "操作成功!";
//} catch (IOException e) {
//reslut = "操作失败!";
//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 CmdProcess extends Thread{

InputStream inputStream;

public InputStream getInputStream() {
return inputStream;
}

public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}

public CmdProcess() {
super();
// TODO Auto-generated constructor stub
}

public CmdProcess(InputStream inputStream) {
super();
this.inputStream = inputStream;
}

public void run() {
// TODO Auto-generated method stub
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader 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 stub
String command = "C:\\Documents and Settings\\Administrator\\桌面\\aa.bat";
try {
Process process = Runtime.getRuntime().exec(command);
CmdProcess cmdProcess = new CmdProcess(process.getInputStream());
cmdProcess.start();
process.waitFor();
}catch(Exception e){
e.printStackTrace();
}
}
}


example.sql用于作为sql语句的模板,便于扩展,每次运行把example.sql的内容先更改数据库名再复制到test.sql中。bat路径参考:点这里