我正在使用带有 JDTS 驱动程序的 eclipse。我正在尝试在 MySQL 数据库中插入一行。连接和语句生成得很好(我认为)但是当我运行我的 executeUpdate 方法时,我得到一个空指针异常。
我有一个 DBclient 类来处理与 DB 的连接,然后我有一个 Uploader 类,它将收集所有相关信息并将其提供给我的 insert 方法。
我的 DBclient 类:
public class DBclient {
String driver="net.sourceforge.jtds.jdbc";
String URL="xxxx";
String user="xxxx";
String pass="xxxx";
Connection connection;
Statement statement;
ResultSet rs;
public void connect() throws SQLException
{
try {
Class.forName(driver);
}//try
catch (ClassNotFoundException e)
{
e.printStackTrace();
}//catch
try {
connection=DriverManager.getConnection(URL, user, pass);
statement=connection.createStatement();
} //try
catch (SQLException e)
{
e.printStackTrace();
}//catch
}//connect
public void insertToDB(String sqlstatement) throws SQLException
{
int i=statement.executeUpdate(sqlstatement);
}
}//数据库客户端
然后是我的 Uploader 类
private String testinsert="insert into xxxx (Latitude, Longitude) values (1, 2)";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.uploader);
initialize();
getaddress();
update();
try {
client.insertToDB(testinsert);
} catch (SQLException e) {
// TODO Auto-generated catch block
Toast t=Toast.makeText(getApplicationContext(), "oops", 4);
t.show();
}
}//onCreate
public void initialize()
{
client = new DBclient();
geocoder = new Geocoder(this);
Bundle coords = getIntent().getExtras();
street = (TextView) findViewById(R.id.street);
town = (TextView) findViewById(R.id.town);
state = (TextView) findViewById(R.id.state);
country = (TextView) findViewById(R.id.country);
lat=coords.getDouble("Latitude");
lon=coords.getDouble("Longitude");
}//initialize
public void update()
{
street.setText(address.getAddressLine(0));
town.setText(address.getLocality());
state.setText(address.getAdminArea());
country.setText(address.getCountryName());
}//update
public void getaddress()
{
try
{
addresslist = geocoder.getFromLocation(lat, lon, 1);
} //try
catch (IOException e)
{
Toast t=Toast.makeText(getApplicationContext(), "Input Error", 2);
t.show();
}//catch
address=addresslist.get(0);
}
}//上传者
空指针异常是从 DBclient 类的 insertToDB() 方法引发的。
任何帮助将不胜感激。