各位仁兄大家好,我遇到了一个比较困惑的问题,恳请大家助我一臂之力,万分感谢!是在不好意思啊,我只有这么多可用分了!这是数据库中的表 BookInfo
id name author price type publishdate
A123 Java核心技术 Cay S.Horstmann 98 计算机/程序设计 2008-6-1这是查询数据库的代码
package bean;
import java.sql.*;
public class DBConnect {
private static Connection con;
public static Connection getConnection() {
String className = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433;databasename=Book";
String user = "sa";
String password = "sa";
try {
Class.forName(className);
con = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public ResultSet GetBookInfo() {
PreparedStatement ps = null;
String sql = "select * from BookInfo";
ResultSet rs = null; try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
public static void main(String[] args) {
System.out.println(new DBConnect().GetBookInfo());
}
}=====
但是运行之后出现这样的问题java.lang.NullPointerException
at bean.DBConnect.GetBookInfo(DBConnect.java:24)
at bean.DBConnect.main(DBConnect.java:32)
null
=======
ps = con.prepareStatement(sql);->这一行出现问题,不知道为什么啊,着急啊
id name author price type publishdate
A123 Java核心技术 Cay S.Horstmann 98 计算机/程序设计 2008-6-1这是查询数据库的代码
package bean;
import java.sql.*;
public class DBConnect {
private static Connection con;
public static Connection getConnection() {
String className = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433;databasename=Book";
String user = "sa";
String password = "sa";
try {
Class.forName(className);
con = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public ResultSet GetBookInfo() {
PreparedStatement ps = null;
String sql = "select * from BookInfo";
ResultSet rs = null; try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
public static void main(String[] args) {
System.out.println(new DBConnect().GetBookInfo());
}
}=====
但是运行之后出现这样的问题java.lang.NullPointerException
at bean.DBConnect.GetBookInfo(DBConnect.java:24)
at bean.DBConnect.main(DBConnect.java:32)
null
=======
ps = con.prepareStatement(sql);->这一行出现问题,不知道为什么啊,着急啊
解决方案 »
- 基于web的聊天室或者棋牌游戏开发
- JSP用户登录界面,三种用户,用户登录不了,怎么解决
- web.xml的问题
- 怎么蒋一个字符串显示在一个表格中
- 小弟刚开始学"Struts + Spring +hibernate " ,请前辈们指点下,怎么才能学得更好?
- tomcat + sqlserver + struts jsp在家里的winxp下运行的好好的,在单位的win2003服务器上运行老出出错,急死人了,哪位老大能帮忙找一下原因
- 求jsp聊天室
- Struts的Action,根据处理不一样,返回的jsp也不一样,可以直接对不同jsp的Form直接赋值吗?
- 如何实现双纵轴折线图
- web项目访问路径问题
- 在jsp上怎么获得客户端的计算机名称呢??
- 局域网网站 怎样可以让外网访问
PreparedStatement ps = null;
String sql = "select * from BookInfo";
ResultSet rs = null;
List list=new ArrayList();
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
//循环遍历
while(rs.next()){
BookInfo b=new BookInfo();
b.set……
b.set……
b.set……
b.set……
b.set……
b.set……
list.add(b);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public List GetBookInfo() {
PreparedStatement ps = null;
String sql = "select * from BookInfo";
ResultSet rs = null;
List list = new ArrayList();
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next){
Book book = new Book();
book.setId = rs.getString("列名"); //这里取出字段并放入bean
list.add(book);
}
} catch (Exception e) {
e.printStackTrace();
}
return list ;
}
package bean; import java.sql.*;
public class DBConnect { private static Connection con; //声明一个静态Connection引用
//这个方法是返回一个Connection对象,但从来没用过
public static Connection getConnection() {
String className = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433;databasename=Book";
String user = "sa";
String password = "sa";
try {
Class.forName(className);
con = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public ResultSet GetBookInfo() {
PreparedStatement ps = null;
String sql = "select * from BookInfo";
ResultSet rs = null; try {
ps = con.prepareStatement(sql); //看清楚,这行用了con,而你没有对其初始化,空异常报的是它
rs = ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
public static void main(String[] args) {
System.out.println(new DBConnect().GetBookInfo());
}
}
你都没有循环取出resultset
ps = con.prepareStatement(sql); //con还是null当然就NULLPOINTER啦
public class DBConnect { private static Connection con; //声明一个静态Connection引用
//这个方法是返回一个Connection对象,但从来没用过
public static Connection getConnection() {
String className = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433;databasename=Book";
String user = "sa";
String password = "sa";
try {
Class.forName(className);
con = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public ResultSet GetBookInfo() {
PreparedStatement ps = null;
String sql = "select * from BookInfo";
ResultSet rs = null; try {
ps = con.prepareStatement(sql); //看清楚,这行用了con,而你没有对其初始化,空异常报的是它
rs = ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
public static void main(String[] args) {
System.out.println(new DBConnect().GetBookInfo());
}
}
应该是
String url = "jdbc:microsoft:sqlserver://localhost:1433;databasename=Book";
出毛病的地方楼上的已经指出来了。
建议楼主可以考虑 public ResultSet GetBookInfo() 这个方法中调用getConnection()方法
这样就初始化了con这个对象。
还有一点2楼的也提出来了,就是执行SQL之后的返回,用一个LIST集合返回更好一些。
下面是我以前写的一个数据查询的方法,贴给楼主看下,希望对楼主有所帮助
public ArrayList stuByName(String sname){// 创建一个学生集合对象,用来存放查询学生对象的结果
ArrayList<StudentPo> stu = new ArrayList<StudentPo>();
String sql = "select * from s where sname like '"+sname+"'";
try{
this.initConnection();//打开数据库的连接
Connection con = getConnection();
ResultSet rs = con.createStatement().executeQuery(sql);
while(rs.next())
{
StudentPo stup = new StudentPo();
stup.setId(rs.getString("id"));
stup.setSname(rs.getString("sname"));
stup.setSex(rs.getString("sex"));
stup.setBirthday(rs.getString("birthday"));
stu.add(stup); }
}catch(Exception ex){ex.printStackTrace();}
finally{
this.closeConnection();//做完一次查询之后关闭数据库的连接,释放资源,楼主这一点没有考虑到~
System.out.println("selectt success");
}
return stu;
}
执行getConnection()方法啊,所以你的con是空的,下面也就无法执行,
就像楼上说的一样,不要返回ResultSet,最好返回一个List,结果集是要关闭的,
你可以用debug调试看一下吧!