import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Application {public static Connection getConnection() {Connection conn = null;try { //连接driver为:oracle.jdbc.driver.OracleDriver//或者oracle.jdbc.OracleDriver,前者extends后者Class.forName("oracle.jdbc.OracleDriver");String url="jdbc:oracle:thin:@localhost:1521:orcl";String user="scott";String password="wjk139";try {//建立连接conn = DriverManager.getConnection(url,user,password);System.out.println("返回的conn值:"+conn);//用于在控制台输出返回的信息return conn;} catch (SQLException e) {e.printStackTrace();}} catch (ClassNotFoundException e) {e.printStackTrace();} finally {if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}return null;}public static void main(String[] args) {Connection conn = getConnection();if (conn != null) {System.out.println("连接成功");System.out.println("返回的值:"+conn);} else {System.out.println("连接失败");System.out.println("返回的值:"+conn);}}
}
如果访问成功,则会返回一串字符串,若不成功,则不会返回值
下面是连接成功与失败时的控制台界面
图1 连接成功的界面
图2 连接失败时的界面
以上就是对Oracle数据库成功与否的判断