如果数据库中不存在作者,我试图让这个函数只插入作者。
如果作者已经在'authors'表中(意思是相同的名字和相同的姓),那么我希望这个函数不插入作者但我还是希望它返回作者的ID而我不确定怎么做。
这是我的insertAuthor函数,即使他已经存在,也会插入作者:
public static int insertAuthor(Connection conn, Author author) throws SQLException
{
ResultSet keys = null;
int authorId;
// Insert the new Authors into the authors table
String sqlInsertAuthor = "INSERT into authors"
+ "(author_firstname, author_lastname)"
+ " VALUES (?, ?)";
try (PreparedStatement statement = conn.prepareStatement(sqlInsertAuthor, Statement.RETURN_GENERATED_KEYS);)
{
statement.setString(1, author.getFirstName());
statement.setString(2, author.getLastName());
// Execute and return number of rows affected
int affectedRows = statement.executeUpdate();
// The number of affected rows should be equal to 1
if (affectedRows == 1)
{
keys = statement.getGeneratedKeys();
keys.next();
authorId = keys.getInt(1);
}
else
{
return 0;
}
}
catch (SQLException e)
{
System.err.println("ERROR INSERTING AUTHOR: " + e.getMessage());
return 0;
}
return authorId;
}
答案 0 :(得分:0)
嗯,有几种方法可以做到这一点。
您可以使用hibernate和它的二级缓存来加载所有作者并检查名字和姓氏。
您可以将lastname设置为唯一 - 如果lastname已存在,则会引发错误。
您可以手动执行 - 只需检查名字和姓氏是否已存在(选择first name和lastname的计数)。如果0添加新行。
等等。
如果你想要id,那么最后一个解决方案可能是最好的:而不是计数你可以选择in - 如果没有,则会有null。
答案 1 :(得分:0)
好的,当我尝试kamirru的选择计数时,我设法得到了我想要的东西。
以下是工作代码:
public static int insertAuthor(Connection conn, Author author) throws SQLException
{
ResultSet keys = null;
int authorId;
// Insert the new Authors into the authors table
String sqlInsertAuthor = "INSERT into authors"
+ "(author_firstname, author_lastname)"
+ " VALUES (?, ?)";
try (PreparedStatement statement = conn.prepareStatement(sqlInsertAuthor, Statement.RETURN_GENERATED_KEYS);)
{
// Check if author already exists, if yes return his id
authorId = authorExists(conn, author.getFirstName(), author.getLastName());
// If 0 is returned then the author does not exist so insert the author
if (authorId == 0)
{
statement.setString(1, author.getFirstName());
statement.setString(2, author.getLastName());
// Execute and return number of rows affected
int affectedRows = statement.executeUpdate();
// The number of affected rows should be equal to 1
if (affectedRows == 1)
{
keys = statement.getGeneratedKeys();
keys.next();
authorId = keys.getInt(1);
}
else
{
return 0;
}
}
}
catch (SQLException e)
{
System.err.println("ERROR INSERTING AUTHOR: " + e.getMessage());
return 0;
}
return authorId;
}
public static int authorExists(Connection conn, String firstName, String lastName) throws SQLException
{
int authorId = 0;
// Select the author to check if he exists
String sqlCountAuthor = "SELECT author_id"
+ " FROM authors"
+ " WHERE author_firstname = ? AND author_lastname = ?";
ResultSet rs = null;
try (PreparedStatement statement = conn.prepareStatement(sqlCountAuthor);)
{
statement.setString(1, firstName);
statement.setString(2, lastName);
rs = statement.executeQuery();
// If a result is returned then the author already exists so take his id
if (rs.next())
{
authorId = rs.getInt("author_id");
}
}
catch (SQLException e)
{
System.err.println("ERROR SELECTING AUTHOR: " + e.getMessage());
}
finally
{
if (rs != null)
{
rs.close();
}
}
return authorId;
}