[RESOLVED] com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint-VBForums
Results 1 to 3 of 3

Thread: [RESOLVED] com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Resolved [RESOLVED] com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint

    Error on this line

    Code:
    int rowsAffected = statement.executeUpdate();
    With this code

    Code:
    int rowsAffected = statement.executeUpdate();
    		if(rowsAffected > 0) 
    		{
    			   JOptionPane.showMessageDialog(null, "Data Inserted successfully!");
    			}
    			else
    			{
    			    JOptionPane.showMessageDialog(null, "Data is not Inserted!", "Not successfully", JOptionPane.ERROR_MESSAGE);
    			}
    		statement.close();
    		con.close();
    Error.

    Code:
    com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint 'PK__Employer__6F09E6800F975522'. Cannot insert duplicate key in object 'dbo.Employer'.
    	at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
    	at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)
    	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404)
    	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
    	at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    	at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
    	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
    	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:314)
    But as I see in my Management Studio that the data has been saved.but still getting this error ..why?

  2. #2
    Addicted Member
    Join Date
    Oct 2008
    Location
    Califorina
    Posts
    235

    Re: com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constra

    You can't insert a duplicate value in your primary key field. That's the your error message is telling you, it's saying that the value you're attempting to insert already exists.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constra

    It was missing curly braces issue.

    It has been solved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width