当前位置: 技术问答>java相关
大侠解决一下java 如何和 oracle 进行连接??(高分—)
来源: 互联网 发布时间:2015-04-16
本文导语: 大侠解决一下java 如何和 oracle 进行连接??或推荐一本与此有关的书! | 关于这个问题,你先要清楚你想如何操作? 1、是应用程序,直接用jdbc连接,这样,你可以参考一下关于jdbc的连接...
大侠解决一下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();
}
}
}
好了,代码帖完了,好久没这样帖代码了:)
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();
}
}
}
好了,代码帖完了,好久没这样帖代码了:)