大家好,我有SWING应用程序。使用NetBeans8.2java 1.8.0_45构建,连接到Oracle11g。
这个应用程序由16个标签组成。每个选项卡连接到不同的表,并执行多个功能(添加记录、更新、搜索等)。
在我的DBConnection类中,我试图设置用户角色。
当我启动应用程序时,我得到了(设置角色不成功)消息,在应用程序连接之前打印了几次。也就是说它起作用了。
在调用应用程序中的一些功能时,我得到了相同的信息。任何人都可以告诉我我在哪里犯了错误,或者如何改进类以消除这个错误?
附加如果DBConnection类。
public class DBConnection {
public static Connection getConnection() throws Exception {
Connection conn = null;
ReadPropertyFile data = new ReadPropertyFile();
DecryptCalss decr = new DecryptCalss();
try {
Class.forName("oracle.jdbc.OracleDriver");
//Getting connection info
conn = DriverManager.getConnection(data.getUrl(), data.getUser(), decr.decrypt(data.getEPassword()));
} catch (SQLException sqle) {
out.println("SQLException: Unable to open connection to db: " + sqle.getMessage());
throw sqle;
} catch (Exception e) {
out.println("Exception: Unable to open connection to db: " + e.getMessage());
throw e;
}
try {
String sql = "SET ROLE HIVP_WRITE";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet result = ps.executeQuery();
if (result.next()) {
out.println("Role successfully set");
} else {
out.println("Setting Role is not successful");
}
} catch (Exception e) {
out.println("Error while setting role: " + e);
}
return conn;
}
public static void executeUpdate(String strQuery) throws Exception {
try (Connection conn = getConnection()) {
Statement stmt = conn.createStatement();
stmt.executeUpdate(strQuery);
} catch (SQLException sqle) {
out.println("SQLException: Unable to execute query : " + strQuery);
throw sqle;
} catch (Exception e) {
out.println("Exception: Unable to execute query: " + e);
throw e;
}
}
public static void executeQuery(String strQuery) throws Exception {
try (Connection conn = getConnection()) {
Statement stmt = conn.createStatement();
stmt.execute(strQuery);
} catch (SQLException sqle) {
out.println("SQLException: Unable to execute query : " + strQuery);
throw sqle;
} catch (Exception e) {
out.println("Exception: Unable to execute query: " + e);
throw e;
}
}
//CLOSE CONNECTION
public static void closeConnection(Connection conn) {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException sqle) {
out.println("Error while closing connection: "+sqle);
}
}
}发布于 2016-01-04 14:30:51
//Solved the issue by surrounding the if Statement by while loop
try {
String sql = "SET ROLE HIVP_WRITE";
Statement stmt = null;
stmt = conn.createStatement();
ResultSet result = stmt.executeQuery(sql);
while (result.next()) {
if (result.next()) {
out.println("ROLE SUCCESSFULLY SET");
} else {
out.println("ROLE IS NOT SET");
}
}
} catch (Exception e) {
out.println("ERROR WHILE SETTING ROLE: " + e);
}发布于 2015-12-23 19:26:58
在您的示例中,SQL语句返回一个空的ResultSet,因此,result.next()返回false。
SQL 执行,但ResultSet只是空的。
例如,如果要执行"select“语句,那么ResultSet将包含查询返回的任何数据。
下面是可用的execute方法,每个函数如下:
execute:如果查询返回的第一个对象是ResultSet对象,则返回true。如果查询可以返回一个或多个ResultSet对象,则使用此方法。通过反复调用ResultSet检索从查询返回的Statement.getResultSet对象。executeQuery:返回一个ResultSet对象。executeUpdate:返回一个整数,表示受SQL语句影响的行数。如果使用INSERT、DELETE或UPDATE SQL语句,请使用此方法。这一点在Oracle JDBC指南中作了进一步解释,该指南可在此处获得:
https://stackoverflow.com/questions/34441983
复制相似问题