Saturday, 15 June 2013

MySQL and Java - throwing exception when inserting into the table -


I am a new MySQL user who is trying to write a program that manages the database through Java.

Code> User with the following attributes: | USERNAME | EMAIL | ROLE | , where is the USERNAME key and the remaining attributes are NOT NULL .

Let's say, I enter user , because there is a lack of primary keys on USERNAME , MySQL should throw error. How will this error be thrown and how can I catch it in Java? Do I need to write the stored procedure to throw errors, or is this capability even in the INSERT INTO statement?

If the primary key is violated then SQLException with that message Things that will be thrown into the name of the compulsion of infringement can be seen in dealing with your exception: try {// insert}} (if eclipse) E.getMessage ()) contains ("MY_CONSTRAINT_NAME")) / duplicate key} other {// some other error}}

an option to SQLException.getSQLState () Received SQLException.getErrorCode () to the Czech .

I would recommend that anyone who needs to be careful and usually needs to reduce the amount of JDBC code.

No comments:

Post a Comment