您就可以自己看看结果如何了:
程序1:
/*
applet that reads in several rows from a remote oracle database and presents them in a listbox.
*/
import java.sql.*;
import java.awt.*;
import java.applet.*;
import java.lang.*;
public class jdbctest extends applet
{
file://single-select listbox
private list employee_list = new list(5, false)
public void init()
{
setlayout(new borderlayout());
try {
file://set up the oracle jdbc driver
drivermanager.registerdriver(new oracle.jdbc.driver.oracledriver());
file://connect to an oracle database on machine sforza, using username
file://"scott" and password "tiger"
connection db_conn = drivermanager.getconnection
("jdbc:oracle:thin:@sforza:1521:orcl", "scott", "tiger");
file://connection db_conn = drivermanager.getconnection
("jdbc:oracle:thin:scott/tiger@sforza:1521:orcl");
file://create a jdbc statement object
statement statement_obj = db_conn.createstatement();
file://execute a query to get the employee names from the test db
resultset results = statement_obj.executequery ("select ename from emp");
file://populate the list box with all the employee names
while (results.next ()){
employee_list.add(results.getstring(1));
}
} catch (sqlexception e) {
throw new runtimeexception("sql exception " + e.getmessage());
}
add(employee_list);
}
}
程序2:
oracle jdbc driver test
程序1的大部分都是与具体数据库无关的jdbc调用,依样画葫芦您也可以访问postgresql,主要和oracle相关的部分是:
connection db_conn = drivermanager.getconnection
("jdbc:oracle:thin:@sforza:1521:orcl", "scott", "tiger");
注意 oracle thin jdbc driver中uri的格式:
jdbc:oracle:thin:@::
如果您想在uri中包括用户名和口令的话:
jdbc:oracle:thin:username/password@::
同样的uri还有一个较长的格式:
"jdbc:oracle:thin:@(description=(address=(host= _ip>)(protocol
=tcp)(port=))(connect_data=(sid=)))"
注意orcl是系统安装的缺省sid(system identifier,系统标识符)。如果您在安装时使用了其他的sid,在uri里也用那个sid。$oracle_home.network/admin/tnsnames.ora包括了oracle服务器的端口和sid设置。这是tns listener,oracle网络连接进程的配置文件。如果您无法通过网络连接数据库的话,首先应该检查端口号和sid是否正确,tns listner是否正常(译注:lsnrctl status,或者tnsping)。jdbc并不是开发sql数据库应用的唯一手段,ansi标准sqlj允许在java程序中直接嵌入sql语句。oracle for linux 8.0.5并没有捆绑sqlj,但可以作为另一个独立产品获得。sqlj是100%的java,应该在linux上运行没有问题,事实上sqlj在底层使用的就是jdbc,因此可以同任何sql数据库互联,并不限于oracle。
希望上面的这些例子能对您的编程有所帮助。可惜oracle for linux 的编程文档相当的少,要不就是隐藏得十分深:) 也许在数据库编程得心应手之前,您得先啃它一两本书。
pl/sql
对sql的过程性语言扩展(pl/sql)可能是oracle最著名的特点。这种强大的专业数据库管理语言允许您对声明性的sql加逻辑控制。
一般,pl/sql的存储过程(stored procedure)用命令行的sqlplus,图形化的developer/2000(linux上目前还没有)或其他工具开发,允许您从oracle众多的查询接口(oracle application server, python, perl,jdbc 或者c)访问。
程序3:
create or replace package stored_sample as
function get_annual_salary (emp_number in number) return number;
end stored_sample;
/
create or replace package body stored_sample as
function get_annual_salary (emp_number in number) return number
is
annual_salary number;
monthly_salary number;
begin
select sal into monthly_salary from emp where empno = emp_number;
annual_salary := monthly_salary * 24;
return (annual_salary);
end get_annual_salary;
end stored_sample;
/
上面的程序3是存储进程的一个小示范,它接受一个员工号,返回该员工的年薪。相关的函数get_annual_salary封装在pl/sql包sample_package中。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器