方法1:通过纯java数据库驱动程序
一、java 环境变量
java_home C:\java\jdk1.6.0_30
classpath .;C:\Java\jdk1.6.0_30\lib\tool.jar;C:\Java\jdk1.6.0_26\lib\dt.jar
path C:\java\jdk1.6.0_30\bin
二、把ojdbc14.jar 拷贝到 c:\java\jre6\lib\ext下
三、程序1,2放于c:\下,编译生成to_oracle2.class和to_oracle22.class也在c:\ 都能成功访问
运行java to_oracle2 java to_oracle22 都能成功访问
程序1: to_oracle2.java
import java.sql.*;
public class to_oracle2 {
static Connection conn = null;
static ResultSet rs = null;
static {
try{ Class.forName("oracle.jdbc.driver.OracleDriver");}
catch(ClassNotFoundException e)
{ e.printStackTrace();}
}
public static ResultSet executeQuery(String sql) {
rs = null;
try {
conn=DriverManager.getConnection("jdbc:oracle:thin:@10.77.48.9:1521:zlj","lyz","lyz");
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
public static void main (String[] args)
{
try
{
String sql="select * from work_log";
ResultSet rs1=executeQuery(sql);
while (rs1.next())
{System.out.println(rs1.getString(1));}
}
catch (SQLException e) { }
}
}
--------程序2: to_oracle22.java
import java.sql.*;
public class to_oracle22{
Connection con =null;
Statement sql=null;
ResultSet rs=null;
{
try{ Class.forName("oracle.jdbc.driver.OracleDriver");}
catch(ClassNotFoundException e)
{ e.printStackTrace();}
try {con=DriverManager.getConnection("jdbc:oracle:thin:@10.77.48.9:1521:zlj","lyz","lyz");}
catch(SQLException e)
{ e.printStackTrace();}
}
public void query() {
try{
sql=con.createStatement();
rs=sql.executeQuery("select * from work_log");
while(rs.next())
{
System.out.println(rs.getString(1));
}
con.close();
}
catch (SQLException e)
{
}
}
public static void main(String args[]){
to_oracle22 o= new to_oracle22();
o.query();
}
}
方法2:通过odbc数据源
-程序3,程序4放于c:\下,编译后的class文件也位于c:\下
java to_oracle0
java to_oracle1
正常访问数据库
程序3:to_oracle0.java
import java.sql.*;
public class to_oracle0 {
static String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
static String sConnStr = "jdbc:odbc:zlj";
static Connection conn = null;
static ResultSet rs = null;
public to_oracle0() {
try { Class.forName(sDBDriver); }
catch(java.lang.ClassNotFoundException e)
{ System.err.println("faq(): " + e.getMessage()); }
}
public static ResultSet executeQuery(String sql) {
rs = null;
try {
conn = DriverManager.getConnection(sConnStr,"lyz","lyz");
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
public static void main (String[] args)
{
try
{
String sql="select * from work_log";
ResultSet rs1=executeQuery(sql);
while (rs1.next())
{System.out.println(rs1.getString(1));}
}
catch (SQLException e) { }
}
}
程序4:to_oracle1.java
import java.sql.*;
public class to_oracle1 {
static String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
static String sConnStr = "jdbc:odbc:zlj";
static Connection conn = null;
static ResultSet rs = null;
public to_oracle1() {
try { Class.forName(sDBDriver); }
catch(java.lang.ClassNotFoundException e)
{ System.err.println("faq(): " + e.getMessage()); }
}
public static ResultSet executeQuery(String sql) {
rs = null;
try {
conn = DriverManager.getConnection(sConnStr,"lyz","lyz");
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
public static void main (String[] args)
{
try
{
String sql="select * from work_log";
ResultSet rs1=executeQuery(sql);
while (rs1.next())
{System.out.println(rs1.getString(1));}
}
catch (SQLException e) { }
}
}