File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes SQL exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "SQL exception" Watch "SQL exception" New topic

SQL exception

lokesh rajarathnam
Ranch Hand

Joined: May 17, 2007
Posts: 35
Hi all.,

I strucked in data storing to database, please push me out or this hurdel
While running this program I encounter the exception
java.sql.sqlException:[Microsoft Odbc driver for oracle][Oracleora-401:unique constraint (SCOTT.NN) violated.

import java.sql.*;

public class databaseconnecting

public databaseconnecting()
public String EFname=null;
public String EAddress=null;
public int Eage=0;

public dataconnect()
{ databaseconnecting dbc=new databaseconnecting();

Connection con=DriverManager.getConnection ("jdbcdbc:MYDSNname","scott","tiger");
Statement stmt=con.createStatement();
int count=stmt.executeUpdate("insert into tablename (name,age,address) values"+" ('"+dbc.getName()+"',"+dbc.getAge()+",'"+dbc.getAddress+"')");
System.out.println("count=" +count);
catch(Exception ex)
{ System.out.println("connection faild");

public String getName()
{ EFname="myname";
return EFname;
public int getAge()
return Eage;
public String getAddress()
return EAddress;

public Static void main(String args[])
databaseconnecting dbc=new databaseconnecting();
dbc.dataconnect() ;

Issue: The values are not inserted in to the database(oracle).
Abhishek Asthana
Ranch Hand

Joined: Sep 08, 2004
Posts: 146
Please give the stacktrace. Also looking at the exception I think you are violating the unique constraint, meaning: You are trying to insert a duplicate value in a column which is meant to store only unique entries.

It is possible that if you are trying to generate values programatically your code is not working properly and generating duplicate values. Or if you are giving values manually you are giving test input which is already present in table.
Paul Campbell
Ranch Hand

Joined: Oct 06, 2007
Posts: 338
query user_cons_columns for the columns in your table... it is probably name... if you really want to keep it as a unique constraint, you need to catch the error and update the row with the new information (if there is new information).
I agree. Here's the link:
subject: SQL exception
It's not a secret anymore!