课外天地 李树青学习天地JavaEE网站开发课件 → 连接Oracle的方法(以JSP为例)


  共有10064人关注过本帖树形打印复制链接

主题:连接Oracle的方法(以JSP为例)

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1847 积分:24934 威望:0 精华:34 注册:2003/12/30 16:34:32
连接Oracle的方法(以JSP为例)  发帖心情 Post By:2012/4/22 20:39:28 [只看该作者]

1、ODBC桥
数据准备:
CREATE TABLE student
(
        "NUMBER" varchar2(6) NOT NULL,
        name varchar2(8) NULL,
        sex number(1),
        birthday timestamp NULL,
        height number(3,2) NULL
);

insert into student ("NUMBER",name,sex,birthday,height)  values ( '000001','黎明',1,TO_DATE('15-4月 -1990','dd-Mon-
yyyy'),1.78);
insert into student ("NUMBER",name,sex,birthday,height)  values ( '000002','赵怡春',0,TO_DATE('17-12月 -1992','dd-Mon-
yyyy'),1.77);
insert into student ("NUMBER",name,sex,birthday,height)  values ( '000003','张富平',1,TO_DATE('1-2月 -1995','dd-Mon-
yyyy'),1.78);
insert into student ("NUMBER",name,sex,birthday,height)  values ( '000004','白丽',0,TO_DATE('20-11月 -1996','dd-Mon-
yyyy'),1.73);
insert into student ("NUMBER",name,sex,birthday,height)  values ( '000005','牛玉德',1,TO_DATE('1-5月 -1993','dd-Mon-
yyyy'),1.74);
insert into student ("NUMBER",name,sex,birthday,height)  values ( '000006','姚华',0,TO_DATE('12-5月 -1996','dd-Mon-
yyyy'),1.77);
insert into student ("NUMBER",name,sex,birthday,height)  values ( '000007','胡歌',1,TO_DATE('30-6月 -1995','dd-Mon-
yyyy'),1.77);


建立数据源,选择Oracle in Ora10gDb_home1

代码:
<%@ page c%>
<%@page import="java.sql.*"%>
<html>
<head>
</head>

<body>
<%
        Connection con;
        Statement stm;
        ResultSet res;
        try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con = DriverManager.getConnection("jdbc:odbc:StuSQL","system","sys");
                stm = con.createStatement();
                res = stm.executeQuery("select * from student");

                while (res.next()) {
                        out.print(res.getString(1) + "<br>");
                }
                con.close();
        } catch (Exception ex) {
                System.out.println(ex.getMessage());
        }
%>
</body>
</html>

2、专用驱动
将ojdbc14.jar文件放入lib中

代码:
<%@ page c%>
<%@page import="java.sql.*"%>
<html>
<head>
</head>

<body>
<%
        Connection con;
        Statement stm;
        ResultSet res;
        try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","system","sys");
                stm = con.createStatement();
                res = stm.executeQuery("select * from student");

                while (res.next()) {
                        out.print(res.getString(1)+"<br>");                    
                }
                con.close();
        } catch (Exception ex) {
                System.out.println(ex.getMessage());
        }
%>
</body>
</html>

 

 

更多可见:http://blog.csdn.net/m13666368773/article/details/7234171

 

 

 


 


 回到顶部
帅哥哟,离线,有人找我吗?
admin
  2楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1847 积分:24934 威望:0 精华:34 注册:2003/12/30 16:34:32
专用驱动包(Oracle 10g)  发帖心情 Post By:2012/4/22 20:47:47 [只看该作者]

 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:ojdbc14.zip


 回到顶部