ResultSet rs =pStmnt.executeQuery() vil returnere resultatsæt, der aldrig vil være null, før der ikke er nogen undtagelse, hvis du vil kontrollere, om poster er der, kan du bruge rs.next()-metoden og verificere, om der er nogen poster, der er der eller ej resultatsæt
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)
{
}
}
}
}