JDBC連結Oracle資料庫的6個步驟
阿新 • • 發佈:2019-02-12
public class JDBC {
public static void main(String[] args) {
//1.註冊驅動器
Class.forName(oracle.jdbc.driver.OracleDriver);
//2.得到Connection
Connection conn = DriverManager.getConnection();
try {
//3.建立Statement
Statement stmt = conn.createStatement();
//4.得到結果集
ResultSet rs = stmt.executeQuery("Select * from t_student");
//5.對結果集進行處理(例如遍歷t_student表中的sname欄位)
while(rs.next()) {
System.out.println(rs.getString("sname"));
}
catch(SQLException e) {
e.printStackTrace();
}
//6.資源釋放
if(rs != null) {
try {
rs.close();
rs = null;
} catch(SQLException e) {
e.printStackTrace();
}
}
if(stmt != null) {
try {
stmt.close();
stmt = null ;
} catch(SQLException e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
conn = null;
} catch(SQLException e) {
e.printStackTrace();
}
}
}
}