com.mchange.v2.c3p0.impl.NewProxyConnection cannot be cast to com.mysql.jdbc

本文介绍使用C3P0连接池和自定义DataSourceUtils工具类进行转账操作时遇到的问题及解决方法,解释了com.mysql.jdbc.Connection与java.sql.Connection的区别。
摘要由CSDN通过智能技术生成

今天利用C3P0连接池、自己做的DataSourceUtils工具类做了一个转账操作,运行的时候出现这个错误,刚开始的时候有点蒙圈,后来仔细一看报错行,都是出现了强转了的,比如:

connection=(Connection)DataSourceUtils.getConnection();

这是报错说的就是类型不匹配,想着可能是导的包不对,然后发现工具类里面导的是 java.sql.Connection ,而dao里面导的是com.mysql.jdbc.connection,尝试把com.mysql.jdbc.connection改成java.sql.Connection ,就不报错了。

有点搞不懂com.mysql.jdbc.connection和java.sql.Connection的区别在哪里,去百度了一下:com.mysql.jdbc.Connection 是mysql自己的接口,针对于对mysql,java.sql.Connection 是公共的接口,包括对mysql的,支持oracle,sqlserver,是对很多数据库的一个公共的API。

简单来说,就是com.mysql.jdbc.Connection extends java.sql.Connection

工具类和dao类代码如下

package com.day12.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.c3p0.ComboPooledDataSource;

public class DataSourceUtils {
    private static ComboPooledDataSource ds = new ComboPooledDataSource();

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

    public static DataSource getDataSource() {
        return ds;
    }

    public static Connection getConnection() throws SQLException {
        Connection connection = tl.get();

        if (connection == null) {
            connection = ds.getConnection();

            tl.set(connection);
        }

        return connection;
    }

    public static void closeResource(Connection conn, Statement st, ResultSet rs) {
        closeResource(st, rs);
        closeConn(conn);
    }

    public static void closeResource(Statement st, ResultSet rs) {
        closeResultSet(rs);
        closeStatement(st);
    }

    public static void closeConn(Connection conn) {
        if (conn != null) {
            try {
                conn.close();

                tl.remove();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }

    }

    public static void closeStatement(Statement st) {
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            st = null;
        }

    }

    public static void closeResultSet(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }

    }

    public static void startTransaction() throws SQLException {
        // 开启事务
        getConnection().setAutoCommit(false);
    }

    // 提交事务
    public static void commitAndClose() {
        try {
            Connection connection = getConnection();

            connection.commit();

            connection.close();

            tl.remove();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void RollbackAndClose() {
        try {
            Connection connection = getConnection();

            connection.rollback();

            connection.close();

            tl.remove();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
package com.day12.Dao;

import java.sql.SQLException;

import com.day12.Utils.DataSourceUtils;
import com.day12.Utils.JdbcUtils_;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class AccountDao1 {

    public void Out(String out, String money) throws Exception {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DataSourceUtils.getConnection();

            String sql = "update Account set money=money-?where name=?";

            statement = connection.prepareStatement(sql);

            statement.setString(1, money);
            statement.setString(2, out);

            int i = statement.executeUpdate();

            System.out.println(out + "转出" + money);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DataSourceUtils.closeResource(statement, resultSet);
        }

    }

    public void In(String in, String money) throws Exception, SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DataSourceUtils.getConnection();

            String sql = "update Account set money=money+?where name=?";

            statement = connection.prepareStatement(sql);

            statement.setString(1, money);
            statement.setString(2, in);

            int i = statement.executeUpdate();

            System.out.println(in + "转入" + money);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DataSourceUtils.closeResource(statement, resultSet);
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值