java PreparedStatement 抛出 NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6658748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:50:14 来源:igfitidea点击:
PreparedStatement throws NullPointerException
提问by Rebel
problem occurs on the line
线路出现问题
PreparedStatement pStmt = conn.prepareStatement("select * from employee where upper(FIRSTNAME) like ? and upper(LASTNAME) like ? "
my database exist btw.
我的数据库存在顺便说一句。
public class StubEmployeeRepositoryImpl implements EmployeeRepository {
private Connection conn;
private DataSource dataSource;
// DataSource class encapsulates the driver, database url, username and
// password information. The dataSource object is automatically created by
// the Spring framework and passed to the constructor therefore there's no
// need
// to instantiate the dataSource variable. A connection can be acquired by
// accessing the getConnection method of dataSource.
//
// Tip: create member variables in this class that will contain the objects
// passed by the Spring framework so that other methods can access the
// objects.
private static Logger log = Logger
.getLogger(StubEmployeeRepositoryImpl.class);
public StubEmployeeRepositoryImpl(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public List<Employee> findEmployeesByName(String firstName, String lastName) {
List<Employee> list = new ArrayList<Employee>();
try {
Connection myConnection = dataSource.getConnection();
PreparedStatement pStmt = conn
.prepareStatement("select * from employee where upper(FIRSTNAME) like ? and upper(LASTNAME) like ? "
+ "order by ID ASC");
pStmt.setString(1, "%" + firstName.toUpperCase() + "%");
pStmt.setString(2, "%" + lastName.toUpperCase() + "%");
ResultSet rs = pStmt.executeQuery();
while (rs.next()) {
list.add(new Employee(rs.getInt("ID"), rs
.getString("firstName"), rs.getString("middleInitial"),
rs.getString("LastName"), rs.getString("level"), rs
.getString("workforce"), rs
.getString("enterpriseID")));
}
rs.close();
pStmt.close();
} catch (SQLException e) {
}
return list;
}
@Override
public Employee findEmployeeByID(long employeeID) {
Employee result = null;
try {
Connection myConnection2 = dataSource.getConnection();
PreparedStatement pStmt = conn
.prepareStatement("select * from employee where ID = ?");
pStmt.setInt(1, (int) employeeID);
ResultSet rs = pStmt.executeQuery();
if (rs.next()) {
result = new Employee(rs.getInt("ID"), rs
.getString("firstName"), rs.getString("middleInitial"),
rs.getString("LastName"), rs.getString("level"), rs
.getString("workforce"), rs
.getString("enterpriseID"));
}
rs.close();
pStmt.close();
} catch (SQLException e) {
}
return result;
}
@Override
public List<Employee> findEmployeesByProject(long projectID) {
List<Employee> list = new ArrayList<Employee>();
try {
Connection myConnection3 = dataSource.getConnection();
PreparedStatement pStmt = conn.prepareStatement("");
pStmt.setInt(1, (int) projectID);
ResultSet rs = pStmt.executeQuery();
while (rs.next()) {
list.add(new Employee(rs.getInt("ID"), rs
.getString("firstName"), rs.getString("middleInitial"),
rs.getString("LastName"), rs.getString("level"), rs
.getString("workforce"), rs
.getString("enterpriseID")));
}
rs.close();
pStmt.close();
} catch (SQLException e) {
}
return list;
}
}
回答by Mark Elliot
The problem is you never initialize conn
.
问题是你从不初始化conn
.
For instance, where you do this:
例如,您在何处执行此操作:
Connection myConnection2 = dataSource.getConnection();
PreparedStatement pStmt = conn.prepareStatement("select * from employee where ID = ?");
conn
is still null. Perhaps you meant:
conn
仍然为空。也许你的意思是:
Connection myConnection2 = dataSource.getConnection();
PreparedStatement pStmt = myConnection2.prepareStatement("select * from employee where ID = ?");
回答by Eng.Fouad
Obviously, because conn
is null. You have to initialize it.
显然,因为conn
是空的。你必须初始化它。