Через Android хочу подключиться к базе MySQL. Использую стандартный пример JDBC. Нет явной ошибки, точно такой-же код компилирую в IntelliJ - работает.
Ошибка:
java.lang.NullPointerException: Attempt to invoke interface method void java.sql.Connection.close()' on a null object reference
String query = "select count(*) from books";
try {
// opening database connection to MySQL server
con = DriverManager.getConnection(url, user, password);
// getting Statement object to execute query
stmt = con.createStatement();
// executing SELECT query
rs = stmt.executeQuery(query);
while (rs.next()) {
int count = rs.getInt(1);
text.setText("Total number of books in the table : " + count);
}
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
} finally {
//close connection ,stmt and resultset here
try { con.close(); }
catch(SQLException se) { /*can't do anything */ }
try { stmt.close(); }
catch(SQLException se) { /*can't do anything */ }
try { rs.close(); }
catch(SQLException se) { /*can't do anything */ }
}