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 exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "SQL exception" Watch "SQL exception" New topic
Author

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();
try
{

Class.forname("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection ("jdbcdbc:MYDSNname","scott","tiger");
System.out.println("connected");
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()
{
Eage=22;
return Eage;
}
public String getAddress()
{
EAddress="bangalore";
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).
 
Consider Paul's rocket mass heater.
 
subject: SQL exception
 
Similar Threads
write a bytearray to an oracle db
Error in creating Instance
missing expression error
data feild storing in oracle database
Where to write this method in my application?