Java 从SQL失败中获取表
我在JAVA应用程序中执行了许多查询。由于SQL查询失败,我遇到异常。从SQL查询中获取异常后,我希望在Web应用程序的警报消息中显示表名。是否有任何方法可以获取我得到异常的表名。提前谢谢 我还询问了由于某些表而出现的任何类型的SQLException。 我想将下面的表名获取到System.out.println(“exception1::”+e);线路 代码:Java 从SQL失败中获取表,java,sql,jsp,jdbc,Java,Sql,Jsp,Jdbc,我在JAVA应用程序中执行了许多查询。由于SQL查询失败,我遇到异常。从SQL查询中获取异常后,我希望在Web应用程序的警报消息中显示表名。是否有任何方法可以获取我得到异常的表名。提前谢谢 我还询问了由于某些表而出现的任何类型的SQLException。 我想将下面的表名获取到System.out.println(“exception1::”+e);线路 代码: 试试看{ System.out.println(“DB文件:”+文件名); fileData=readFile(文件名); Strin
试试看{
System.out.println(“DB文件:”+文件名);
fileData=readFile(文件名);
String[]staticProperties=fileData.toString().split(“\n”);
ipAddress=staticProperties[Constants.IP];
port=staticProperties[Constants.port];
datasource_name=staticProperties[Constants.datasource];
Hashtable env=新的Hashtable();
环境put(Context.INITIAL\u Context\u工厂,Constants.JNDI\u工厂);
env.put(Context.PROVIDER_URL,“t3://”+ipAddress+:“+port);
datasource=(datasource)新建InitialContext(env).lookup(datasource\u名称);
if(数据源!=null){
connection=datasource.getConnection();
statement=connection.createStatement();
rset=语句.executeQuery(查询);
ResultSetMetaData rsetMetaData=rset.getMetaData();
while(rset.next()){
dataFromDB=newarraylist();
对于(int i=1;i我知道您想要从sql错误消息中提取表名,而不是如何运行sql。但是,如果有实例,您将能够做到这一点(至少不需要付出极大的努力,也不需要访问系统表,甚至可能不需要)您面临的基本问题是Oracle在语句中不是表级发布错误消息。请考虑以下事项:(这些都是在SQL开发人员中运行的,类似的消息发生的是SQL加,我建议您尝试通过java运行相同的结果,看看这些结果)
您是如何执行查询的?请输入代码,查看以下信息:@devpuh使用DataSource。如果您有普通sql查询,您只需打印即可those@VijayKumar编辑您的问题并添加导致问题的代码。java中可能有不同的方式来执行SQL查询,因此请添加您的代码,否则我们无法在这里帮助您。
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);
}
}
}
create table table_that_exists ( te_id integer, description varchar2(200) );
/*
Table TABLE_THAT_EXISTS created.
*/
insert into able_that_exists( id, description) values (1, 'test1') ;
/*
Error starting at line : 6 in command -
insert into able_that_exists( id, description) values (1, 'test1')
Error at Command Line : 6 Column : 13
Error report -
SQL Error: ORA-00942: table or view does not exist
00942. 00000 - "table or view does not exist"
*Cause:
*Action:
The lines "Error starting ... Error Report" are added by my development environment.
I'm not all that familier with Java or what ever you useing to connect. But I would
guee hat all you'll ge back is:
SQL Error: ORA-00942: table or view does not exist
00942. 00000 - "table or view does not exist"
*/
insert into table_that_exists( id, description) values (1, 'test1') ;
/*
Error starting at line : 18 in command -
insert into table_that_exists( id, description) values (1, 'test1')
Error at Command Line : 18 Column : 32
Error report -
SQL Error: ORA-00904: "ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Again I would guess all you'll get back in Jave is
SQL Error: ORA-00904: "ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*/
insert into table_that_exists( te_id, description) values (1, 'test1') ;
/*
1 row inserted.
No message returned to Java (?)
*/
select * from table_that_exists where te_id = 2;
/*
results in an 'empty' result' set. IE the values returned for td_id, description is NULL, NULL
but no error generated
No message returned to Java (?)
*/
-- finally:
select *
from table_that_exists te
join table_tha_does_not_exist tne
on te.te_id = tne.te_id ;
/*
ORA-00942: table or view does not exist
00942. 00000 - "table or view does not exist"
*Cause:
*Action:
Error at Line: 44 Column: 8
Even my 'nice friendly' development environment doesn't tell me which table.
NOTE: You will get the same message if all tables do exist but you have not been granted access.
*/