我想在jsp页面中调出oracle数据库中的数据,该怎么实现?(100分)

  • 主题发起人 主题发起人 fuzhimin
  • 开始时间 开始时间
F

fuzhimin

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在jsp页面中调出oracle数据库中的数据,它的驱动该怎么写,请大家帮我看看,感学
好让我困惑呀,jsp一端该怎么来实现,这方面的例程有吗,谢谢!
 
装完oracle后,目录下就有Oracle自带的JDBC驱动和例子,
找一下吧。
 
<%@ page import="javax.naming.*, javax.sql.*, java.sql.*" %>
<html>
<body>
<%
InitialContext ic = new InitialContext();
DataSource ds = (DataSource)ic.lookup("jtds");
Connection conn = null;
if (ds != null) {
conn = ds.getConnection();
}
if (conn == null)
out.println("failure");
else
out.println("successful");
%>
</body>
</html>

package com;
import java.sql.*;
public class DBUtils {

public static Connection getConnection(String className, String url, String userName, String password) {
try {
Class.forName(className);
return DriverManager.getConnection(url, userName, password);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Connection getOracleConnection(String driverName, String machine, String orcl, String port, String userName, String password) {
String url = "jdbc:oracle:thin:@" + machine + ":" + port + ":" + orcl;
return getConnection(driverName, url ,userName, password);
}
public static Connection getSpecialOracleConnection() {
return getOracleConnection("jdbc.oracle.driver.OracleDriver", "127.0.0.1", "ORCL", "1521", "system", "manager");
}
}
大家看看,这是个不错的连接oracle的驱动和连接程序,是我从校园网上下的,值得收藏
,各位看看,顺便给诠释一下,我有些地方不是很明白,比如各个参数的详细含义。
 
大家好,我用JDBC_ODBC连接数据库时提示"no suitable driver",请问JSP用JDBC_ODBC连接数据库时是否需要什么驱动?以下是我的程序代码,代码应该没有错,我想可能是配制的问题,请各位帮帮忙:
<%@ page contentType="text/html;
charset=GBK" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>
ItemQuery
</title>
</head>
<body>
<h1>
JBuilder Generated JSP
</h1>
<%
Connection con = null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbc");
con = DriverManager.getConnection("jdbc:odbc:jspdb","","");
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery("select * from itemTab");
%>
<TABLE BORDER="1">
<TR><TH>NO</TH><TH>NAME</TH><TH>PRICE</TH><TH>QTY</TH></TR>
<%
while(rs.next())
{
out.println("<TR><TD>"+rs.getObject("id")+"</TD>");
out.println("<TD>"+rs.getString("desc")+"</TD>");
out.println("<TD>"+rs.getDouble("price")+"</TD>");
out.println("<TD>"+rs.getInt("qty")+"</TD></TR>");
}
rs.close();
con.close();
}
catch(Exception e)
{
out.println(e.getMessage());
}
%>
</body>
</html>
 
JavaBean代码
package database;
import java.sql.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002 Yaoguodong</p>
* <p>Company: </p>
* @author unascribed YaoGuodong
* @email net_spirit@163.com
* @version 1.0
*/
public class DBconnect {
private String strDBDriver="oracle.jdbc.driver.OracleDriver";
private String strConnstr="jdbc:oracle:thin:@192.168.0.68:1521:dynasty";
Connection conn=null;
ResultSet rs=null;
Statement stmt=null;
//构造函数
public DBconnect(){
try{
Class.forName(strDBDriver);
conn = DriverManager.getConnection(strConnstr,"dynasty","dynasty");
stmt=conn.createStatement();
}
catch(Exception e){
System.out.println(e);
}
}
public ResultSet executeQuery(String sql){
try{
rs=stmt.executeQuery(sql);
}
catch(SQLException ex){
System.out.println("执行SQL语句出错: " + ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql) {
try {
stmt.executeUpdate(sql);
}
catch(SQLException ex) {
System.err.println("执行SQL语句出错: " + ex.getMessage());
}
}
public Connection getConnection(){
return conn;
}
public boolean close(){
try{
if(this.rs!=null){ this.rs.close();
}
if(stmt!=null){ this.stmt.close();
}
if(conn!=null){ this.conn.close();
}
return true;
}catch(Exception err){
return false;
}
}
}

JSP文件
<html>
<head>
<title>Connect Database test!</title>
<meta http-equiv="Content-Type" content="text/html;
charset=gb2312">
<%@page contentType="text/html;charset=gb2312"%>
<%@page language="java" import="java.sql.*" %>
<%@page import="oracle.jdbc.driver.*" %>
<%@page import="database.*" %>
<jsp:useBean id="connect" scope="page" class="database.DBconnect" />
</head>
<body>
<table border=1 align="center">
545454645645645
<%
//String sql="SELECT yong_hm,yong_hkl FROM yong_h";
%>
545454645645645
<%
//String sql="select * from yong_h";
ResultSet rs = connect.executeQuery("select * from yong_h");
//读queyrSql方法,返回结果集。
out.println("<tr><td align=center><font color=red>用户名</font></td>");
out.println("<td align=center><font color=red>密码</font></td></tr>");

while (rs.next())
{
String aa=rs.getString(1);

String bb=rs.getString(2);

%>
<tr><td><%=aa%></td><td><%=bb%></td></tr>
<%
}
out.println("<tr><td colspan=2> </td></tr>");

rs.close();
%>
</table>
</body>
</html>
报错信息:
执行SQL语句出错: ORA-00600: 内部错误代码,参数: [ttcgcshnd-1], [0], [], [], [],
[], [], []
java.lang.NullPointerException
at _login._datatest__jsp._jspService(/login/datatest.jsp:23)
at com.caucho.jsp.JavaPage.service(JavaPage.java:74)
at com.caucho.jsp.Page.subservice(Page.java:485)
at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:181)
at com.caucho.server.http.Invocation.service(Invocation.java:291)
at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:132)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:217)
at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:159)
at com.caucho.server.TcpConnection.run(TcpConnection.java:136)
at java.lang.Thread.run(Thread.java:484)
 
连接如下:
Connection conn =DriverManager.getConnection("jdbc:oracle:thin:@172.16.1.1:1521:dbname","user","password");
提示:can not access "172.16.1.1":1521
但我的Oracle Client是能够连到Server的
请问这是什么问题?
敬盼回音!谢谢!

试试这个:
Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection myCon = DriverManager.getConnection("jdbc:oracle:thin:@rnd:1521:dbname","system","manager");

host 名字是装有oracle server的机器名字,而不是装oracle client的机器名,用IP地址较保险,connecting String的格式肯定没问题,主要检查一下IP地址,端口号是否正确,默认端口是1521.
[这个贴子最后由doctest在 2002/01/29 02:46pm 编辑]
我的host名字是test,IP是172.16.1.1
IP地址和端口号都是和Net8设置一样的
我是个Java新手
我把整个类都放上来
请大家帮我看看,多多指教
import java.awt.*;
import java.applet.*;
import java.sql.*;
import java.lang.*;

public class NEW extends Applet {
protected String msg;
public void init() {
try
{
Statement stmt;
String driverName = "oracle.jdbc.driver.OracleDriver";
Driver driver = (Driver)Class.forName ("oracle.jdbc.driver.OracleDriver").newInstance();

Connection conn =DriverManager.getConnection("jdbc:oracle:thin:@172.16.1.1:1521:MY_TEST","system","manager");

conn.setAutoCommit(true);
stmt = conn.createStatement();
stmt.executeUpdate("insert into MY_TEST values ('T','T','A','0','TEST')");
stmt.close();
conn.close();
}
catch(Exception e)
{
msg="error:"+e;
System.out.println("error:"+e);

}
}
public void paint(Graphics g) {
g.drawString(msg,100,200);

}
}

applet在客户端是没有权利访问非Server外的网络资源的,而且在本地建立网络连接似乎也是sandbox所不允许的
我希望在IE中利用Java进行Oracle数据库操作
调用Procedure
该怎么做呢?
请指点迷津
谢谢!

看看applet的security模型吧,
可以用数字签名和授权来解决本地资源不能访问的问题
详细还是自己看看吧,不是一下可以说清的


 
源程序如下:
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.sql.*" %>
<%@ page import="oracle.jdbc.driver.*" %>
<html>
<body>
<%
sDBDriver = "oracle.jdbc.driver.OracleDriver";
Driver strDriver = Class.forName(sDBDriver).newInstance();
Connection conn = DriverManager.getConnection("java:oracle:thin:@192.168.0.100:1521:orcl","system","manager");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from name" );
while( rs.next() )
{
String name = rs.getString("test");
out.println(name);
}
%>
</body>
</html>
运行时出错,提示为:
Package oracle.jdbc.driver not found in import.
import oracle.jdbc.driver.*;
是什么原因导致错误啊?但我在tomcat里面能找到oracle.jdbc.driver这个包啊?高手们,请指教!

import java.sql.*;
public class selectBean {
//oracle数据库
String sDBDriver = "oracle.jdbc.driver.OracleDriver";
String sConnStr = "jdbc:oracle:thin:@hostname:1521:DBname";
String username="system";
String password="manager";
//access数据库
/*String sConnStr = "jdbc:odbc:bbs";
String sDBDriver= "sun.jdbc.odbc.JdbcOdbcDriver"
String username = "";
String password = "";*/
Connection conn = null;
ResultSet rs = null;
public selectBean() {
try{
Class.forName(sDBDriver);
}
catch(ClassNotFoundException e){
System.out.println(e.getMessage());
}
}
public ResultSet executeQuery(String sql){
try{
conn = DriverManager.getConnection(sConnStr,username,password);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException e){
System.err.println("selectBean_executeQuery:"+e.getMessage()) ;
}
return rs;
}
}

String sDBDriver= "sun.jdbc.odbc.JdbcOdbcDriver" 这不对,这样调用的driver是 sun的
jdbc-odbc driver应该用
String sDBDriver= "oracle.jdbc.driver.OracleDriver";


例子里用的是 conn = DriverManager.getConnection(
"jdbc:oracle:oci8:@", "scott", "tiger");
我改了也一样的说,会不会是环境设置有问题呢?正确的该怎么设,谢谢!
你看的这个是用OCI连接的例子,不是用thin driver连接的例子
用thin driver的该是这样的
conn = DriverManager.getConnection("jdbc:oracle:thin:@host:1521:<serverSID>", "scott", "tiger");
其实你写的确实没什么问题了,而且thin driver是不需要别的什么支持的,只要有jdbc driver就可以了,
用OCI的话可能还要设一下LIB_PATH之类的环境变量,我现在真怀疑你的这个Driver是不是支持thin的方法连接数据库了
我快没办法了..........郁闷中
http://otn.oracle.com/software/tech/java/sqlj_jdbc/content.html这个地址可以下载
oracle的jdbc driver,重新下在一个看看
 
请问访问远程的oracle数据库应该如何写驱动?我用了jdbc:oracle:thin数据库驱动但是不行。

这么写:
Class.forName("oracle.jdbc.driver.OracleDriver");//或者其他的驱动程序
java.sql.Connection con = java.sql.DriverManager.getConnection( "jdbc:oracle:thin(或其他与你使用的驱动程序相应的url):@数据库服务器地址:1521(端口号,默认是1521):数据库名","用户名","密码");


我写的驱动是:Class.forName("oracle.jdbc.driver.OracleDriver");
java.sql.Connection con = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.20:1521:oral", "***", "***");
我是想从192.168.1.110的机器上访问数据库。
不知道你有没有安装oracle的classes21.jar驱动包这个就是oracle的驱动程序文件,没有他怎么也连接不上数据库的,这个文件包在oracle的安装盘里有
装上Oracle的client吧
配置网络监视器
明明是网络不不能到达嘛,检测你的用户名跟密码,然后确认你的classpath中配置了
classes12.zip驱动程序的路径没
 
接受答案了.
 
后退
顶部