Topic: servlet连接数据库的问题 (JBuilder, Tomcat, Oracle)

  Print this page

1.servlet连接数据库的问题 (JBuilder, Tomcat, Oracle) Copy to clipboard
Posted by: carolingian
Posted on: 2003-06-27 15:25

我用jbuilder+tomcat开发servlet,在用jdbc连接oracle数据库出现问题:
我的测试程序如下:
package servlet;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Database
extends HttpServlet {

public void doGet(
HttpServletRequest request,
HttpServletResponse response
) throws ServletException, IOException {
PrintWriter out;
String title = "Simple Servlet connecting to Oracle DB";

// set content type and other response header fields first
response.setContentType("text/html;charset=GB2312");

// then write the data of the response
out = response.getWriter();

out.println("<HTML><HEAD><TITLE>");
out.println(title);
out.println("</TITLE></HEAD><BODY>");
out.println("<H1>" + title + "</H1>");
out.println("<P>This is output from SimpleServlet.");
String driverName = "oracle.jdbc.driver.OracleDriver";
Driver d;
Connection con;
Statement stmt;
ResultSet results;

try {
d = (Driver) Class.forName(driverName).newInstance();
con = DriverManager.getConnection(
"jdbc:oracle:thin:scott/tiger@192.168.168.168:1521:oradelta");
stmt = con.createStatement();
String sqlstr = "select SYSDATE from dual";
results = stmt.executeQuery(sqlstr);
displayResult(results, out);
stmt.close();
con.close();
}
catch (Exception e) {
out.println("error: " + e.toString());
}
out.println("</BODY></HTML>");
out.close();
}

public void displayResult(ResultSet results, PrintWriter out) {
StringBuffer buf = new StringBuffer();
String temp;
try {
ResultSetMetaData rsmd = results.getMetaData();
int numCols = rsmd.getColumnCount();
int i, rowcount = 0;
// get column header info
for (i = 1; i <= numCols; i++) {
if (i > 1) {
buf.append(",");
}
buf.append(rsmd.getColumnLabel(i));
}
buf.append("\n");
while (results.next() && rowcount < 100) {
for (i = 1; i <= numCols; i++) {
if (i > 1) {
buf.append(",");
}
buf.append( (results.getString(i)));
}
buf.append("<br>");
rowcount++;
}
out.println("<br>");
out.println(buf.toString());
results.close();

}
catch (Exception e) {
out.println("error: " + e.toString());

return;
}
}
}
我在configure library加入了oracle的class12.zip,命名为oracle,然后在project的required library里增加了oracle的库。
编译没有任何问题,但当我点右键web run时,总是提示:
This is output from SimpleServlet. error: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
我已经测试了两天了,被这个问题搞得焦头烂额,拜托各位高手帮帮忙!!!

2.Re:servlet连接数据库的问题 (JBuilder, Tomcat, Oracle) [Re: carolingian] Copy to clipboard
Posted by: why
Posted on: 2003-06-28 03:32

I don't use JBuilder.
Have you tried deploying the servlet directly on Tomcat?

By the way, the name of Oracle JDBC drivers is classes12.zip

3.Re:servlet连接数据库的问题 (JBuilder, Tomcat, Oracle) [Re: carolingian] Copy to clipboard
Posted by: floater
Posted on: 2003-06-28 04:48

Could you find this class: oracle.jdbc.driver.OracleDriver in your class12.zip?

Have you read the doc for sample usage??

The error says this class is not found.

It's not jb issue, unlikely.

4.Re:servlet连接数据库的问题 (JBuilder, Tomcat, Oracle) [Re: carolingian] Copy to clipboard
Posted by: wzbwambition
Posted on: 2003-06-28 11:22

tools--config jdks加上clsss12.jar,重起jb就可以了!

5.Re:servlet连接数据库的问题 (JBuilder, Tomcat, Oracle) [Re: carolingian] Copy to clipboard
Posted by: wzbwambition
Posted on: 2003-06-28 11:24

对了,你是class12.zip肯定不对,应该是class111.jar,class222.jar,你去重下一下,或者把你的class12.zip打开看看是不是有class222.jar

6.Re:servlet连接数据库的问题 (JBuilder, Tomcat, Oracle) [Re: carolingian] Copy to clipboard
Posted by: why
Posted on: 2003-06-28 12:00

wzbwambition:
Please be careful to provide accurate information.
Are you sure there're class111.jar and class222.jar?
I'm only aware of classes111.zip for JDK1.1, classes12.zip for JDK1.2/1.3, and ojdbc14.jar for JDK1.4.

7.Re:servlet连接数据库的问题 (JBuilder, Tomcat, Oracle) [Re: carolingian] Copy to clipboard
Posted by: reborn
Posted on: 2003-06-29 14:33

应该是编译时找到了驱动,运行时没有找到,
把你的驱动包打在web运行的classpath里试试


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923