File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sql integrity exception

 
nith thn
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using my java program i'm inserting email id,name to database.I hav set email id as primary key.So if the user is inputting a mail id that is already inserted an sql exception wil occur.Instead of showing that exception I just want to print like this"alresdy existing user".please help me if anyone knows the solution
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I guess it depends on the technology you use to access your database (JDBC, O/R mapping etc.) what kind of information you will get in case of a constraint violation. JPA, Hibernate and other O/R mappers usually tell you in a more convenient way what kind of constraint violation (i.e. duplicate key) occurred whereas you have to deal with more low-level details if you use a more low-level API like JDBC.

For example you can get some details out of the information encapsulated inside an ordinary SQLException. According to the documentation you can expect the following content although I must admit, I'm unsure how to figure out exactly what went wrong in case of a constraint violation:

Each SQLException provides several kinds of information:

a string describing the error. This is used as the Java Exception message, available via the method getMesasge.
a "SQLstate" string, which follows either the XOPEN SQLstate conventions or the SQL:2003 conventions. The values of the SQLState string are described in the appropriate spec. The DatabaseMetaData method getSQLStateType can be used to discover whether the driver returns the XOPEN type or the SQL:2003 type.
an integer error code that is specific to each vendor. Normally this will be the actual error code returned by the underlying database.
a chain to a next Exception. This can be used to provide additional error information.
the causal relationship, if any for this SQLException.


A cleaner way would probably be to check for existing database entries before you insert the new data records or check for return values the corresponding API methods offer you.

But as I said it depends on the API/technology you use. So to get more help, it would be helpful for us if you could tell us a little bit more details

Marco
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd strongly recommend against using an email address as a primary key but instead generated an internal GUID through the DB itself or another mechanism.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4116
195
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"nith thn", please check your private messages regarding an important administrative matter.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to JDBC.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic