Question : Best Practice for seeing if a value already exists prior to trying to do an INSERT?

Following is a sample of what I am doing to check to see if a value exists prior to trying to insert it - I would appreciate hearing if there is a best practice for this or whether or not my code is inefficient.  I have other values to check so I'd like to handle it the most efficent way for all of them.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
private boolean existsDescriptor(String descriptor) throws java.sql.SQLException
{
	String sqlStatement = "SELECT descriptor_id FROM descriptors WHERE descriptor_name = \"" + descriptor + "\"";
	Statement stmt = conn.createStatement();
	ResultSet rs = stmt.executeQuery(sqlStatement);
		
	if(rs.next())
	{
		System.out.println (rs.getInt(1));
		System.out.println (rs.getString(1));
		rs.close();
		stmt.close();
	
		return true;
	}
	
	rs.close();
	stmt.close();
	
	return false;	
}

Answer : Best Practice for seeing if a value already exists prior to trying to do an INSERT?

just use:

INSERT IGNORE INTO mytable .....
Random Solutions  
 
programming4us programming4us