急!java问题!我的查询语句为什么输不出值!

[复制链接]
查看11 | 回复2 | 2010-9-13 11:04:04 | 显示全部楼层 |阅读模式
package Demo;
import java.sql.*;
public class prepareUpdate {
public String execute()throws Exception{
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=zap";

String userName = "sa";

String password = "123";

ResultSet rs = null;

Connection con=null;



PreparedStatement pstmt=null;


Book book = new Book();

intID = book.getID();



String SQL = "SELECT name,author,price from note where ID= ?";


try{

Class.forName(driverName).newInstance();

con=DriverManager.getConnection(url , userName,


password);

pstmt = con.prepareStatement(SQL) ;



PreparedStatement prestate=con.prepareStatement(SQL);

prestate.setInt(1,ID);



rs = prestate.executeQuery();

while(rs.next()){



book.setName(rs.getString(1));

book.setAuthor(rs.getString(2));

book.setPrice(rs.getString(3));

}

if(SQL != null)

{ return "success";

}

else{

return "fail"; }



}


catch(Exception e){

System.out.println(e.getMessage());

}
finally{

if (rs != null)

try {

rs.close();

} catch (Exception e) {}

if (pstmt != null)

try {

pstmt.close();

} catch (Exception e) {}

if (con != null)

try {

con.close();

} catch (Exception e) {}

}
return SQL;


}
}

回复

使用道具 举报

千问 | 2010-9-13 11:04:04 | 显示全部楼层
Book book = new Book();intID = book.getID();看看上面这两句,Book这个类里面你在构造函数里就把ID那个属性赋值了吗?或者从其它方法赋值了吗?你应该在这两句下面再写句System.out.println("ID:"+ID);看看ID取到值了吗?其它的我没看,我经常用MYSQL,SQL忘的差不多了,你还要看看你是不是确实连接到数据库了。
回复

使用道具 举报

千问 | 2010-9-13 11:04:04 | 显示全部楼层
pstmt = con.prepareStatement(SQL) ;
PreparedStatement prestate=con.prepareStatement(SQL);你这里为什么要写两次?还有你的 intID = book.getID();貌似没有值吧?只是构造了一个空的BOOK对象而已
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行