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)
向各位路过人请教。
{
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)
向各位路过人请教。
aStatement = con.createStatement();
aStatement.executeUpdate(strSQL);
i=Employees.size()-1;
btw:没对aEmployee是否为null做过判断
..还有些其他问题。。嘿嘿
......
try
{
getDBConnection();
aStatement.executeUpdate(strSQL);
i=Employees.size()-1;
}
应该是
try
{
aStatement=getDBConnection().createStatement();
aStatement.executeUpdate(strSQL);
i=Employees.size()-1;
}
其中的220行是aStatement.executeUpdate(strSQL);
应该是
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;
}
Statement 類型的實例就應該是getDBConnection().createStatement();生成。為什么樓主直接就
【Statement aStatement=null;】
然后就
【aStatement.executeUpdate(strSQL);】這個時候aStatement還是null呢,怎么可以?
原来没有初始化
aStatement
添加这句话就没问题了,谢谢各位的提示。
aStatement=aConnection.createStatement();