Search code examples
javaandroidmysqljdbcjtds

Insert Row into MySQL database


I am using eclipse with the JDTS driver. I am trying to insert a row into a MySQL database. The connection and statement are generated just fine (I think) However when I run my executeUpdate method, I get a null pointer exception.

I have a DBclient class which handles the connection to the DB, and then I have an Uploader class which will collect all the relevant information and supply it to my insert method.

My DBclient class:

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

}//DBclient

and then my Uploader class

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

}

}//Uploader

The Null pointer exception is thrown from the insertToDB() method from the DBclient class.

Any help will be appreciated.


Solution

  • It turns out that I was having the same issue as the user here

    What I had to do was add the jtds.jar to my assetts folder, add it to my build path, and then in the "Configure Build Path" menu, go to order and export, and check the jar file.