package com.ist.common.util; import java.sql.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class JdbcOracleTest { //orcl爲oracle數據庫中的數據庫名,localhost表示鏈接本機的oracle數據庫 //1521爲鏈接的端口號 private static String url="jdbc:oracle:thin:@192.168.99.98:1521:orcl"; //system爲登錄oracle數據庫的用戶名 private static String user="nms09"; //manager爲用戶名system的密碼 private static String password="nms09"; public static Connection conn; public static PreparedStatement ps; public static ResultSet rs; public static Statement st ; //鏈接數據庫的方法 public void getConnection(){ try { //初始化驅動包 Class.forName("oracle.jdbc.driver.OracleDriver"); //根據數據庫鏈接字符,名稱,密碼給conn賦值 conn=DriverManager.getConnection(url, user, password); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } //測試可否與oracle數據庫鏈接成功 public static void main(String[] args) { JdbcOracleTest basedao=new JdbcOracleTest(); basedao.getConnection(); if(conn==null){ System.out.println("與oracle數據庫鏈接失敗!"); }else{ System.out.println("與oracle數據庫鏈接成功!"); } } }