Exception in thread "main" java.lang.NullPointerException 哦。关闭之前要判断rs !=null 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 写成if (rs != null) { rs.close();} rs = null了,所以会调用出错,null不能参加运算 rs.close(); 换成if (rs != null) { rs.close();}stmt.close(); 换成if (stmt != null) { stmt.close();} 不行啊,改了之后stmt 又报同样错误,我也像上面写了if语句conn我也做了同样处理,运行的时候console里出现<terminated>JDBCHello[Java Application]E:\java\jdk\bin\javaw.exe(Mar 2,2008 9:30:55 PM)这是什么原因啊? 我个人觉得你的问题应该是con对象为null导致的。所以你检查一下。只要con不为空应该没有问题的。 天啊,神啊。都说了判断null了,连null都搞不定,还玩啥java?}finally{ try { if (rs != null)rs.close(); } catch(SQLException e){ } try { if (stmt != null)stmt.close(); } catch(SQLException e){ } try { if (conn != null)conn.close(); }catch(SQLException e){} } } } 被鄙视了--!我是按上面那样写的可是在console中运行了一下之后标题栏就出现<terminated> JDBCHello[Java Application]E:\java\jdk\bin\javaw.exe(Mar 2,2008 9:30:55 PM) 这次console中没有报错,也没有任何内容,只是在console的标题栏出现了上面的那段文字 经运行测试没有出现问题import java.sql.*;public class JDBCHelloWorld{ /** * @param args */ public static void main(String[] args) { try { Class.forName( "com.mysql.jdbc.Driver" ); } catch ( ClassNotFoundException e ) { e.printStackTrace(); } java.sql.Connection conn = null; java.sql.Statement stmt = null; java.sql.ResultSet rs = null; try { // 获取数据库连接 conn = java.sql.DriverManager .getConnection( "jdbc:mysql//localhost:3306/school?useUnicode=true&characterEncoding=GBK", "root", "1236006" ); // 获取表达式 stmt = conn.createStatement(); // 执行查询 rs = stmt.executeQuery( "select * from student" ); // 显示结果集里面的数据 while ( rs.next() ) { System.out.println( "编号=" + rs.getInt( "id" ) ); System.out.println( "姓名=" + rs.getString( "usename" ) ); System.out.println( "密码=" + rs.getString( "password" ) ); System.out.println( "年龄=" + rs.getInt( "age" ) ); } } catch ( SQLException e ) { e.getStackTrace(); } finally { try { if(rs != null) rs.close(); } catch ( SQLException e ) { } try { if(stmt != null) stmt.close(); } catch ( SQLException e ) { } try { if(conn != null) conn.close(); } catch ( SQLException e ) { } } }} 事件流问题 SSH大结合? structs2无法接收参数的问题... 一个表有多个主键,每一个主键都有外键关联,该如何建模? 新手Hibernate配置SQLServer2000数据库的问题 在Java中如何读取XML字符串的元素值?跪求 Xdoclet标签问题,在线等待,急急急!!! 100分求SQSERVER在JBOSS3.2.6里面的配置(配合JBUILD2005) 用delphi写web services写客户端的问题 JPA的createNativeQuery一个问题 JSP自定义标签的问题 struts1.2中通过验证框架提交后,验证没有起作用???这是为什么?????
rs.close();
}
if (rs != null) {
rs.close();
}
stmt.close(); 换成
if (stmt != null) {
stmt.close();
}
stmt 又报同样错误,我也像上面写了if语句
conn我也做了同样处理,
运行的时候console里出现
<terminated>JDBCHello[Java Application]E:\java\jdk\bin\javaw.exe(Mar 2,2008 9:30:55 PM)
这是什么原因啊?
我个人觉得你的问题应该是con对象为null导致的。所以你检查一下。只要con不为空应该没有问题的。
神啊。都说了判断null了,连null都搞不定,还玩啥java?}finally{
try {
if (rs != null)
rs.close();
} catch(SQLException e){
}
try {
if (stmt != null)
stmt.close();
} catch(SQLException e){
}
try {
if (conn != null)
conn.close();
}catch(SQLException e){}
} }
}
我是按上面那样写的
可是在console中运行了一下之后标题栏就出现
<terminated> JDBCHello[Java Application]E:\java\jdk\bin\javaw.exe(Mar 2,2008 9:30:55 PM)
这次console中没有报错,也没有任何内容,只是在console的标题栏出现了上面的那段文字
{ /**
* @param args
*/
public static void main(String[] args)
{ try
{
Class.forName( "com.mysql.jdbc.Driver" );
}
catch ( ClassNotFoundException e )
{
e.printStackTrace();
}
java.sql.Connection conn = null;
java.sql.Statement stmt = null;
java.sql.ResultSet rs = null;
try
{
// 获取数据库连接
conn = java.sql.DriverManager
.getConnection(
"jdbc:mysql//localhost:3306/school?useUnicode=true&characterEncoding=GBK",
"root", "1236006" );
// 获取表达式
stmt = conn.createStatement();
// 执行查询
rs = stmt.executeQuery( "select * from student" );
// 显示结果集里面的数据
while ( rs.next() )
{
System.out.println( "编号=" + rs.getInt( "id" ) );
System.out.println( "姓名=" + rs.getString( "usename" ) );
System.out.println( "密码=" + rs.getString( "password" ) );
System.out.println( "年龄=" + rs.getInt( "age" ) );
}
}
catch ( SQLException e )
{
e.getStackTrace();
}
finally
{
try
{
if(rs != null)
rs.close();
}
catch ( SQLException e )
{
}
try
{
if(stmt != null)
stmt.close();
}
catch ( SQLException e )
{
}
try
{
if(conn != null)
conn.close();
}
catch ( SQLException e )
{
}
} }
}