高手请进,这里有个小问题,本人是菜鸟,真完全搞不懂事什么问题
package com.bjsxt.shopping;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.sql.*;
import com.bjsxt.shopping.util.DB;
public class User {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public Timestamp getRdate() {
return rdate;
}
public void setRdate(Timestamp rdate) {
this.rdate = rdate;
}
private String username;
private String password;
private String phone;
private String addr;
private Timestamp rdate;
public void save(){
Connection conn = DB.getConn();
String sql = "insert into user values (null,?,?,?,?,?)";
PreparedStatement pstmt = DB.getPStmt(conn,sql);
try {
pstmt.setString(1,username);
pstmt.setString(2, password);
pstmt.setString(3, phone);
pstmt.setString(4, addr);
pstmt.setTimestamp(5, rdate);
pstmt.executeUpdate();
} catch (
SQLException e) {
e.printStackTrace();
}finally{
DB.closeStmt(pstmt);
DB.closeConn(conn);
}
}
public static List<User> getUsers(){
List<User> list = new ArrayList<User>();
try {
Connection conn = null;
ResultSet rs = null;
conn = DB.getConn();
String sql = "select * from ruser";
rs =DB.executeQuery(conn, sql);
while(rs.next()){
User u = new User();
u.setId(rs.getInt("id"));
u.setUsername(rs.getString("username"));
u.setPassword(rs.getString("password"));
u.setPhone(rs.getString("phone"));
u.setAddr(rs.getString("addr"));
u.setRdate(rs.getTimestamp("rdate"));
list.add(u);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DB.closeRs(rs);
DB.closeConn(conn);
}
return list;
}
}
红色部分是错误的地方,错误提示:rs
cannot be resolved和conn cannot be resolved,完全搞不懂到底出了什么问题
我DB的封装:
public static void closeRs(ResultSet rs){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
}
public static void closeConn(Connection conn){
if(conn!=null){
conn=null;
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
------解决方案--------------------
将
Connection conn = null;
ResultSet rs = null;
放在 try{ 之前就可以了!