I am currently using MyBATIS in my project.
I sometimes need to run an INSERT query but ignore if the row I'm trying to insert is already present on DB, but instead handle other SQL errors.
How can I find that the SQLException is related to primary key violation?
Something like
try {
sqlMap.insert(query, params);
} catch (DuplicateKeyException ex) {
//Do nothing, it's OK for mew
} catch (SQLException ex) {
throw ex;
}
I suppose that specific exception doesn't really exist...
SQL isn't my strong point, but you could look into the Error Code and SQL State codes contained within the SQLException that would be thrown. Those may offer more insight into why the query failed.
Related
After I execute/compile a stored procedure/function, I query the all_statements view to extract the statements that were in the stored procedure/function like below:
select * from all_statements where owner = 'MY_USER' and object_name='MY_FUNCTION' and object_type='FUNCTION' order by line asc;
However, one of my applications run the following code:
try (Connection con = DriverManager.getConnection(url,username,password); Statement statement = con.createStatement();) {
statement.execute("ALTER FUNCTION MY_FUNCTION COMPILE");
} catch (Exception e) {
LOGGER.error("error while executing query: {}", query, e);
}
Whenever this code is run, the all_statements view is cleared. Even if I execute this same function like below:
try (Connection con = DriverManager.getConnection(url,username,password); Statement statement = con.createStatement();) {
statement.execute("SELECT MY_FUNCTION(123) FROM DUAL");
} catch (Exception e) {
LOGGER.error("error while executing query: {}", query, e);
}
There are no new entries in all_statements showing the statements that were within my MY_FUNCTION function
I need to log into SQL Developer and compile the function manually, for this to start working again.
What is the cause for this?
Any reason for this?
all_statements is populated by PL/Scope. By default, SQL Developer enables PL/Scope. By default, most other connections do not.
Within your JDBC session, you can enable PL/Scope
ALTER SESSION SET PLSCOPE_SETTINGS='IDENTIFIERS:ALL';
If you do so, compiling the procedure will cause all_statements to be populated.
Here is a dbfiddle that shows an example where I create a trivial function, show that neither creating nor compiling it populates all_statements, then enable PL/Scope, recompile it, and all_statements is populated.
I'm trying to do this at onUpgrade() in my Android Java App:
try {
db.execSQL("SELECT fechacontrol from parametres", null);
} catch(Exception e) {
e.printStackTrace();
db.execSQL("ALTER TABLE parametres ADD COLUMN fechacontrol BIGINT");
}
My problem is, the column fechacontrol exists, but I always end up in the exception block, then the app crashes because of a duplicated column name.
What am I doing wrong?
Thanks you all.
execSQL(anything, null) will throw an IllegalArgumentException due to null bindargs. That's why you always end up in the catch. There is execSQL(String) overload for executing SQL without bindargs.
However, database upgrades should not be done like this. The schema version number is stored in the database file and you get it as a param in onUpgrade(). Use that information to deduce what needs to be updated.
First of all I'm a begginer so please chill guyz. I'd like to create an app which allow us to get every one NOT UNIQUE row from first sqlite Table and place it to another table. So if the row already exist in the second table the program should increment index of row's ID. I mean sth like this e.g.
for(int i=0, i<10,i++){
query = "Select * from table where ID="+i;
executeQuery(query);
}
If the query cannot be executed I'm getting an exception like this one:
java.sql.SQLException :UNIQUE constraint failed: NewTableAUi.PHONE
I've got a little problem with catching an exception while the row I wanna insert is already exist. Thanks for all feedback!
So, if you are getting an exception, that means you need to handle it. Upon the query execution do something like this:
try {
//your query code
} catch (SQLException e) {
System.err.println("Exception Message");
}
we try to insert Primary Keys (Strings) in our MS SQL Database, sometimes the program will try to insert a PrimaryKey although this key is already existing in the db. Then the Java program will be stopped because of the exception which the MSSQL Server throws. How is it possible not to listen to this Exception in Java OR to disable this Message from the MSSQL Server?
We could check that in java before we try to insert but it would be better for the performance not to check every time, just throwing away the error-message would be fine!
Suppress the exception with a try statement:
try {
//.....code that inserts
}
catch (SQLException sqle) {
//ignore or do something else here
}
I am using JDBC, and am running a query. Is there a way to get the reason it failed from the SQLException object returned?
In particular, I want to know if my query violated a foreign key constraint (and which one), or a key constraint.
Would this result be vendor-specific? Just in case, I am using the postgresql-8.4-701.jdbc4.jar driver. If it is vendor-specific, where would I find the codes?
EDIT: I want to do this dynamically - i.e.
if(violated foreign key constraint on attribute x) {
return 5;
} else if (violated primary key constraint) {
return 7;
} else {
return 0;
}
Or something like that.
EDIT: According to this post, there are no vendor-specific error codes for PostgreSQL JDBC. Dunno if that's still valid.
You can process the error code from the SQLException object.
sqlException.getErrorCode()
This retrieves the vendor-specific exception code for this SQLException object. By using the vendor-specific error code, you can branch to the required block of code.
try printing the stack trace (e is of SQLException type)
e.printStackTrace()
or getting the Message
e.getMessage()
These may give you some explanation of why the query failed