在java代码中创建mysql数据库和导入数据

public class DatabaseCreatorMysql implements DatabaseCreator{
	
	@Override
	public void createDatabaseAndInitialize(DriverConnectionProvider cp,
			String databaseName, String sourceFilePath) {
		createDatabase(cp,databaseName);
		initializeDatabase(cp,databaseName,sourceFilePath);
	}
	
	
	@Override
	public void createDatabase(ConnectionProvider cp,String databasename){
		Connection con = cp.getConnection();
		try {
			con.createStatement().execute("create database "+databasename);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void initializeDatabase(DriverConnectionProvider cp,String databaseName,String sourceFilePath){
		// TODO Auto-generated method stub
		try {
			String username = cp.getUser();
			String password = cp.getPassword();
			
			StringBuffer sb = new StringBuffer("cmd.exe /C mysql -u");
			sb.append(username);
			
			if(password!=null && !password.equalsIgnoreCase(""))
				sb.append(" -p").append(password);
			
			sb.append(" ").append(databaseName).append("<").append(sourceFilePath);
			//Process proc = java.lang.Runtime.getRuntime().exec("cmd.exe /C mysql -uroot test2d:/test.sql",
//				"cmd /c mysql -u root",
//				"source C:/Users/jiangkai/Desktop/erp.sql",
//				"cmd.exe /C mysqldump -uroot erp>d:/test.sql"
//		};
//		
//		Process proc = java.lang.Runtime.getRuntime().exec("cmd.exe /C mysql -uroot test2

你可能感兴趣的:(j2ee学习之路)