为什么会报空指针异常呢?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | package com.joey.jdbc.goods; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.joey.jdbc.common.DbUtils; import com.joey.jdbc.goods.command.*; import javax.sql.DataSource; import java.io.FileInputStream; import java.net.URLDecoder; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Properties; public class GoodsData { public static void main(String[] args) { Connection conn = null ; PreparedStatement pstmt = null ; ResultSet rs = null ; Properties properties = new Properties(); String propertyFile = GoodsData. class .getResource( "/druid-config.properties" ).getPath(); try { propertyFile = new URLDecoder().decode(propertyFile, "UTF-8" ); properties.load( new FileInputStream(propertyFile)); DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); conn = dataSource.getConnection(); String sql = "select * from goods" ; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { Integer id = rs.getInt( "id" ); String name = rs.getString( "name" ); Float price = rs.getFloat( "price" ); String desp = rs.getString( "desp" ); Date create_time = rs.getDate( "create_time" ); System.out.println(id + " " + name + " " + price + " " + desp + create_time); } } catch (Exception e) { e.printStackTrace(); } finally { DbUtils.closeConnection(rs,pstmt,conn); } } } |
34
收起
正在回答 回答被采纳积分+1
2回答
java工程师2020版
- 参与学习 人
- 提交作业 9402 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧