Apparently you need to access a table column, but I cannot say exactly what type of component you are using, so my best suggestion is to use the EntityManager
of JPA.
Start by creating a class that will open the connection to the database, as in the example below.
private static ConnectionFactory instance;
private final EntityManagerFactory factory;
private ConnectionFactory() {
factory = Persistence.createEntityManagerFactory("nome da persistence unit"));
}
private EntityManager getEntityManager() {
return factory.createEntityManager();
}
public static EntityManager getConnection() {
if (instance == null) {
instance = new ConnectionFactory();
}
return instance.getEntityManager();
}
After that, create an Entity class, which will be responsible for receiving the recovered table data by another class accessing the database, example: "Summed".
And finally create the data access class and use a Query
to recover the data from the table column you want to carry out the sum.
public Integer soma() {
EntityManager em = getEntityManager();
em.getTransaction().begin();
Query q = em.createQuery("SELECT sum(coluna) from tabela");
Integer soma = (Integer) q.getSingleResult();
em.getTransaction().commit();
em.close();
return soma;
}
EDIT 1
Here is the code for a SELECT command in the Sqlite database that can be used on Android systems. See this question: Android Sqlite SELECT
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT sum(column1) FROM table ", null);
if(c.moveToFirst()){
do{
//Recuperando valores
int column1 = c.getInt(0);
//Fazendo algo com os valores
}while(c.moveToNext());
}
c.close();
db.close();
And the Javascript tag?
– Pablo Almeida