参考 http://java.sun.com/docs/books/tutorial/jdbc/basics/index.html Java JDBC中操作Oracle数据库,经过以下几个步骤,
1.将OracleHome/jdbc/lib/目录的所有文件添加到jre/lib/ext目录;(配置Java JDBC驱动)
2.创建odbc源,在控制面板=》管理工具=》数据源(odbc)中添加DSN,比如取名为OracleDSN,选择一个Service,输入用户名密码,测试连接,若通过说明成功;
3.在程序中加载jdbc驱动(下面的例子用的是JdbcOdbc驱动),建立连接,执行Query.
下面是连接OracleDSN ODBC data source的一个类,方法Test()连接数据库后,读取tbljob的内容,并显示所有记录。
import java.sql.*;
class OracleConnect {
public static void Test() { //connection by JdbcOdbcDriver
try {
//load the driver:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//connect the Database,OracleDSN is the ODBC data source ;
String url = "jdbc:odbc:OracleDSN";
Connection conn = DriverManager.getConnection(url, "system",
"system_passwd");
// Create a Statement
Statement stmt = conn.createStatement();
// Select first_name and last_name column from the employees table
ResultSet rset = stmt.executeQuery("select * from tbljob;");
// Iterate through the result and print the employee names
while (rset.next())
System.out.println(rset.getString(1) + " " + rset.getString(2));
// Close the RseultSet2
rset.close();
// Close the Statement
stmt.close();
// Close the connection
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
【责任编辑:彭凡 TEL:(010)68476606】