jsp+DAO我的查询语句中构造PreparedStatement时dbc.getConnection().prepareStatement(sql)出错
//查所有publicListselectAll(){Listall=newArrayList();Stringsql="SELECTid,title,author,co...
//查所有
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;
}
构造PreparedStatement时dbc.getConnection().prepareStatement(sql)出错,也就是我的代码中位置1老出错, 我用的是ACCESS数据库,我是初学者,不好意思请高手指教 ,
我试过了,prepareStatment()中必须有参数 pstmt= dbc.getConnection().prepareStatement() ;ResultSet rs = pstmt.executeQuery(sql); 这样写通编译不过 展开
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;
}
构造PreparedStatement时dbc.getConnection().prepareStatement(sql)出错,也就是我的代码中位置1老出错, 我用的是ACCESS数据库,我是初学者,不好意思请高手指教 ,
我试过了,prepareStatment()中必须有参数 pstmt= dbc.getConnection().prepareStatement() ;ResultSet rs = pstmt.executeQuery(sql); 这样写通编译不过 展开
4个回答
展开全部
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();
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);
pstmt= dbc.getConnection().prepareStatement() ;
ResultSet rs = pstmt.executeQuery(sql);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一楼正解
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
fgyisdgfvb
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询