jsp+DAO我的查询语句中构造PreparedStatement时dbc.getConnection().prepareStatement(sql)出错

来源:百度知道 编辑:UC知道 时间:2024/05/29 03:03:44
//查所有
public List selectAll() {
List all = new ArrayList() ;
String sql = "SELECT id,title,author,content FROM note " ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try{
try{///////////
pstmt= dbc.getConnection().prepareStatement(sql) ;
}catch(Exception ee){
System.out.println("1位置出错!");
}///////////////

ResultSet rs = pstmt.executeQuery();
while(rs.next()){
Note note=new Note() ;
note.setId(rs.getInt(1));
note.setTitle(rs.getString(2));
note.setAuthor(rs.getString(3));
note.setContent(rs.getString(4));

all.add(note);
}
rs.close();
pstmt.close();

}catch (Exception e){
System.out.println("2位置出错!");
}
finally
{
dbc.close() ;
}
return all

public List selectAll() {
List all = new ArrayList() ;
String sql = "SELECT id,title,author,content FROM note " ;
PreparedStatement pstmt = null ;
ResultSet rs = null;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try{

pstmt= dbc.getConnection().prepareStatement(sql) ;

System.out.println("1位置出错!");

rs = pstmt.executeQuery();
while(rs.next()){
Note note=new Note() ;
note.setId(rs.getInt(1));
note.setTitle(rs.getString(2));
note.setAuthor(rs.getString(3));
note.setContent(rs.getString(4));

all.add(note);
}
rs.close();
pstmt.close();

2处分别改成这样
pstmt= dbc.getConnection().prepareStatement() ;

ResultSet rs = pstmt.executeQuery(sql);

一楼正解

fgyisdgfvb