Java 将行插入MySQL数据库

Java 将行插入MySQL数据库,java,android,mysql,jdbc,jtds,Java,Android,Mysql,Jdbc,Jtds,我正在使用eclipse和JDTS驱动程序。我试图在MySQL数据库中插入一行。连接和语句生成得很好(我想),但是当我运行executeUpdate方法时,我得到了一个空指针异常 我有一个DBclient类来处理与数据库的连接,然后有一个Uploader类来收集所有相关信息并将其提供给我的insert方法 我的DBclient类: public class DBclient { String driver="net.sourceforge.jtds.jdbc"; String URL="xxx

我正在使用eclipse和JDTS驱动程序。我试图在MySQL数据库中插入一行。连接和语句生成得很好(我想),但是当我运行executeUpdate方法时,我得到了一个空指针异常

我有一个DBclient类来处理与数据库的连接,然后有一个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);
}
}//数据库客户端

然后是我的上传类

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);

}
}//上传器

Null指针异常是从DBclient类的insertToDB()方法引发的

任何帮助都将不胜感激

public void insertToDB(String sqlstatement) throws SQLException
{
    int i=statement.executeUpdate(sqlstatement);
}

看起来只有在
DBClient
类的
connect()
中设置了
语句
,但在问题中显示的任何其他代码中都没有调用此方法。因此,
语句
将为null,从中调用方法将导致
NullPointerException

结果表明,我与用户有相同的问题


我需要做的是将jtds.jar添加到我的assetts文件夹中,将其添加到我的构建路径中,然后在“配置构建路径”菜单中,转到order and export,并检查jar文件

另外,在sql资源管理器中运行“testinsert”sql语句也有效。您是正确的。但是,在修复该问题后,我仍然得到相同的NullPointerException。我创建了一个新项目来测试任何连接问题,相同的代码也可以工作。我复制并粘贴了它,除了新类名为tester之外,没有做任何更改。你认为这与最初的android项目有关吗?DBclient是一个标准Java类,被实例化为类扩展活动。在DBclient中,除非被try-catch子句包围,否则它不会运行。这是我能想到的唯一区别,而且我知道驱动程序字符串不正确。它已更改为“net.sourceforge.jtds.jdbc.Driver”,在测试项目中运行良好。@BrockHallenbeck如果仍在同一位置获得NPE,则表示
语句
仍然为空。在分配了
语句
之后,可以尝试将日志记录/调试代码添加到
connect()
中的
try
块中,以确保它不会更早引发异常。根据Android调试环境的设置,如果调用e.printStackTrace(),您可能看不到它的输出。我修改了connect()以返回字符串。如果连接成功=null,否则为“失败”。它返回了失败。所以现在我们知道这种联系根本就没有发生。我完全感到困惑,因为测试项目中相同的代码工作得很好。我们认为这可能是一个防火墙问题,因为android在一个公共wifi网络上,而测试项目不是,但是我只是通过eclipse的emulator运行了它,它与测试程序在同一个网络上,仍然得到“失败”。我想我得用谷歌搜索一下。如果您能在我之前找到任何东西,我们将不胜感激。我想我已经找到了问题所在。尝试获取连接时,我在“net.sourceforge.jtds.jdbc.Driver”上收到一个NoClassDefFoundError和一个ClassNotFoundException。如果研究正确,我需要将jtds jar文件添加到我的类路径中。让我痛苦的是我以为我已经做到了。我将jar文件拖到我的项目中,然后右键单击addtobuildpath。“net.sourceforge.jtds.jdbc”现在位于引用库中的jar文件下。我必须对Manifest.xml做些什么吗?我很难在谷歌上找到任何相关的点击。