I am writing a tool in Java that sends instructions to a database that are later launched. I am using JDBC to connect to the database. The database is Oracle 10g.
Before the instructions are written to the database, I want to analyze them to check when they will work later, and there will be no problems. I studied the use of the ANTLR solution for this, since there are grammars available, but of course, if I have a database connection, there should be a way to use the databases created in the parser.
So essentially my question is:
Is there a way to use JDBC I can make a database parser call by passing it an SQL statement and it will give me some feedback telling me if it was successful or any error messages?
Any help is appreciated, thanks a lot.
Edit:
Using connection.prepareStatement does not seem to work, for example, these results are successfully parsed!
String statement = "WHERE DISTINCT SELECT"; Connection connection; try { connection = this.controller.getDataSource().getConnection(); connection.prepareStatement(statement); connection.close(); mainPanel.setPositiveText("Parsed Successfully!"); } catch (Exception e) { mainPanel.setNegativeText("ERROR: " + e.getMessage()); return; }
The soul I used is as follows:
String statement = "DECLARE " + "myNumber NUMBER; " + "BEGIN " + "myNumber := SYS.dbms_sql.open_cursor; " + "SYS.DBMS_SQL.PARSE(myNumber, '" + text + "', SYS.DBMS_SQL.NATIVE); " + "END;"; Connection connection; try { connection = this.controller.getDataSource().getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(statement); preparedStatement.execute(); connection.close(); mainPanel.setPositiveText("Parsed Successfully!"); } catch (Exception e) { mainPanel.setNegativeText("ERROR: " + e.getMessage()); System.out.println(e.getMessage()); return; }
source share