ITPub博客

首页 > 数据库 > Oracle > Connecting to Oracle through JDBC - Oracle 9i Java Programming

Connecting to Oracle through JDBC - Oracle 9i Java Programming

原创 Oracle 作者:jinava 时间:2005-01-09 03:50:33 0 删除 编辑
DBConnection.java

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

public class DBConnection{
public static void main(String args[]){
if (args.length < 3){
System.out.println("Syntax: DBConnection [url] [username] [password]");
System.exit(-1);
}

String urlurl = args[0];
String username = args[1];
String password = args[2];
// String sql = "select object_name,object_type from user_objects";
String sql = "select systimestamp object_name, sysdate object_type from dual";
String objName, objType;

try{
System.out.println("Step 01. Registering JDBC Driver");
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
System.out.println("Step 02. Establishing connection to: " + urlurl);
Connection con = DriverManager.getConnection(urlurl, username, password);
System.out.println(" . Connected to Oracle !");
System.out.println("Step 03. Creating SQL statement.");
Statement sta = con.createStatement();
System.out.println("Step 04. Executing SQL statement.");
ResultSet query = sta.executeQuery(sql);
System.out.println("Step 05. Printing results.");
while (query.next()){
objName = query.getString("object_name");
objType = query.getString("object_type");
System.out.println(" * " + objType + " * " + objName);
}
System.out.println("Step 06. Closing JDBC objects.");
query.close();
sta.close();
con.close();
}catch(SQLException e){
System.out.println("The following error occurred: " + e);
}
}
}



// use Thin Driver
java DBConnection jdbc:oracle:thin:@jinava:1521:jinava System linjin
// use OCI Driver
java DBConnection jdbc:oracle:oci8:@jinava System linjin




===================================================


DBConnectionInternal.java

import java.sql.*;

public class DBConnectionInternal{
public static void main(String args[]){

// String sql = "select object_name,object_type from user_objects";
String sql = "select systimestamp object_name, sysdate object_type from dual";
String objName, objType;

try{
System.out.println("Step 01. Getting default connection");
Connection con = new oracle.jdbc.OracleDriver().defaultConnection();
System.out.println("Step 02. Creating SQL statement.");
Statement sta = con.createStatement();
System.out.println("Step 03. Executing SQL statement.");
ResultSet query = sta.executeQuery(sql);
System.out.println("Step 04. Printing results.");
while (query.next()){
objName = query.getString("object_name");
objType = query.getString("object_type");
System.out.println(" * " + objType + " * " + objName);
}
System.out.println("Step 06. Closing JDBC objects.");
query.close();
sta.close();
con.close();
}catch(SQLException e){
System.out.println("The following error occurred: " + e);
}
}
}


// OCI driver
loadjava -user system/linjin@jinava DBConnectionInternal.class -verbose -resolve
// thin driver
loadjava -user system/linjin@jinava:1521:jinava DBConnectionInternal.class -verbose -resolve

create or replace procedure prConnection
as language java
name 'DBConnectionInternal.main(java.lang.String[])';

set serveroutput on size 5000
call dbms_java.set_output(5000);
exec prConnection

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/205087/viewspace-786702/,如需转载,请注明出处,否则将追究法律责任。

上一篇: Function Based Index
下一篇: utl_file_dir
请登录后发表评论 登录
全部评论
  • 博文量
    32
  • 访问量
    40421