package com.northglass.util;
|
|
import java.sql.Connection;
|
import java.sql.Driver;
|
import java.sql.ResultSet;
|
import java.sql.SQLException;
|
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
|
import org.springside.modules.utils.PropertiesLoader;
|
|
public class SetupUtil {
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(SetupUtil.class);
|
|
/**
|
* 判断数据库存在的方法。
|
* 主要用在首次安装系统时,若系统中相应的数据库和表都不存在,则在页面上提示用户创建表,并且不启动监听。
|
*
|
* @return
|
* @throws ClassNotFoundException
|
* @throws SQLException
|
*/
|
@SuppressWarnings("unchecked")
|
public static boolean databaseExists() throws ClassNotFoundException, SQLException {
|
LOGGER.trace("> Start databaseExists");
|
|
PropertiesLoader propertiesLoader = new PropertiesLoader("classpath:/application.properties");
|
|
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
|
dataSource.setDriverClass((Class<? extends Driver>) Class.forName(propertiesLoader.getProperty("jdbc.driver")));
|
dataSource.setUrl(propertiesLoader.getProperty("jdbc.url"));
|
dataSource.setUsername(propertiesLoader.getProperty("jdbc.username"));
|
dataSource.setPassword(propertiesLoader.getProperty("jdbc.password"));
|
|
Connection conn = dataSource.getConnection();
|
|
ResultSet rs = conn.getMetaData().getCatalogs();
|
LOGGER.trace("rs: " + rs);
|
|
while (rs.next()) {
|
String databaseName = rs.getString(1);
|
LOGGER.trace("databaseName:"+databaseName);
|
if (databaseName.equalsIgnoreCase("gmms_sdyk")) {
|
ResultSet tableRS = conn.getMetaData().getTables(null, "%", "%", new String[]{"TABLE"});
|
LOGGER.trace("tableRS: " + tableRS);
|
while (tableRS.next()) {
|
String tableName = tableRS.getString(3);
|
if (tableName.equalsIgnoreCase("gmms_user")) {
|
return true;
|
}
|
}
|
}
|
}
|
|
LOGGER.trace("> End databaseExists");
|
return false;
|
}
|
}
|