今天利用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);
}
}
}