I am new MySQL user trying to write program that manipulates database through Java.
Have table named users with the following attributes | USERNAME | EMAIL | ROLE |, where USERNAME is PRIMARY KEY and rest of the attributes are NOT NULL.
Lets say I do INSERT INTO users, since there is Primary Key constrain on USERNAME MySQL should throw error. How this error will be throw and how do I catch it within JAVA ? Do I need to write Stored procedure for throwing errors or INSERT INTO statement has this capability as well?
PreparedStatementinstead of aStatement.mysql insert from javaand on the first page of results I found a good prepared statement example here. Downside: its exception handling is terrible. The first hit with decent exception handling is here. Between the two you'll figure it out. Finally, MySQL will throw when appropriate; you don't have to do anything special.