public class UserDao implements UserDaoInterface {
private Connection conn;
public UserDao(Connection conn){
this.conn = conn;
}
public List<User> selectUserList() {
String sql = "select * from t_user";
PreparedStatement sta = null;
ResultSet rs = null;
try {
System.out.println(1);
sta = conn.prepareStatement(sql);---------------------------------155
System.out.println(32);
rs = sta.executeQuery();
List users = new ArrayList();
while(rs.next()){
User u = new User();
u.setId(rs.getInt("id"));
u.setUsername(rs.getString("username"));
u.setPassword(rs.getString("password"));
u.setCallname(rs.getString("callname"));
u.setAge(rs.getInt("age"));
u.setBankaccount(rs.getString("bankaccount"));
u.setSalary(rs.getDouble("salary"));
users.add(u);
//System.out.println(users);
}
return users;
} catch (SQLException e) {
e.printStackTrace();
return null;
}finally{
try {
if(rs!=null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(sta!=null)
sta.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
UserDao us = new UserDao();
System.out.println(us.selectUserList());-------------------------------195
}
}
--------------------------------------------------------
控制台输出:
1
Exception in thread "main" java.lang.NullPointerException
at dao.UserDao.selectUserList(UserDao.java:155)
at dao.UserDao.main(UserDao.java:195)
----------------------------------------------------------------------------
数据库表
create table t_user(
id Integer not null primary key,
username varchar2(255) not null ,
password varchar2(255) not null,
callname varchar2(255) not null,
age Integer not null,
bankaccount varchar2(255) not null,
salary decimal(8,2) not null
)
INSERT INTO t_user (id,username,password,callname,age,bankaccount,salary) VALUES(4,'李四','gasg','ettet',24,12313,2423.00);
select * from t_user;
delete from t_user where id=1;