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 ?
This is my part code
/ 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 ‘PC_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!");
}