File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes sql integrity exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "sql integrity exception" Watch "sql integrity exception" New topic
Author

sql integrity exception

nith thn
Greenhorn

Joined: Sep 19, 2010
Posts: 1
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

Joined: Mar 07, 2007
Posts: 1282

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

Joined: Sep 29, 2008
Posts: 12617

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
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3179
    
  49

"nith thn", please check your private messages regarding an important administrative matter.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

Moving to JDBC.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sql integrity exception