import java.sql.*;
public class TestPrepStmt {
//将输入的三个参数插入到数据库中//输入的参数放在args中
public static void main(String[] args) {
if(args.length !=2){
System.out.println("Parameter Error ! Please Input Again!");
System.exit(-1);
}
int uid = 0;
//插入的数值要与数据库中的表数据类型匹配
//F3能看到声明的位置 成员变量一般放在一起 都在头上
try{

uid = Integer.getInteger(args[0]);

}catch(NumberFormatException e){
System.out.println("数据类型错误");
System.exit(-1);
}
String uname = args[1];

Connection conn = null;
PreparedStatement pst = null;

try {
Class.forName("com.mysql.jdbc.Driver");

conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?"
+ "user=root&password=root");
pst = conn.prepareStatement("insert into user values (?,?)");
pst.setInt(1, uid);
pst.setString(2, uname);
pst.executeUpdate();

 //String sql = "insert into user values (" + uid + ",'" + uname + "')"; //注意写法
        //System.out.println(sql);//打印检测是否正确
//pst.executeUpdate(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {

if(pst!=null){
pst.close();
pst=null;
}

if(conn!=null){
  conn.close();
  conn=null;
}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}

解决方案 »

  1.   

    debug 看哪里包空指针了 在对症下药了
      

  2.   

    主要问题是uid = Integer.getInteger(args[0]); 应该改为  uid=Integer.parseInt(args[0]);
    还有一个问题你的表名称是user,这个是mysql的关键字,建议你换一个表名称,我这里用的customer
    用下面的代码看看还报错么package csdn.p31;import java.sql.*;
    public class TestPrepStmt {
        //将输入的三个参数插入到数据库中//输入的参数放在args中
        public static void main(String[] args) {
            if(args.length !=2){
                System.out.println("Parameter Error ! Please Input Again!");
                System.exit(-1);
            }
            int uid = 0;
    //插入的数值要与数据库中的表数据类型匹配
    //F3能看到声明的位置 成员变量一般放在一起 都在头上
            try{//            uid = Integer.getInteger(args[0]);
                  uid=Integer.parseInt(args[0]);
            }catch(NumberFormatException e){
                System.out.println("数据类型错误");
                System.exit(-1);
            }
            String uname = args[1];        Connection conn = null;
            PreparedStatement pst = null;        try {
                Class.forName("com.mysql.jdbc.Driver");            conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?"
                        + "user=root&password=root");
                
    //            pst = conn.prepareStatement("insert into user values (?,?)");
                pst = conn.prepareStatement("insert into customer(id,name) values (?,?)");
                pst.setInt(1, uid);
                pst.setString(2, uname);
                pst.executeUpdate();//String sql = "insert into user values (" + uid + ",'" + uname + "')"; //注意写法
    //System.out.println(sql);//打印检测是否正确
    //pst.executeUpdate(sql);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
    // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally{
                try {                if(pst!=null){
                        pst.close();
    //                    pst=null;
                    }                if(conn!=null){
                        conn.close();
    //                    conn=null;
                    }            } catch (SQLException e) {
                // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }}