jdbc 连接不上数据库 到底怎么解决啊
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
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
...全文
相关推荐
Jdbc连接oracle远程数据库中文乱码解决
NULL 博文链接:https://joinyo.iteye.com/blog/1947696
Android使用JDBC连接本地数据库
Android使用JDBC连接本地数据库
JDBC数据库连接技术[归类].pdf
JDBC数据库连接技术[归类].pdf
Pycharm连接Hive数据仓库[项目代码]
本文详细介绍了如何使用Pycharm连接Hive数据仓库,并通过Python进行数据库的增删改查操作。文章首先说明了Hive数据仓库的底层配置,即基于HDFS的分布式文件系统,并提醒读者本文重点在于连接和管理数据库,而非环境配置。接着,文章提供了三种连接方法:1)使用专业版Pycharm的Big Data Tools插件进行界面化连接;2)通过pyhive库进行编码连接;3)使用JDBC连接。每种方法都详细说明了所需的库安装、配置步骤以及可能遇到的问题和解决方案。此外,文章还包含了具体的Python代码示例,展示了如何上传数据到HDFS、载入数据到Hive以及进行增删改查操作。最后,文章强调了运行成功的前提是Hive和HDFS配置正确,并提供了所使用的软件版本信息。
MyEclipse连接Mysql数据库的方法(一)
主要介绍了MyEclipse连接Mysql数据库的方法(一)的相关资料,非常实用,具有参考价值,需要的朋友可以参考下


Web 开发
