日期:2014-05-20  浏览次数:20891 次

JDBC executeQuery问题
import java.sql.*;
import java.util.*;
import java.io.*;
public class Charu {
   
public String driver;
Connection conn;
public String pass;
public String user;
public String url;
Statement sta;

public void ininpram(String wenjian) throws IOException
{
Properties props=new Properties();
props.load(new FileInputStream(wenjian));
driver=props.getProperty("driver");
 
pass=props.getProperty("pass");
user=props.getProperty("user");
url=props.getProperty("url");
 
 
 
 
}
public ResultSet inser(String sql) throws Exception
{ try
{
  // Class.forName(driver);
  //conn=DriverManager.getConnection(url,user,pass);
  Class.forName(driver);
conn=DriverManager.getConnection(url,user,pass);
  sta=conn.createStatement();
  ResultSet re=sta.executeQuery(sql);
  return re;
}
finally{

if(sta!=null)
{
sta.close();
}
if(conn!=null)
{
conn.close();
}
}
}
public static void main(String[] args) throws Exception
{
Charu ed=new Charu();

ResultSet res=ed.inser("select * from student");
while (res.next())
{
System.out.println(res.getInt(1)+"\t"+
res.getInt(1)+"\t"+
res.getInt(1)+"\t"+
res.getInt(1)+"\t");
}

}

}


用eclipe提示

Exception in thread "main" java.lang.NullPointerException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Charu.inser(Charu.java:32)
at Charu.main(Charu.java:54)


------解决方案--------------------
没找到驱动类,加入jar包
------解决方案--------------------
空指针异常,我建议你检查一下配置文件,配置文件的读取代码有问题,配置文件的路径名你都没穿进去,如果代码是你抄的书上的,我建议你仔细对一对,
------解决方案--------------------
估计是配置文件的问题。
Java code

Properties props=new Properties();    
props.load(new FileInputStream(wenjian));  // 这里wenjian路径对了没?
driver=props.getProperty("driver");