I’m trying to solve this: 1.create a standalone application that inserts a couple of records into the ‘price’ table of the ‘fly-four-less’ database.
2. When you add the new records, make sure the ‘PC_ID’ column is unique by issuing a query first,
3.finding out the maximum ‘PC_ID’, and using the “maximum ‘PC_ID’ + 1” as the new ‘PC_ID’.
I’ve done the first part and trying do the 2rd part sorting out the unique part.
My question is what the best way the solve it,I’ve used the check duplicate method by count the dups in the PC_ID
column,Am I do it correctly?
part code:
import java.sql.Statement;
// 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!”);
}