jdbc連線orcal資料庫
阿新 • • 發佈:2018-12-03
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBTools { // 定義一個方法,用來得到一個"新的"連線物件 public static Connection getConnection() { Connection conn = null; String driverName = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:ora9i"; String userName = "scott"; String passWord = "tiger"; try { Class.forName(driverName); conn = DriverManager.getConnection(url,userName,passWord ); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void closeConn(Connection conn) { try { if(conn != null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeState(Statement state) { try { if(state != null) { state.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeRs(ResultSet rs) { try { if(rs != null) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }