关于这个问题,你先要清楚你想如何操作?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 <host>:<port>:<sid>. 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中先设置一下: <data-source class="com.evermind.sql.DriverManagerDataSource" name="OracleDS" location="jdbc/OracleCoreDS" xa-location="jdbc/xa/OracleXADS" ejb-location="jdbc/OracleDS" connection-driver="oracle.jdbc.driver.OracleDriver" username="lili" password="lili" url="jdbc:oracle:thin:@192.168.0.19:1521:serverme" inactivity-timeout="30" />程序中这样: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(); } } }好了,代码帖完了,好久没这样帖代码了:)
不好意思,我使用OCI进行连接的时候,总是出现这样的错误。 jdbc:oracle:oci8:@192.168.0.196:1521:LMS Exception in thread "main" java.lang.UnsatisfiedLinkError: no ocijdbc9 in java.l ibrary.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1312) at java.lang.Runtime.loadLibrary0(Runtime.java:749) at java.lang.System.loadLibrary(System.java:820) at oracle.jdbc.oci8.OCIDBAccess.logon(OCIDBAccess.java:294) at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:287) at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.ja va:442) at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:321) at java.sql.DriverManager.getConnection(DriverManager.java:517) at java.sql.DriverManager.getConnection(DriverManager.java:177) at Employee.main(Employee.java:22) 如果使用Thin连接将不会出现错误。为什么。oci和thin连接方式有什么区别,哪一种更好
* 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 <host>:<port>:<sid>. 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中先设置一下:
<data-source
class="com.evermind.sql.DriverManagerDataSource"
name="OracleDS"
location="jdbc/OracleCoreDS"
xa-location="jdbc/xa/OracleXADS"
ejb-location="jdbc/OracleDS"
connection-driver="oracle.jdbc.driver.OracleDriver"
username="lili"
password="lili"
url="jdbc:oracle:thin:@192.168.0.19:1521:serverme"
inactivity-timeout="30"
/>程序中这样: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();
}
}
}好了,代码帖完了,好久没这样帖代码了:)
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","system","manager");在光盘上有一个sample目录,找一下,里面有例子.
package com.forlink.gaofeng;
import java.sql.*;public class Dboracle{
String sDBDriver="oracle.jdbc.driver.OracleDriver";
String sConnStr="jdbc:oracle:thin:@:地址1521:ora8";
String sUser="agent";
String sPassword="agent";
private Connection conn=null;
private PreparedStatement prestmt=null;
ResultSet rs = null;
public Dboracle(){
try {
Class.forName(sDBDriver);
conn=DriverManager.getConnection(sConnStr,sUser,sPassword);
}
catch(java.lang.Exception e){
e.printStackTrace();
System.err.println("Dboracle(): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql){
rs = null;
try{
prestmt=conn.prepareStatement(sql);
System.out.println("**************"+sql);
rs=prestmt.executeQuery();
}
catch(SQLException ex){
ex.printStackTrace();
System.err.println("aq.executeQuery:executeQuery " + ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql) {
prestmt = null;
rs=null;
try {
conn.setAutoCommit(false);
prestmt=conn.prepareStatement(sql);
System.out.println("**************"+sql);
prestmt.executeUpdate();
prestmt.close();
conn.commit();
}
catch(SQLException ex){
ex.printStackTrace();
System.err.println("aq.executeQuery:executeUpdate()" + ex.getMessage());
try{
conn.rollback();
}catch(SQLException e){
e.printStackTrace();
System.err.println("aq.executeQuery:rollback() " + e.getMessage());
}
}
}
public void closePrestmt(){
try{
prestmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
public void closeConn(){
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
我赶紧copy下来,留做资料
jdbcDriver = oracle.jdbc.driver.OracleDriver
String Url = jdbc:oracle:thin:@192.168.0.50:1521:数据库名
dbLoginID = scott
dbPassword = tiger
Class.forName(jdbcDriver);
conn = DriverManager.getConnection( jdbcUrl, dbLoginID, dbPassword );
jdbc:oracle:oci8:@192.168.0.196:1521:LMS
Exception in thread "main" java.lang.UnsatisfiedLinkError: no ocijdbc9 in java.l
ibrary.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1312)
at java.lang.Runtime.loadLibrary0(Runtime.java:749)
at java.lang.System.loadLibrary(System.java:820)
at oracle.jdbc.oci8.OCIDBAccess.logon(OCIDBAccess.java:294)
at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:287) at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.ja
va:442)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:321)
at java.sql.DriverManager.getConnection(DriverManager.java:517)
at java.sql.DriverManager.getConnection(DriverManager.java:177)
at Employee.main(Employee.java:22)
如果使用Thin连接将不会出现错误。为什么。oci和thin连接方式有什么区别,哪一种更好
OCI访问速度快,但是要安装客户端,thin小,速度略慢,不过也差不太大。
dbvis.url.1=jdbc:idb:<propertyFile>
dbvis.url.2=jdbc:mysql://<host>/<database>
dbvis.url.3=jdbc:sybase:Tds:<host>:<port2638>/<database>
dbvis.url.4=jdbc:db2://<host>/<database>
dbvis.url.5=jdbc:cloudscape:rmi:<dbFile>
dbvis.url.6=jdbc:jdbc:rmi:<host>/<dbURL>
dbvis.url.7=jdbc:HypersonicSQL:hsql://<host>:<port>
dbvis.url.8=jdbc:postgresql:net