日期:2014-05-18  浏览次数:20818 次

请教:Exception in thread "main" java.lang.NullPointerException
运行内容如下的UseDriver.java文件,


import   java.sql.*;
public   class   UseDriver   {
public   static   void   main(String[]   args)   {
String   url   =   "jdbc:mysql://localhost/sql_test ";
String   userName   =   "root ";
String   password   =   "   ";
String   sql   =   null;
Connection   conn   =   null;
Statement   stmt   =   null;
try   {
//第一步:加载驱动器
Class.forName( "com.mysql.jdbc.Driver ");
}   catch(ClassNotFoundException   e)   {
System.err.print( "ClassNotFoundException ");
}

try   {
                        //第二步:调用DriverManager.getConnection静态方法得到数据库连接
conn   =   DriverManager.getConnection(url,   userName,   password);
//创建Statement语句
stmt   =   conn.createStatement();
sql   =   "INSERT   INTO   student   "   +
            "VALUES( '12 ',   'zhangjun ',   'tianjin ',   '1981-01-01 ') ";
                        //使用Statement语句对象执行SQL语句
stmt.executeUpdate( "DELETE   FROM   student   WHERE   stu_id= '12 ' ");
stmt.executeUpdate(sql);
System.out.println( "Insert   a   row   successful! ");
}   catch(SQLException   e)   {
System.err.println( "Insert   SQLException ");
}   finally   {
//关闭语句和数据库连接
try   {
stmt.close();
conn.close();
}   catch(SQLException   e)   {
System.err.println( "Close   SQLException ");
}
}

}
}


在DOS窗体下运行,不知怎么出现如下的错误,请问一下这可能是什么错误啊,谢谢!


G:\> javac   UseDriver.java

G:\> java   UseDriver
Insert   SQLException
Exception   in   thread   "main "   java.lang.NullPointerException
                at   UseDriver.main(UseDriver.java:35)


------解决方案--------------------
String password = " ";
===
密码是一个空格?
------解决方案--------------------
stmt.executeUpdate( "DELETE FROM student WHERE stu_id= '12 ' ");
stmt.executeUpdate(sql);
System.out.println( "Insert a row successful! ");
} catch(SQLException e) {
e.printStch....();//这里测试一下.看看具体错在哪里了
System.err.println( "Insert SQLException ");
} finally {

------解决方案------