数据库问题????

zwd2878 2003-12-03 11:27:15
我写了个bulletins.properties文件
内容为:
db_driver=oracle.jdbc.driver.OracleDriver
db_url=jdbc:oracle:thin:@192.168.0.11:1521:orcl1
db_user=test
db_password=test

在java文件中,我通过下面方式调用

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

public class bulletinshttp extends HttpServlet
{
private static Properties http_props = new Properties();
private static String db_Driver;
private static String db_Url;
private static String db_User;
private static String db_Password;


Connection connect;

public void init(ServletConfig configuration)
throws ServletException
{
super.init(configuration);

try
{
// FileInputStream infile = new FileInputStream("/bulletins/bulletins.properties");

InputStream infile = this.getClass().getResourceAsStream("/bulletins.properties");




http_props.load(infile);
db_Driver = http_props.getProperty("db_driver");
db_Url = http_props.getProperty("db_url");
db_User = http_props.getProperty("db_user");
db_Password = http_props.getProperty("db_password");



infile.close();
}
catch (Exception e)
{
System.out.println(e);
}
}

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
doPost(req, resp);
}

public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{


PrintWriter outhtml;

String driver = db_Driver;
String url = db_Url;
String user = db_User;
String password = db_Password;
String urlextrapath;

resp.setContentType("text/html");
outhtml = resp.getWriter();

/*
outhtml.println ("driver = "+driver);
outhtml.println ("url = "+url);
outhtml.println ("user = "+user);
outhtml.println ("password = "+password);
*/

try
{
Class.forName(driver);
connect = DriverManager.getConnection("url", "user", "password");
urlextrapath = req.getPathInfo();
bulletins bltn = new bulletins();
bltn.connect = connect;
Enumeration enum = req.getParameterNames();


…………………………………………………………………………

…………

}
catch(ClassNotFoundException e)
{
outhtml.println ("Cannot load driver:"+ e.getMessage ());
}
catch(SQLException e)
{
outhtml.println ("SQLException: "+e.getMessage ());
}
finally
{
try
{
if(connect!=null)
connect.close ();
}
catch(SQLException ignored)
{}

}
}
}



另外,在tomcat的common/lib里面已经加了class12.jar及class12.zip

可是最后测试时:却捕捉到下面的错误(在html页面上)

SQLException: No suitable driver


请高手指点,如何调试??

万分感谢:)
...全文
29 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
lekuaile 2003-12-06
  • 打赏
  • 举报
回复
connect = DriverManager.getConnection("url", "user", "password");这句中的参数不应该加双引号呀,他们不是变量吗?应该是
connect = DriverManager.getConnection(url, user, password);
zwd2878 2003-12-06
  • 打赏
  • 举报
回复
真的是这样吗??
zwd2878 2003-12-06
  • 打赏
  • 举报
回复
俺试了,不是环境变量的问题。

我觉得应该是server文件的事情,在server文件中也有数据库的连接,而在servlet中也有,两者可能冲突,所以不通了。

不知是不是这样,我用一个testdb作实验成功了,
testdb文件如下:

<%@ page contentType="text/html;charset=GBK"%>
<%@ page import= "java.sql.* "%>
<%@ page import= "javax.naming.* "%>
<%
try{
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
//获取连接池对象
Object obj = (Object) ctx.lookup("jdbc/OracleDB_test");
//类型转换
javax.sql.DataSource ds = (javax.sql.DataSource)obj;
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
//String strSql = "insert into test(id,name) values('00001','holen')";
//stmt.executeUpdate(strSql);
String strSql = "select id,name from test";
ResultSet rs = stmt.executeQuery(strSql);
if(rs.next()){
out.println(rs.getString(1));
out.println(rs.getString(2));
}
}catch(Exception ex){
ex.printStackTrace();
throw new SQLException("cannot get Connection pool.");
}
%>
<hr>
zwd2878 2003-12-06
  • 打赏
  • 举报
回复
HASHMAP
俺还没有用过:)不知道怎么用,
高手可以教教俺吗,谢谢:)
hymarx 2003-12-04
  • 打赏
  • 举报
回复
Class.forName(driver);
把driver debug出来看看
再看看这个方法的返回值
很哟可能是classpath问题,在环境变量里把class12.jar加进去看看
connect = DriverManager.getConnection("url", "user", "password");
cocosunshine 2003-12-04
  • 打赏
  • 举报
回复
我前一段用HASHMAP做了一个类似的~~
cocosunshine 2003-12-04
  • 打赏
  • 举报
回复
倒,你为什么不用HASHMAP读呢~~
zwd2878 2003-12-04
  • 打赏
  • 举报
回复
我试了,db_Driver能读出来。


如果我在server文件中也做了连数据库的设置,在servlet中如上所示,会不会冲突呀?

qiume 2003-12-04
  • 打赏
  • 举报
回复
SQLException: No suitable driver

加载驱动错误
mysam 2003-12-04
  • 打赏
  • 举报
回复
你的驱动好像加载错了
xuewuz 2003-12-04
  • 打赏
  • 举报
回复
你的属性文件是否真的读进来了?看看 db_Driver=“oracle.jdbc.driver.OracleDriver"?
lijong29909 2003-12-04
  • 打赏
  • 举报
回复
4.2. My application throws a SQLException 'No Suitable Driver'. Why is this happening?

One of two things are happening. Either the driver is not in your CLASSPATH (see the "INSTALLATION" section above), or your URL format is incorrect (see "Developing Applications with MySQL Connector/J").

mysql上面的
lijong29909 2003-12-04
  • 打赏
  • 举报
回复
注册驱动的驱动名称写错了
ladofwind 2003-12-04
  • 打赏
  • 举报
回复

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧