Right now this is my code and I would like to know how can i utilize the dataset and insert it in the SQLite. Is there any way that I can access a java class located at my android application which contains the methods (insertDebt() and insertCredit() ) which can instantiated for the dataset to add in SQLite.
JAVA SERVLET:
package com.jnext.webapp;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "SyncDB", urlPatterns = {"/SyncDB"})
public class SyncDB extends HttpServlet {
static Connection con = null;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try{
this.makeConnection();
PreparedStatement ps= con.prepareStatement("");
ResultSet rs=ps.executeQuery();
}catch(Exception e){
System.out.println("Error : "+e);
}
}
public void makeConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
//change DATABASE into table name
con = DriverManager.getConnection("jdbc:mysql://localhost/DATABASE","root","root");
}catch(Exception e){
System.out.println("Error : "+e);
}
}
}
Android Class in android app which adds data to the SQLite:
package com.example.raycarlo.miutangfinalfinal;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
/**
* Created by RayCarlo on 3/27/2016.
*/
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_NAME = "debts_table";
public static final String COLUMN_ID = "id";
public static final String COLUMN_AMOUNT = "amount";
public static final String COLUMN_DUEDATE = "due_date";
public static final String COLUMN_DEBTOR = "debtor";
public static final String TABLE_CREDIT = "credit_table";
public static final String CREDIT_ID = "id";
public static final String CREDIT_AMOUNT = "amount";
public static final String CREDIT_DUEDATE = "due_date";
public static final String CREDIT_DEBTOR = "debtor";
private static final String DATABASE_NAME = "debts.db";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table " + TABLE_NAME + " (" + COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_AMOUNT + " text not null, "
+ COLUMN_DUEDATE + " text, "
+ COLUMN_DEBTOR + " text not null);";
private static final String DATABASE_CREATE_CREDIT = "create table " + TABLE_CREDIT + " (" + CREDIT_ID + " integer primary key autoincrement, "
+ CREDIT_AMOUNT + " text not null, "
+ CREDIT_DUEDATE + " text, "
+ CREDIT_DEBTOR + " text not null);";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE_CREDIT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CREDIT);
onCreate(db);
}
public boolean insertDebt(String amount, String debtor, String due_date){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_AMOUNT, amount);
contentValues.put(COLUMN_DEBTOR, debtor);
contentValues.put(COLUMN_DUEDATE, due_date);
db.insert(TABLE_NAME, null, contentValues);
return true;
}
public boolean insertCredit(String amount, String debtor, String due_date){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(CREDIT_AMOUNT, amount);
contentValues.put(CREDIT_DEBTOR, debtor);
contentValues.put(CREDIT_DUEDATE, due_date);
db.insert(TABLE_CREDIT, null, contentValues);
return true;
}
}
Aucun commentaire:
Enregistrer un commentaire