Android : jdbc in android, NullPointerException thrown

on Wednesday, April 1, 2015


public class MainActivity extends ActionBarActivity {



String id ;
String name ;
int price;

private ResultSet result;
private Connection con;
String url = "jdbc:mysql://localhost:3306/fyp_restaurant";
String username = "root";
String password = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

try{
Class.forName("com.mysql.jdbc.Driver").newInstance();

}catch(Exception e){
System.err.println("Cannot create connection");
}

try{
con = DriverManager.getConnection(url, username, password);



Statement statement = con.createStatement();

String query = "SELECT * FROM product";
result = statement.executeQuery(query);
}catch(Exception e){
System.err.println("Error");
}

try {

id = result.getString("id");
name = result.getString("name");
price = result.getInt("price");
}catch (Exception e){
System.out.println(e.getMessage());
}


//Toast.makeText(getApplicationContext(), "ID" + id + "\nName" + name + "\nPrice" + price,Toast.LENGTH_SHORT).show();
}


I dont know whether my jar file put in the correct directory, is that libs? I am using android studio. Do I need to add it as library in android studio? Please help!


0 comments:

Post a Comment