Java tutorial
/* * Copyright 2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.noday.core.dao; import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; import java.util.List; import net.noday.core.exception.AppStartupException; import net.noday.core.model.App; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.EncodedResource; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceUtils; import org.springframework.stereotype.Repository; import org.springframework.util.FileCopyUtils; /** * cat AppDao * * @author <a href="http://www.noday.net">Noday</a> * @version , 2012-11-24 * @since */ @Repository public class AppDao { private static final Logger log = Logger.getLogger(AppDao.class); @Autowired private JdbcTemplate jdbc; public void initDB() { // TODO ?? executeSql("domaincat.sql"); } public void updateDB(String version) { executeSql("db/update" + version + ".sql"); // throw new AppStartupException("??"); } /** * sql * // TODO ????? * @param sqlFilePath */ private void executeSql(String sqlFilePath) { if (sqlFilePath == null) { throw new AppStartupException("sql[" + sqlFilePath + "]?"); } try { log.info("[" + sqlFilePath + "]"); Resource sqlRes = new ClassPathResource(sqlFilePath); EncodedResource encRes = new EncodedResource(sqlRes, "UTF-8"); String sqls = null; sqls = FileCopyUtils.copyToString(encRes.getReader()); String[] sqlArr = sqls.split(";"); log.info(""); for (String sql : sqlArr) { log.info(sql); jdbc.execute(sql); } log.info("?"); } catch (IOException e) { log.error("?[" + sqlFilePath + "]", e); throw new AppStartupException("?[" + sqlFilePath + "]", e); } catch (Exception e) { log.error("[" + sqlFilePath + "]", e); throw new AppStartupException("[" + sqlFilePath + "]", e); } } public App getAppConfig() { List<String> tables = jdbc.queryForList("show tables", String.class); Connection conn = DataSourceUtils.getConnection(jdbc.getDataSource()); try { conn.setAutoCommit(false); if (tables == null || tables.size() == 0 || !tables.contains("app_config")) { initDB(); } else { String version = jdbc.queryForObject("select a.version from app_config a limit 1", String.class); if (!"1.1".equalsIgnoreCase(version)) { updateDB("1_1"); } } conn.commit(); } catch (DataAccessException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } String sql = "select * from app_config limit 1"; App cfg = jdbc.queryForObject(sql, new BeanPropertyRowMapper<App>(App.class)); if (cfg == null) { throw new AppStartupException("??"); } return cfg; } public void resetData() { executeSql("db/demo.sql"); } }