using jdbc check for unique column

I 've got a table ,and I want to check for a unique column ,so that there is no dups in my mysql.So how is it done using jdbc ?
[142 byte] By [suse69a] at [2007-11-27 4:24:15]
# 1

There are 2 ways:

1) invoke a SELECT on those unique constraints. If one row is returned, you cannot insert.

2) insert anyway, catch the eventual exception and determine the SQL state and error code (may differ per DB).

The first way is the preferred way and less expensive.

BalusCa at 2007-7-12 9:32:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

This is my part code ,so how the code then?

/ Create PreparedStatement object

String sql = "INSERT INTO price VALUES (?, ?, ?, ?, ?)";

PreparedStatement pstmt = conn.prepareStatement(sql);

// Bind values to the parameters

pstmt.setInt(1,74);

pstmt.setInt(2, 1);

pstmt.setInt(3, 100);

pstmt.setDate(4, Date.valueOf("2005-05-05"));

pstmt.setDate(5, Date.valueOf("2005-05-06"));

String sql1 = "INSERT INTO price VALUES (?, ?, ?, ?, ?)";

PreparedStatement pstmt1 = conn.prepareStatement(sql1);

pstmt1.setInt(1, 70);

pstmt1.setInt(2, 2);

pstmt1.setInt(3, 200);

pstmt1.setDate(4, Date.valueOf("2005-06-07"));

pstmt1.setDate(5, Date.valueOf("2005-07-08"));

// check for Dup 慞C_ID?column unique

try{

String sql2= "select count(*) from price GROUP BY PC_ID HAVING count(*) > 1";

Statement stmt = conn.createStatement();

stmt.executeQuery(sql2);

ResultSet rs = stmt.executeQuery(sql2);

// Execute the query

while (rs.next()){

DupRow= rs.getInt(1);

}

System.out.println("This is unique because no Dup" + DupRow );

}

catch (SQLException s){

System.out.println("SQL statement is not executed!");

}

suse69a at 2007-7-12 9:32:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...