seam里面在发布的时候自动创建数据库

(1)下面是发布模板的代码:
public void publishOneTemplate(){
	try{
		ssId = this.getFTPClientInfo();
		if(ssId != null && !"".equals(ssId)){
			this.connectServerAndSetFileType();
			localFilePath = this.getFileAbsolutePath();
			System.out.println("localFilePath="+localFilePath);
			if(localFilePath != null && flag == true){
				createDatabase(localFilePath);
				this.createDirectoryName(publishWebsiteName,ftpClient,new File(localFilePath),localFilePath);
				}
				this.closeConnect();
			}
		}catch(java.net.SocketException se){
			se.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
}

 

(2)下面是自动建立数据库mysql
public void createDatabase(String path){
		Connection connection = null;
		 try {
			String jdbcFile = path + "/WEB-INF/conf/jdbc.properties";
			InputStream in = new FileInputStream(jdbcFile);
			Properties p = new Properties();
			p.load(in);
			String driver = p.getProperty("jdbc.driver");
			String url = p.getProperty("jdbc.url");
			String  databaseUrl = url.substring(0, url.lastIndexOf("/"))+"/information_schema";
			String databaseName = url.substring(url.lastIndexOf("/")+1,url.length());
			String user = p.getProperty("jdbc.username");
			String password = p.getProperty("jdbc.password");
			Class.forName(driver);
			connection = DriverManager.getConnection(databaseUrl, user, password);
			connection.setAutoCommit(false);
			Statement statement = connection.createStatement();
			ResultSet rs = statement.executeQuery("select * from SCHEMATA where SCHEMA_NAME='"+databaseName+"'");
			if (!rs.next()){
				statement.clearBatch();
				statement.addBatch("create database if not exists `"+databaseName+"`");
				File scriptFile = new File(path + "/WEB-INF/script/database.sql");
				String script = FileUtil.convertFile2String(scriptFile);				
				statement.addBatch("USE `"+databaseName+"`");
				String[] scripts = script.split(";");
				for (int i = 0; i < scripts.length; i++){
					if (!"".equals(scripts[i].trim()))
						statement.addBatch(scripts[i]);
				}
				statement.executeBatch();
				connection.commit();
			}
		} catch (FileNotFoundException e) {
			logger.error(e.getMessage());
		} catch (IOException e) {
			logger.error(e.getMessage());
		} catch (ClassNotFoundException e) {
			logger.error(e.getMessage());
		} catch (SQLException e) {
			if (connection != null){
				try {
					connection.rollback();
				} catch (SQLException e1) {
					logger.error(e1.getMessage());
				}
			}
			logger.error(e.getMessage());
		} finally {
			if (connection != null){
				try {
					connection.close();
				} catch (SQLException e) {
					logger.error(e.getMessage());
				}
			}
		}
}

 

(3)这是它里面调用的一个方法。
public static String convertFile2String(File file, String encoding) throws IOException{
		byte[] bs = new byte[new Long(file.length()).intValue()];
		FileInputStream in = new FileInputStream(file);
		try{
			in.read(bs);
		}finally{
			in.close();
		}
		return encoding==null? new String(bs):new String(bs,encoding);
}

public static String convertFile2String(File file) throws IOException{
		return convertFile2String(file,null);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值