JDBC出错
import java.sql.*;
import java.awt.*;
public class JDBCtest{
public static void main(String args[])
{
String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=issa ";
Connection con=null;
String user= "sa ";
String password= " ";
Statement sm=null;
ResultSet rs=null;
try
{
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ").newInstance();
}
catch(Exception e)
{
System.out.println( "失败 ");
return;
}
try
{
con=DriverManager.getConnection(url,user, password);
sm=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=sm.executeQuery( "select * from shop ");
System.out.println( "结果为 ");
while(rs.next())
{
int num=rs.getInt(1);
String thingname=rs.getString(2);
String type=rs.getString(3);
double cost=rs.getDouble(4);
System.out.println(num+thingname+type+cost);
}
}
catch(SQLException e)
{}
finally
{
try
{
rs.close();
sm.close();
con.close();
}
catch(SQLException e)
{}
}
}
}
报rs.close这行,NULLPOINTEXCEPTION运行出错。好象连不到DATABASE里的数据。
[解决办法]
catch(SQLException e)
{
把错误信息打印出来
}