从 SQL 失败中获取表

Posted

技术标签:

【中文标题】从 SQL 失败中获取表【英文标题】:Getting table from SQL failure 【发布时间】: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);
                    
              

       

【问题讨论】:

你是如何执行查询的? 请输入代码,看看这个信息: ***.com/help/how-to-ask @devpuh 使用数据源。 如果你有普通的 sql 查询,你可以简单地打印那些 @VijayKumar 编辑您的问题并添加导致问题的代码。 java中执行SQL查询的方式可能不同,所以请添加您的代码,否则我们无法帮助您。 【参考方案1】:

我了解您想从 sql 错误消息中提取表名,而不是如何让 sql 运行。但是,很少有实例,如果有的话,您将能够做到这一点(至少无需付出极大的努力和访问系统表 - 甚至可能不会)。您面临的基本问题是 Oracle 在语句而不是表级别发出错误消息。考虑以下情况:(这些都是在 SQL Developer 中运行的,类似的消息出现在 SQL Plus 中,我建议您尝试通过 Java 运行相同的结果并查看这些结果)

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.
*/ 

【讨论】:

以上是关于从 SQL 失败中获取表的主要内容,如果未能解决你的问题,请参考以下文章

如果存储过程失败,则在执行 SQL 任务中将输出变量值获取到 ssis 变量中

从 SQL 代理运行时 SSIS 脚本任务失败

从SQL代理作业调用时,工作存储过程失败

Bug解决:获取JDBC连接失败;嵌套异常是java.sql.SQLException:无法从底层数据库获取连接

pl/sql 过程中的排序表失败

使用带有 Azure 链接表的 Access 插入 SQL 失败