「Java」try-with-resources文のjava.sql.Statementオブジェクトの使い方
書式
try (Statement stmt = conn.createStatement();
ResultSet rslt = stmt.executeQuery(query);) {
//処理コード
}
Javaコード
public static void getEmpInfo(Connection con) throws SQLException {
String query = "SELECT USER_NAME, USER_ID, SALARY, AGE FROM EMP_TBL";
try (Statement stmt = conn.createStatement();
ResultSet rslt = stmt.executeQuery(query);) {
while (rslt.next()) {
String coffeeName = rs.getString("USER_NAME");
int userID = rs.getInt("USER_ID");
float salary= rs.getFloat("SALARY");
int age = rs.getInt("AGE");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + ", " + userID + ", " + salary +
", " + age );
}
} catch (Exception e) {
throw new SQLException("error",e);
}
}
public static void getEmpInfo(Connection con) throws SQLException {
String query = "SELECT USER_NAME, USER_ID, SALARY, AGE FROM EMP_TBL";
try (Statement stmt = conn.createStatement();
ResultSet rslt = stmt.executeQuery(query);) {
while (rslt.next()) {
String coffeeName = rs.getString("USER_NAME");
int userID = rs.getInt("USER_ID");
float salary= rs.getFloat("SALARY");
int age = rs.getInt("AGE");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + ", " + userID + ", " + salary +
", " + age );
}
} catch (Exception e) {
throw new SQLException("error",e);
}
}
public static void getEmpInfo(Connection con) throws SQLException { String query = "SELECT USER_NAME, USER_ID, SALARY, AGE FROM EMP_TBL"; try (Statement stmt = conn.createStatement(); ResultSet rslt = stmt.executeQuery(query);) { while (rslt.next()) { String coffeeName = rs.getString("USER_NAME"); int userID = rs.getInt("USER_ID"); float salary= rs.getFloat("SALARY"); int age = rs.getInt("AGE"); int total = rs.getInt("TOTAL"); System.out.println(coffeeName + ", " + userID + ", " + salary + ", " + age ); } } catch (Exception e) { throw new SQLException("error",e); } }