ResultSet rs =pStmnt.executeQuery() akan mengembalikan hasil yang tidak akan pernah menjadi null sampai tidak ada pengecualian jika Anda ingin memeriksa apakah ada catatan, Anda dapat menggunakan metode rs.next() memverifikasi apakah ada catatan atau tidak. kumpulan hasil
public boolean createRecord(Myuser myuser)
{
Connection cnnct = null;
PreparedStatement pStmnt = null;
try
{
cnnct = getConnection();
String preQueryStatement
= "SELECT * FROM MYUSER WHERE MYUSER.USERID = ?;";
pStmnt = cnnct.prepareStatement(preQueryStatement);
pStmnt.setLong(1,youruserid);
ResultSet rs = pStmnt.executeQuery();
if (!rs.next())
{
String insertStatement
= "INSERT INTO MYUSER VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement ps = cnnct.prepareStatement(insertStatement);
ps.setString(1, myuser.getUserid());
ps.setString(2, myuser.getName());
ps.setString(3, myuser.getPassword());
ps.setString(4, myuser.getEmail());
ps.setString(5, myuser.getPhone());
ps.setString(6, myuser.getAddress());
ps.setString(7, myuser.getSecQn());
ps.setString(8, myuser.getSecAns());
System.out.println("new user inserted");
return true;
}
else
{
System.out.println("user already in data base");
return false;
}
}
catch (SQLException ex)
{
while (ex != null)
{
ex.printStackTrace();
ex = ex.getNextException();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
if (pStmnt != null)
{
try
{
pStmnt.close();
}
catch (SQLException e)
{
}
}
if (cnnct!= null)
{
try
{
cnnct.close();
}
catch (SQLException sqlEx)
{
}
}
}
}