【发布时间】:2017-12-08 14:07:20
【问题描述】:
我在我的 JAVA 应用程序中执行了许多查询。由于 SQL 查询失败,我遇到了异常。从 SQL 查询中获取异常后,我想在我的 Web 应用程序的警报消息中显示表名。有什么方法可以获取我得到异常的表名。提前致谢。
我询问由于某些表而出现的任何类型的 SQLException。 我想得到下面的表名 System.out.println("exception1:::" + e);行。
代码:
try {
System.out.println("DB File : "+fileName);
fileData = readFile (fileName);
String[] staticProperties = fileData.toString().split("\n");
ipAddress = staticProperties[Constants.IP];
port = staticProperties[Constants.PORT];
datasource_name = staticProperties[Constants.DATASOURCE];
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, Constants.JNDI_FACTORY);
env.put(Context.PROVIDER_URL, "t3://" + ipAddress + ":"+port);
datasource = (DataSource) new InitialContext(env).lookup(datasource_name);
if (datasource != null) {
connection = datasource.getConnection();
statement = connection.createStatement();
rset = statement.executeQuery(query);
ResultSetMetaData rsetMetaData = rset.getMetaData();
while (rset.next()) {
dataFromDB = new ArrayList<String>();
for (int i = 1; i <= rsetMetaData.getColumnCount(); i++) {
dataFromDB.add(rset.getString(i));
}
inputDataFromDB.put(rset.getRow(), dataFromDB);
}
}
} catch (SQLException e) {
System.out.println("exception1:::" + e);
throw new SQLException(e);
} catch (Exception e) {
System.out.println("exception2:::" + e);
} finally {
if (rset != null) {
try {
rset.close();
} catch (Exception e) {
System.out.println("exception:::" + e);
}
}
if (statement != null) {
try {
statement.close();
} catch (Exception e) {
System.out.println("exception:::" + e);
}
}
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
System.out.println("exception:::" + e);
}
}
}
【问题讨论】:
-
你是如何执行查询的?
-
请输入代码,看看这个信息: stackoverflow.com/help/how-to-ask
-
@devpuh 使用数据源。
-
如果你有普通的 sql 查询,你可以简单地打印那些
-
@VijayKumar 编辑您的问题并添加导致问题的代码。 java中执行SQL查询的方式可能不同,所以请添加您的代码,否则我们无法帮助您。