查询报错
这里怎么报错了,更新数据是可以的,数据表能写入,就是查询出问题了
package com.imooc.practise.command; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.imooc.practise.common.DbUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import javax.sql.DataSource; import java.io.FileInputStream; import java.net.URLDecoder; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.List; import java.util.Properties; public class ApacheUpdate { private static void update(){ Properties properties = new Properties(); String propertyFile = ApacheUpdate.class.getResource("/druid-config.properties").getPath(); Connection conn = null; PreparedStatement pstmt = null; try { propertyFile = new URLDecoder().decode(propertyFile,"UTF-8"); properties.load(new FileInputStream(propertyFile)); DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); conn = dataSource.getConnection(); conn.setAutoCommit(false); String sql = "insert into goods(name,price,desp,create_time) values(?,?,?,?)"; QueryRunner qr = new QueryRunner(); qr.update(conn,sql,new Object[]{"照相机",5000,"防水,索尼","1999/1/30"}); conn.commit(); query(); } catch (Exception e) { e.printStackTrace(); try { if (conn != null && !conn.isClosed()) { conn.rollback(); System.out.println("添加失败!"); } } catch (SQLException throwables) { throwables.printStackTrace(); } }finally { DbUtils.closeConnection(null,null,conn); } } private static void query(){ Properties properties = new Properties(); String propertyFile = ApacheUpdate.class.getResource("/druid-config.properties").getPath(); try { propertyFile = new URLDecoder().decode(propertyFile, "UTF-8"); properties.load(new FileInputStream(propertyFile)); DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); QueryRunner qr = new QueryRunner(dataSource); List<Goods> list = qr.query("select * from goods", new BeanListHandler<>(Goods.class)); for (Goods good : list) { System.out.println(good.toString()); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { update(); } }
相关截图:
3
收起
正在回答 回答被采纳积分+1
1回答
java工程师2020版
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星