| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3326 人关注过本帖
标题:自学java注册页面的代码时,报错:QueryRunner requires a DataSource to b ...
只看楼主 加入收藏
赞赞的人儿
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-9-13
收藏
 问题点数:0 回复次数:0 
自学java注册页面的代码时,报错:QueryRunner requires a DataSource to be invoked in this way,求教
1.报错内容:java.sql.SQLException: QueryRunner requires a DataSource to be invoked in this way, or a Connection should be passed in
   
2.注册页面的源码:
package com.ithiema.register;

import
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.
import org.

import com.ithiema.utils.DataSourceUtils;
public class RegisterServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //设置request的编码---只适合post方式
        request.setCharacterEncoding("UTF-8");
        
        //get方式乱码解决
        //String username = request.getParameter("username");//乱码
        //先用iso8859-1编码 在使用utf-8解码
        //username = new String(username.getBytes("iso8859-1"),"UTF-8");
        
        
        //1、获取数据
        

        //2、将散装的封装到javaBean
        
        //使用BeanUtils进行自动映射封装
        //BeanUtils工作原理:将map中的数据 根据key与实体的属性的对应关系封装
        //只要key的名字与实体的属性 的名字一样 就自动封装到实体中
        Map<String, String[]> properties = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user, properties);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }

        //现在这个位置 user对象已经封装好了
        //手动封装uid----uuid---随机不重复的字符串32位--java代码生成后是36位
        user.setUid(UUID.randomUUID().toString());

        //3、将参数传递给一个业务操作方法
        try {
            regist(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        //4、认为注册成功跳转到登录页面
        response.sendRedirect(request.getContextPath()+"/login");
        

    }

    //注册的方法
    public void regist(User user) throws SQLException{
        //操作数据库
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "insert into user values(?,?,?,?,?,?,?,?,?,?)";
        
        runner.update(sql,user.getUid(),user.getUsername(),user.getPassword(),user.getName(),
                user.getEmail(),null,user.getBirthday(),user.getSex(),null,null);
    }

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



}
3.自写的DataSource工具类(用的dbutils工具包):
package com.ithiema.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import com.mchange.v2.

public class DataSourceUtils {

    private static DataSource dataSource = new ComboPooledDataSource();

    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();

    // 直接可以获取一个连接池
    public static DataSource getDataSource() {
        return dataSource;
    }

    // 获取连接对象
    public static Connection getConnection() throws SQLException {

        Connection con = tl.get();
        if (con == null) {
            con = dataSource.getConnection();
            tl.set(con);
        }
        return con;
    }

    // 开启事务
    public static void startTransaction() throws SQLException {
        Connection con = getConnection();
        if (con != null) {
            con.setAutoCommit(false);
        }
    }

    // 事务回滚
    public static void rollback() throws SQLException {
        Connection con = getConnection();
        if (con != null) {
            con.rollback();
        }
    }

    // 提交并且 关闭资源及从ThreadLocall中释放
    public static void commitAndRelease() throws SQLException {
        Connection con = getConnection();
        if (con != null) {
            (); // 事务提交
            con.close();// 关闭资源
            tl.remove();// 从线程绑定中移除
        }
    }

    // 关闭资源方法
    public static void closeConnection() throws SQLException {
        Connection con = getConnection();
        if (con != null) {
            con.close();
        }
    }

    public static void closeStatement(Statement st) throws SQLException {
        if (st != null) {
            st.close();
        }
    }

    public static void closeResultSet(ResultSet rs) throws SQLException {
        if (rs != null) {
            rs.close();
        }
    }

}

4.配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

  <default-config>
        <property name="user">root</property>
        <property name="password">123</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql:///web15</property>
    </default-config>
 
  <named-config name="oracle">
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql:///web15</property>
    <property name="user">root</property>
    <property name="password">123</property>
  </named-config>
 

</c3p0-config>

搜索更多相关主题的帖子: java con import User property 
2018-03-26 14:16
快速回复:自学java注册页面的代码时,报错:QueryRunner requires a DataSource ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038817 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved