JDBC异常处理:警告抑制 [英] JDBC exception handling: warning suppression
问题描述
我目前正在学习一些基本的Java SQL编码,为我的SQL项目制作了一个基本的终端UI.我一直在使用PostgreSQL
I am currently learning some basic java SQL coding, making a basic terminal UI for my SQL project. I have been using PostgreSQL
我正在使用PreparedStatement
来确保自己免受SQL注入的伤害,总比后悔要安全. PreparedStatement
似乎总是出于某种原因发出警告,我认为这是异常的子类(应该被异常捕获).
I am using PreparedStatement
to ensure myself from SQL injections, better be safe than sorry. PreparedStatement
seems to always fire warnings for some reason, which I figured is a subclass to exceptions (and should get caught in the exceptions).
在我的SQL触发器和函数中,我创建并测试了我应该触发异常的情况,这些异常都可以正常工作并捕获catch块.
In my SQL triggers and functions I have created and tested the cases where I should fire exceptions, and they are all getting and working properly catch block.
我猜想使用@Supresswarnings
让编译器知道我想抑制我的警告,但是我可能想在catch块中捕捉一些警告,所以我可能正在寻找其他解决方案.
I guess using @Supresswarnings
to let the compiler know I want to do suppress my warnings, but I might want to catch some warnings in the catch block, so I might be looking for a different solution.
问题/问题是:
- 我想将我的照片放在try块中,这没有引发异常.
- 在try块中执行
preparedStmt.executeQuery
后,如何进行打印? - 在处理这样的异常处理时,通常使用
@SuppressWarnings
被认为是良好的编码习惯吗? - 如果我对
preparedStatement.executeQuery()
总是发出警告不正确,那么SQL语言中的警告被视为什么?
- I would like to have my prints in the try block, that is no exceptions were fired.
- What could be done to have my prints after the execution of
preparedStmt.executeQuery
in the try block? - Is using
@SuppressWarnings
usually considered good coding practice when dealing with exception handling like this? - If I am incorrect about
preparedStatement.executeQuery()
always fires warnings, what is considered warnings in SQL language?
我的代码:
void registerStudent(Connection conn, String toBeInserted, String insertedTo)
throws SQLException{
ResultSet res;
String query = "INSERT INTO Registrations VALUES (?, ?)";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, toBeInserted);
ps.setString(2, insertedTo);
try {
ps.executeQuery();
}catch (SQLException e) {
SQLWarning warning = ps.getWarnings();
if (warning != null){
System.out.println("Yay, insert succeeded!
values: "+ toBeInserted +" were inserted into
"+insertedTo);
}else {
System.out.println("Nothing inserted, here goes the big exception SQL fired for ya'll: "
+ e.getMessage());
}
}
}
推荐答案
仍然存在一些缺陷.我还添加了AUTOINCREMENT主键的情况-您可以在插入 后检索它.
There are still some flaws. I added also the case of an AUTOINCREMENT primary key - which you can retrieve after inserting.
缺陷:
- 列出您要插入的字段,这样以后在数据库中添加一列就不会引起问题,并且在代码中列的顺序也很清楚.
- 使用try-with-resources关闭语句,结果集等,即使其中一个返回或抛出异常也是如此.
- 将executeUpdate用于INSERT(和类似的修改操作),以返回更新计数.
- 您捕获了SQLException,然后应重新抛出该SQLException,以使调用方确定在严重失败时应采取的措施.
- SQLWarnings(实际的SQL和SQLException也是如此)可能很有趣, 尽管我并不经常看到SQLWarnings被治疗.对于这种简单的情况,也许会忘记它,而是先在单独的SQL工具中尝试新的SQL语句.
- List which fields you insert, such that a later addition of a column in the database does not cause problems, and the order of columns is clear in the code.
- Use try-with-resources to close statement, result set and such, even when one returns inside, or an exception is thrown.
- Use executeUpdate for INSERT (and similar modifying operations) returning an update count.
- You catched the SQLException, which then should be rethrown, to let the caller determine what to do on gross failure.
- SQLWarnings (as does the actual SQL and the SQLException) might be interesting, though I not often see SQLWarnings being treated. Maybe forget it for such simple case, and instead try new SQL statements first in a separate SQL tool.
所以:
/**
* @return the generated primary key, the Student ID.
*/
int registerStudent(Connection conn, String toBeInserted, String insertedTo)
throws SQLException {
String query = "INSERT INTO Registrations(ToBeInserted, InsertedInto) VALUES(?, ?)";
try (PreparedStatement ps = conn.prepareStatement(query),
Statement.RETURN_GENERATED_KEYS) {
ps.setString(1, toBeInserted);
ps.setString(2, insertedTo);
int updateCount = ps.executeUpdate();
if (updateCount == 1) {
try (ResultSet rs = ps.getGeneratedKeys()) {
if (rs.next()) {
int id = rs.getInt(1);
return id;
}
}
throw new SQLException("No primary key generated");
} else {
SQLWarning warning = ps.getWarnings();
Logger.getLogger(Xyz.class.getName()).info("Not added, values: "
+ toBeInserted + " were not inserted into "+ insertedTo + ": " + warning);
throw new SQLException("Not added");
}
}
}
这篇关于JDBC异常处理:警告抑制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!