我想分页,运行的时候出现:'@P0' 附近有语法错误。我该怎样写那个sql语句??
public class bookDao{
public Connection getConnection() {
Connection con=null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://127.0.0.1:1433;database=test;";
// 数据库用户名
String username = "sa";
// 数据库密码
String password = "";
// 创建Connection连接
con = (Connection) DriverManager.getConnection(url,username,password);
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
return con;
}public List <Book>find(int page){
List<Book>list=new ArrayList<Book>();
Connection con=getConnection();
String sql= "select Top ? * from tb_book order by id desc";
try{
PreparedStatement ps=con.prepareStatement(sql);
ps.setInt(2,Book.PAGE_SIZE);
ResultSet rs=ps.executeQuery();
while(rs.next()){
Book B=new Book();
B.setId(rs.getInt("id"));
B.setName(rs.getString("name"));
B.setPrice(rs.getDouble("price"));
B.setBookCount(rs.getInt("bookCount"));
B.setAuthor(rs.getString("author"));
list.add(B);
}
rs.close();
ps.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
return list;
}
public int findCount(){
int count=0;
Connection con=getConnection();
String sql="select count(*)from tb_Book";
try{
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
count=rs.getInt(1);
}
rs.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
return count;
}
}
public class bookDao{
public Connection getConnection() {
Connection con=null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://127.0.0.1:1433;database=test;";
// 数据库用户名
String username = "sa";
// 数据库密码
String password = "";
// 创建Connection连接
con = (Connection) DriverManager.getConnection(url,username,password);
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
return con;
}public List <Book>find(int page){
List<Book>list=new ArrayList<Book>();
Connection con=getConnection();
String sql= "select Top ? * from tb_book order by id desc";
try{
PreparedStatement ps=con.prepareStatement(sql);
ps.setInt(2,Book.PAGE_SIZE);
ResultSet rs=ps.executeQuery();
while(rs.next()){
Book B=new Book();
B.setId(rs.getInt("id"));
B.setName(rs.getString("name"));
B.setPrice(rs.getDouble("price"));
B.setBookCount(rs.getInt("bookCount"));
B.setAuthor(rs.getString("author"));
list.add(B);
}
rs.close();
ps.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
return list;
}
public int findCount(){
int count=0;
Connection con=getConnection();
String sql="select count(*)from tb_Book";
try{
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
count=rs.getInt(1);
}
rs.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
return count;
}
}
解决方案 »
- hibernate多对多 第三章表插不进去
- Could not resolve placeholder
- 求页面高手-动态表格
- 用手机提交表单时表单内容乱码
- 求教!关于模糊查询功能的实现!在线等!
- 浏览器滚动条问题 高手来下帮忙看看吧
- 100分求:在jsp中用的文本编辑的代码,象word那样,最好是可以贴图的。分不够再加,加,加......
- 找JSP高手兼职(北京)!!!
- 请问jsp能不能写response.write 这样的语句?等简单的jsp问题。
- 如何对发布EAR包进行加密?
- SSH中struts.xml提示错误,但运行不出错。求解
- hibernate中Query query=session.createQuery的问题
List<Book>list=new ArrayList<Book>();
Connection con=getConnection();
String sql= "select Top '"+size+"' * from tb_book order by id desc";
try{
PreparedStatement ps=con.prepareStatement(sql); ps.setInt(2,Book.PAGE_SIZE);
ResultSet rs=ps.executeQuery();
while(rs.next()){
Book B=new Book();
B.setId(rs.getInt("id"));
B.setName(rs.getString("name"));
B.setPrice(rs.getDouble("price"));
B.setBookCount(rs.getInt("bookCount"));
B.setAuthor(rs.getString("author"));
list.add(B);
}
rs.close();
ps.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
return list;
}行不?