欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java jdbc异常处理,jdbc异常处理,package cn.o

来源: javaer 分享于  点击 28520 次 点评:169

java jdbc异常处理,jdbc异常处理,package cn.o


package cn.outofmemory.snippets.core;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class SQLException {  public static void main(String[] args) {    Connection connection = null;    try {        // Load the Oracle JDBC driver        String driverName = "oracle.jdbc.driver.OracleDriver";        Class.forName(driverName);        // Create a connection to the database        String serverName = "localhost";        String serverPort = "1521";        String sid = "mySchema";        String url = "jdbc:oracle:thin:@" + serverName + ":" + serverPort + ":" + sid;        String username = "username";        String password = "password";        connection = DriverManager.getConnection(url, username, password);        System.out.println("Successfully Connected to the database!");    } catch (ClassNotFoundException e) {        System.out.println("Could not find the database driver " + e.getMessage());    } catch (SQLException e) {        while (e != null) {          // the reason for the exception          String message = e.getMessage();          // vendor-specific codes for the error           int errorCode = e.getErrorCode();          String sqlState = e.getSQLState();          // To execute code based on the specific error code we should first check the driver used          String driverName;          try {              driverName = connection.getMetaData().getDriverName();              if (driverName.equals("Oracle JDBC Driver") && errorCode == 123) {                // Process error specific to Oracle database ...              }            } catch (SQLException e1) {                System.out.println("Could not retrieve database metadata " + e1.getMessage());          }          // The exception may have been chained; process the next exception in the chain          e = e.getNextException();        }    } }}

输出:

Successfully Connected to the database!
相关栏目:

用户点评