22,209
社区成员
发帖
与我相关
我的任务
分享
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
ResultSet rs = null;
try {
connection = dataSource.getConnection(mysqlConfig.getUserName(), mysqlConfig.getPasswd());
String sqlTable = "show tables";
statement = connection.prepareStatement(sqlTable);
resultSet = statement.executeQuery();
while (resultSet.next()) {
String tableName = resultSet.getString(1);
String sqlData = "select * from `" + tableName + "` limit 1";
statement = connection.prepareStatement(sqlData);
rs = statement.executeQuery();
rs.previous();
while (rs.next()) {
logger.info(tableName+"表====>数据执行通过");
}
}
return "";
} catch (SQLException e) {
logger.error("connect to database exception", e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
logger.error("close connection error", e);
}
}
//return "";
}
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
ResultSet rs = null;
boolean connIsOK = false; //加上连接是否正常的标识
boolean tableNameExists = false; //判断表名是否存在
try {
connection = dataSource.getConnection(mysqlConfig.getUserName(), mysqlConfig.getPasswd());
connIsOK = true; //如果上一步已打开连接,则这里标识
String sqlTable = "show tables";
statement = connection.prepareStatement(sqlTable);
resultSet = statement.executeQuery();
//加一段判断表是否存在的代码, 不存在不要往下执行
while (resultSet.next()) {
String tableName = resultSet.getString(1);
String sqlData = "select * from `" + tableName + "` limit 1";
statement = connection.prepareStatement(sqlData);
rs = statement.executeQuery();
rs.previous();
while (rs.next()) {
logger.info(tableName+"表====>数据执行通过");
}
}
return "";
} catch (Exception e) {//改成普通的Exception , 保证都能捕捉到。或者你多加几级 Exception 也行
logger.error("connIsOK ", connIsOK); //输出连接标识
logger.error("tableNameExists ", tableNameExists); //输出表存在标识
logger.error("connect to database exception", e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
logger.error("close connection error", e);
}
}
//return "";
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
ResultSet rs = null;
boolean connIsOK = false; //加上连接是否正常的标识
try {
connection = dataSource.getConnection(mysqlConfig.getUserName(), mysqlConfig.getPasswd());
connIsOK = true; //如果上一步已打开连接,则这里标识
String sqlTable = "show tables";
statement = connection.prepareStatement(sqlTable);
resultSet = statement.executeQuery();
while (resultSet.next()) {
String tableName = resultSet.getString(1);
String sqlData = "select * from `" + tableName + "` limit 1";
statement = connection.prepareStatement(sqlData);
rs = statement.executeQuery();
rs.previous();
while (rs.next()) {
logger.info(tableName+"表====>数据执行通过");
}
}
return "";
} catch (Exception e) {//改成普通的Exception , 保证都能捕捉到
logger.error("connIsOK ", connIsOK); //输出连接标识
logger.error("connect to database exception", e);
return mysqlConfig.getConnection()+" connect to database exception";
} finally {
try {
if (rs != null) {
rs.close();
}
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
logger.error("close connection error", e);
}
}
//return "";
}