当前位置: 代码迷 >> J2SE >> 小弟我的添加程序出错了,请各位请问,小弟我是新手
  详细解决方案

小弟我的添加程序出错了,请各位请问,小弟我是新手

热度:211   发布时间:2016-04-24 13:25:14.0
我的添加程序出错了,请各位请教,我是新手。
Java code
public static void AddNew(Employeea aEmployee)    {        Statement aStatement=null;        String strSQL;        strSQL="INSERT INTO Employees";        strSQL+=" (Address,BirthDate,Country,Extension,FirstName,LastName,HireDate,";        strSQL+=" HomePhone,Notes,PostalCode,Region,City,ReportsTO,Title,TitleOfCourtesy,photoPath)";        strSQL+=" VALUES (";        strSQL+="'"+ aEmployee.getAddress()+"'"+",";        strSQL+="'"+ aEmployee.getBirthDate()+"'"+",";        strSQL+="'"+ aEmployee.getCountry()+"'"+",";        strSQL+="'"+ aEmployee.getExtension()+"'"+",";        strSQL+="'"+ aEmployee.getFirstName()+"'"+",";        strSQL+="'"+ aEmployee.getLastName()+"'"+",";        strSQL+="'"+ aEmployee.getHireDate()+"'"+",";        strSQL+="'"+ aEmployee.getHomePhone()+"'"+",";        strSQL+="'"+ aEmployee.getNotes()+"'"+",";        strSQL+="'"+ aEmployee.getPostalCode()+"'"+",";        strSQL+="'"+ aEmployee.getRegion()+"'"+",";        strSQL+="'"+ aEmployee.getCity()+"'"+",";        strSQL+="'"+ aEmployee.getReportsTo()+"'"+",";        strSQL+="'"+ aEmployee.getTitle()+"'"+",";        strSQL+="'"+ aEmployee.getTitleOfCourtesy()+"'"+",";        strSQL+="'"+ aEmployee.getphotoPath()+"')";        try        {            getDBConnection();                    aStatement.executeUpdate(strSQL);            i=Employees.size()-1;        }        catch(SQLException e)        {            JOptionPane.showMessageDialog(null,e.getMessage(),"ERROR",JOptionPane.ERROR_MESSAGE);        }        finally        {                        closeStatement(aStatement);            closeConnection();        }            }

不知道这样做添加程序有什么错误吗?我在Eclipse运行报出这样的错误
java.lang.NullPointerException
at gui.EmployeeDA.AddNew(EmployeeDA.java:220)
向各位路过人请教。

------解决方案--------------------
Connection con = getDBConnection();
 aStatement = con.createStatement();
 aStatement.executeUpdate(strSQL);
i=Employees.size()-1;

------解决方案--------------------
Statement aStatement=null;
......
try
{
getDBConnection();
aStatement.executeUpdate(strSQL);
i=Employees.size()-1;
}
应该是
try
{

aStatement=getDBConnection().createStatement(); 
aStatement.executeUpdate(strSQL);
i=Employees.size()-1;
}

------解决方案--------------------
樓上的似乎都有道理。
应该是 
try 


aStatement=getDBConnection().createStatement();
aStatement.executeUpdate(strSQL); 
i=Employees.size()-1; 

另外,那一行是220行啊?這個對于解決問題很重要的。
還有就是插入的數據NULL什么的判斷,也要根據DB定義狀況注意一下。
------解决方案--------------------
好,這樣的話應該是這樣的
try
{

aStatement=getDBConnection().createStatement();
aStatement.executeUpdate(strSQL);
i=Employees.size()-1;
}
  相关解决方案