我使用以下代码连接到 Oracle 的数据库:
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
String jdbc_url = "jdbc:oracle:thin:hr/hr@localhost:1521:XE";
String query = "";
try {
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
conn = DriverManager.getConnection(jdbc_url);
stmt = conn.createStatement();
query = "select username " + " from users ";
rset = stmt.executeQuery(query);
// my codes
} catch (SQLException sqle) {
System.out.println("result error, " + e.getMessage());
} catch (NumberFormatException nfe) {
} finally {
try {
rset.close();
stmt.close();
conn.close();
} catch (Exception e) {
System.out.println("Error in closing " + e.getMessage());
}
}
我是否必须在为我想要的每个查询所做的每个操作中使用此代码?建立连接并终止它?每次?