jdbc 连接不上数据库 到底怎么解决啊

a33103 2010-06-10 03:51:37
package com.google.blog;

import java.io.IOException;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;

public class BlogServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//解决从JSP页面接受中文参数乱码
request.setCharacterEncoding("UTF-8");
//接受JSP页面传递过来的与博文有关的三个参数:主题、内容、所属种类的ID值
String title=request.getParameter("title");
String content=request.getParameter("content");
String categoryId=request.getParameter("category");
//数据对象可以理解为连接池的管理者,通过它可以获取数据库的连接
DataSource ds=null;

try
{
//通过在context.xml中设定的数据源对象的名字,获取数据源对象
Context context=new InitialContext();
ds=(DataSource) context.lookup("java:/comp/env/jdbc/mysqlds");
}
catch (Exception e)
{
System.out.println("读取数据源时出错");
}
int result=0;

try
{
//添加博文的SQL语句,now()生成当前系统时间
String sql="insert into blog(title,content,category_id,create_time) values (?,?,?,now())";
//为sql语句中的?设定参数
String params[] = {title,content,categoryId};
//DButils中核心类,生成对象时传递数据源对象
QueryRunner qr=new QueryRunner(ds);
//调用它的update,完成sql的运行。其他使用update方法的sql语句:insert into/update/delete
result=qr.update(sql,params);
}
catch(SQLException e)
{
e.printStackTrace();
}

String message="";
if(result==1)
{
message="添加博文成功!";
}
else
{
message="添加博文失败!";
}

request.setAttribute("message", message);
request.getRequestDispatcher("/addBlogResult.jsp").forward(request, response);
}
}
读取数据源时出错
java.sql.SQLException: QueryRunner requires a DataSource to be invoked in this way, or a Connection should be passed in
...全文
1073 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿木木的忧伤 2012-02-04
  • 打赏
  • 举报
回复
我也是遇到了这样的问题,楼主解决了没有???可否告诉下。。。。
sshajaxajax 2010-10-06
  • 打赏
  • 举报
回复
加进去mysqljdbc驱动后,一定要重启tomcat才能生效,你试试看
zktbduandd 2010-06-10
  • 打赏
  • 举报
回复
明显没有获得连接,是否url正确,user,password是否正确,驱动是否添加好了
或者localhost换成ip试试,或者用客户端连接一下看是否可以,如果可以那肯定是程序中写的问题,如果不可以也可能是防火墙之类的,试试看
或者看看数据库版本问题
kuaileok 2010-06-10
  • 打赏
  • 举报
回复
我不知道啊
ronniegxq 2010-06-10
  • 打赏
  • 举报
回复
看来还是你的datasource对象没有正常生成
a33103 2010-06-10
  • 打赏
  • 举报
回复
7楼所说的我应经改过来了 不过不是这的错误
补充:我使用了dbutil组件
ronniegxq 2010-06-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 a33103 的回复:]

我使用过了连接池 更改了C:\tomcat6\conf下的context.xml文件,在其中添加了
<Resource name="jdbc/mysqlds"
auth="Container"
type="javax.sql.DateSource"
macActive="100"
maxIdle="30"
maxWait="10000"
username="root"
passw……
[/Quote]


macActive="100" ===>应该是maxActive="100" 吧
ronniegxq 2010-06-10
  • 打赏
  • 举报
回复
把你数据元的配置贴出来看看
  • 打赏
  • 举报
回复
在创建Context对象前,需要初始化jndi

Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
prop.put(Context.PROVIDER_URL, url);
a33103 2010-06-10
  • 打赏
  • 举报
回复
我使用过了连接池 更改了C:\tomcat6\conf下的context.xml文件,在其中添加了
<Resource name="jdbc/mysqlds"
auth="Container"
type="javax.sql.DateSource"
macActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="1"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1/xiarenjie"/>这样的代码
不知道是不是与这有关
狂想者 2010-06-10
  • 打赏
  • 举报
回复
学习啊。。。。。。。
gulang76 2010-06-10
  • 打赏
  • 举报
回复
java.sql.SQLException: QueryRunner requires a DataSource to be invoked in this way, or a Connection should be passed in
数据源有问题
Java技术栈 2010-06-10
  • 打赏
  • 举报
回复
java.sql.SQLException: QueryRunner requires a DataSource to be invoked in this way, or a Connection should be passed in

很明显没有得到数据源 我没有看到代码中有得到数据源的代码

81,122

社区成员

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

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