java和oracle连接_大侠解决一下java 如何和 oracle 进行连接??(高分—)

大侠解决一下java 如何和 oracle 进行连接??或推荐一本与此有关的书!

|

关于这个问题,你先要清楚你想如何操作?

1、是应用程序,直接用jdbc连接,这样,你可以参考一下关于jdbc的连接方式,oracle分两种方式,一种是thin,或是oci方式,

A.thin方式,一般是一个driver,一个url即可。下面是oracle的例子:

/*

* This sample shows how to list all the names from the EMP table

*

* It uses the JDBC THIN driver.  See the same program in the

* oci8 samples directory to see how to use the other drivers.

*/

// You need to import the java.sql package to use JDBC

import java.sql.*;

class Employee

{

public static void main (String args [])

throws SQLException

{

// Load the Oracle JDBC driver

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

// Connect to the database

// You must put a database name after the @ sign in the connection URL.

// You can use either the fully specified SQL*net syntax or a short cut

// syntax as ::.  The example uses the short cut syntax.

Connection conn =

DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1721:sid",

"scott", "tiger");

// Create a Statement

Statement stmt = conn.createStatement ();

// Select the ENAME column from the EMP table

ResultSet rset = stmt.executeQuery ("select ENAME from EMP");

// Iterate through the result and print the employee names

while (rset.next ())

System.out.println (rset.getString (1));

}

}

B.是oci方式

/*

* This sample shows how to list all the names from the EMP table

*/

// You need to import the java.sql package to use JDBC

import java.sql.*;

class Employee

{

public static void main (String args [])

throws SQLException

{

// Load the Oracle JDBC driver

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

String url = "jdbc:oracle:oci8:@database";

try {

String url1 = System.getProperty("JDBC_URL");

if (url1 != null)

url = url1;

} catch (Exception e) {

// If there is any security exception, ignore it

// and use the default

}

// Connect to the database

Connection conn =

DriverManager.getConnection (url, "scott", "tiger");

// Create a Statement

Statement stmt = conn.createStatement ();

// Select the ENAME column from the EMP table

ResultSet rset = stmt.executeQuery ("select ENAME from EMP");

// Iterate through the result and print the employee names

while (rset.next ())

System.out.println (rset.getString (1));

// Close the RseultSet

rset.close();

// Close the Statement

stmt.close();

// Close the connection

conn.close();

}

}

2 如果你是在web应用中使用,可以直接用app server提供的连接池,比如oc4j中先设置一下:

程序中这样:

import java.io.*;

import java.sql.*;

import java.lang.*;

import java.util.Locale;

import java.sql.Connection;

import java.sql.SQLException;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.sql.DataSource;

import javax.ejb.EJBException;

public   class Strong {

public void DoSomeThingWithDatabase ()

{

//let’s go

try

{

InitialContext ic = new InitialContext();

DataSource ds = (DataSource)ic.lookup("jdbc/OracleDS");

Connection conn = ds.getConnection();

//...do some work...

Statement stmt=conn.createStatement();

ResultSet rs=stmt.executeQuery("select * from tree");

while(rs.next())

{

System.out.println(rs.getString("channelname"));

}

conn.close();

}catch(Exception e)

{

//System.out.println("Here have some eeor");

e.printStackTrace();

}

}

}

好了,代码帖完了,好久没这样帖代码了:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值