/    Sign up×
Community /Pin to ProfileBookmark

Checking unique column

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!”);
}

to post a comment
Java

0Be the first to comment 😎

×

Success!

Help @gpo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...