標籤:cat resultset 關閉 .sql ret tco 資料庫連接 tst generated
package Util;import java.sql.*;public class DBUtil { public static Connection getConnection() { Connection conn = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); //載入資料庫驅動 System.out.println("資料庫驅動載入成功!"); //輸出的資訊 String url = "jdbc:oracle:thin:@localhost:1521:orcl"; //擷取串連URL String user = "TEACHER"; //串連使用者名稱 String password = "***"; //串連密碼 Connection con = DriverManager.getConnection(url, user, password); //擷取資料庫連接 if (con != null) { System.out.println("成功的與Oracle資料庫建立串連!!"); } } catch (Exception e) { e.printStackTrace(); } return conn; //返回Connection執行個體 } //關閉資源的方法 public static void close(Connection connection ) { try { if (connection != null) { connection.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(PreparedStatement preparedStatement ) { try { if (preparedStatement != null) { preparedStatement.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(ResultSet resultSet ) { try { if (resultSet != null) { resultSet.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
Java串連Oracle代碼