Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL exception

 
lokesh rajarathnam
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic