Java连接oracle数据库


     Java连接oracle数据库有好几种方式,可以通过设置电脑上的ODBC数据源(这个是windows提供的连数据库的数据源)连接,也可以直接用oracle的thin方式连:
 

 Windows下采用JDBC-ODBC Bridge连接oracle数据库

  1、 安装oracle客户端程序,在tnsnames.ora中配置好相应的数据库连接串文件,此处设连接串名为“dbora”。

  2、 在windows的控制面板->“数据库源ODBC”中,建立相应的用户或者系统DSN,具体方法:在安装的数据源的驱动程序选择“Microsoft ODBC for Oracle”。

  点击“完成”,在弹出的对话框中,填入如下信息:

  数据源名称:dbjdbc
  描述:jdbc数据源
  用户名称:manager -此为数据库用户名
  服务器:dbora -此即为连接串名
  其中“dbjdbc”是在java程序中要引用的名字,至此数据源dbjdbc已建立。

  3、 建立如下的java程序

view plaincopy to clipboardprint?
// 使用本地的jdbc连接串,查询oracle数据库表   
import java.sql.*;   
public class lookup {   
public static void main(String[] args)   
throws SQLException, ClassNotFoundException {   
//定义了数据库连接串   
String dbUrl = "jdbc:odbc:dbjdbc";   
//数据库的用户名   
String user = "manager";   
//数据库的用户口令   
String password = "ora912";   
// 加载jdbc-odbc bridge驱动程序   
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");   
// 与url指定的数据源建立连接   
Connection c = DriverManager.getConnection(dbUrl, user, password);   
//采用Statement进行查询   
Statement s = c.createStatement();   
ResultSet r = s.executeQuery("SELECT empno,name from emp");   
while(r.next()) {   
// 打印字段信息   
System.out.println(r.getString("empno") + ",   
" + r.getString("name ") );   
}   
// 关闭Statement,其上的ResultSet也将关闭   
s.close();   
}   
}  
// 使用本地的jdbc连接串,查询oracle数据库表
import java.sql.*;
public class lookup {
public static void main(String[] args)
throws SQLException, ClassNotFoundException {
//定义了数据库连接串
String dbUrl = "jdbc:odbc:dbjdbc";
//数据库的用户名
String user = "manager";
//数据库的用户口令
String password = "ora912";
// 加载jdbc-odbc bridge驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// 与url指定的数据源建立连接
Connection c = DriverManager.getConnection(dbUrl, user, password);
//采用Statement进行查询
Statement s = c.createStatement();
ResultSet r = s.executeQuery("SELECT empno,name from emp");
while(r.next()) {
// 打印字段信息
System.out.println(r.getString("empno") + ",
" + r.getString("name ") );
}
// 关闭Statement,其上的ResultSet也将关闭
s.close();
}


  在jdbc中查询的语句有3类:Statement、PreparedStatement、CallableStatement。

jdbc的thin方式

  此种方法不需要安装Oracle的客户端,也不需要配置odbc,故此种方法用得比较普遍。

  此方法在使用时需要将oracle的jar包加到classpath变量中,此包可以在oralce客户端程序的$ORACLE_HOME/jdbc/lib/classes12.jar找到。

view plaincopy to clipboardprint?
import java.sql.*;   
public class jdbcthin {   
//dbUrl数据库连接串信息,其中“1521”为端口,“ora9”为sid   
String dbUrl = "jdbc:oracle:thin:@10.10.20.15:1521:ora9";   
//theUser为数据库用户名   
String theUser = "sman";   
//thePw为数据库密码   
String thePw = "sman";   
//几个数据库变量   
Connection c = null;   
Statement conn;   
ResultSet rs = null;   
//初始化连接   
public jdbcthin() {   
try {   
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();   
//与url指定的数据源建立连接   
c = DriverManager.getConnection(dbUrl, theUser, thePw);   
//采用Statement进行查询   
conn = c.createStatement();   
} catch (Exception e) {   
e.printStackTrace();   
}   
}   
//执行查询   
public ResultSet executeQuery(String sql) {   
rs = null;   
try {   
rs = conn.executeQuery(sql);   
} catch (SQLException e) {   
e.printStackTrace();   
}   
return rs;   
}   
public void close() {   
try {   
conn.close();   
c.close();   
} catch (Exception e) {   
e.printStackTrace();   
}   
}   
public static void main(String[] args) {   
ResultSet newrs;   
jdbcthin newjdbc = new jdbcthin();   
newrs = newjdbc.executeQuery("select * from eventtype");   
try {   
while (newrs.next()) {   
System.out.print(newrs.getString("event_type"));   
System.out.println(":"+newrs.getString("content"));   
}   
} catch (Exception e) {   
e.printStackTrace();   
}   
newjdbc.close();   
}   


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/luckyp/archive/2009/01/17/3812899.aspx

posted @ 2009-09-24 17:38  mogu  阅读(1806)  评论(0编辑  收藏  举报