解决JdbcUtils.getQueryRunner().query(sql2,new ScalarHandler())报错

 @SuppressWarnings({ "unchecked", "rawtypes" })
    public int save(Orders order) {
	String sql = "INSERT INTO orders(orderDate,orderPrice,orderStatus,userId) VALUES(?,?,?,?);";
	String sql2 = "select last_insert_id() from orders";
	try {
	    JdbcUtils.getQueryRunner().update(sql, order.getOrderDate(),
		    order.getOrderPrice(), order.getOrderStatus(),
		    order.getUserId());
	    return JdbcUtils.getQueryRunner().query(sql2,
		    new ScalarHandler());
	  
	} catch (Exception e) {
	    
	    throw new RuntimeException();
	}
    }

首先说明:我的代码是第一条sql语句插入数据,第二条返回刚刚存入的数据id。

 

一开始我的代码如上图所示,执行的时候一直抛出异常,百度搜了很多方法,

有的说JdbcUtils.getQueryRunner().query(sql2, new ScalarHandler())先转换成long,再转换成int;我试了一下不对。

 

又把catch里改成了:e.e.printStackTrace();这次会输出准确的异常,java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long。我又修改了jar包,改成了mysql-connector-java-5.1.46。

可是还没解决,最后的正确代码就是:

 public int save(Orders order) {
	String sql = "INSERT INTO orders(orderDate,orderPrice,orderStatus,userId) VALUES(?,?,?,?);";
	String sql2 = "select last_insert_id() from orders";
	try {

	    JdbcUtils.getQueryRunner().update(sql, order.getOrderDate(),
		    order.getOrderPrice(), order.getOrderStatus(),
		    order.getUserId());
	    String orderId = (JdbcUtils.getQueryRunner().query(sql2,
		    new ScalarHandler())).toString();
	    return Integer.valueOf(orderId);

	} catch (Exception e) {
	    e.printStackTrace();
	    throw new RuntimeException();
	}
    }

花费了两个多小时才解决问题,记录下来以便看到的人尽快解决。

package druidJDBCUtils; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class DruidJDBCUtils { //定义成员变量 private static DataSource ds; //静态代码块加载配置文件 static { try { Properties prop = new Properties(); InputStream is = DruidJDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); prop.load(is); ds = DruidDataSourceFactory.createDataSource(prop); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * 获取数据库连接对象 */ public static Connection getConnection() throws SQLException { return ds.getConnection(); } /** * 获取连接池方法 */ public static DataSource getDataSource(){ return ds; } /** * 关闭资源方法 * close()查询sql方法 */ public static void close(ResultSet resultSet, Statement statement, Connection connection) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 关闭资源方法 * close()增删改sql方法 */ public static void close(Statement statement, Connection connection) {
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值