撰写LinterPersist.java类代码
// ……..
public class LinterPersist extends ContentProvider {
private static final String LINTER_TABLE_NAME = "Student123";
private Connection con;
@Override public boolean onCreate() {
try {
Class.forName("com.relx.jdbc.LinterDriver").newInstance();
con = DriverManager.getConnection(
"jdbc:Linter:linapid:localhost:1070:local", "SYSTEM",
"MANAGER");
} catch(Exception e) { Log.e("Conn failed", e.toString());}
return false; }
try{ Statement stmt = con.createStatement();
stmt.executeUpdate("drop table " + LINTER_TABLE_NAME);
} catch (Exception e)
{ Log.e("drop table failed", e.toString()); }
try { Statement stmt = con.createStatement();
stmt.executeUpdate( "create table " +
LINTER_TABLE_NAME + " (stud_no char(10), stud_name char(20));");
PreparedStatement prepstmt1 = con.prepareStatement(
"insert into " + LINTER_TABLE_NAME + " values (?,?);");
prepstmt1.setString(1, "linter_5"); prepstmt1.setString(2, "Lisa");
prepstmt1.executeUpdate();
prepstmt1.setString(1, "linter_8"); prepstmt1.setString(2, "Kitty");
prepstmt1.executeUpdate( );
} catch (Exception e){ Log.e("create/insert table failed", e.toString());
return false; }
return true;
}
@Override public Cursor query(Uri uri, String[]
projection, String selection,
String[] selectionArgs, String sortOrder) {
ResultSet rs = null;
try { Statement stmt = con.createStatement();
rs = stmt.executeQuery("select * from " +
LINTER_TABLE_NAME);
} catch (Exception e) { e.printStackTrace();
return null; }
Cursor c = new LinterCursor(rs, con);
return c;
}
@Override public String getType(Uri uri) {
return null;
}
@Override public Uri insert(Uri uri, ContentValues initialValues) {
String field_1 = initialValues.get("stud_no").toString();
String field_2 = initialValues.get("stud_name").toString();
try{ PreparedStatement prepstmt1 = con.prepareStatement(
"insert into " + LINTER_TABLE_NAME + " values (?,?);");
prepstmt1.setString(1, field_1);
prepstmt1.setString(2, field_2);
prepstmt1.executeUpdate();
} catch (Exception e) { Log.e("ERROR", e.toString()); }
return uri;
}
@Override public int delete(Uri uri, String where, String[]
whereArgs) { return 0; }
@Override public int update(Uri uri, ContentValues values, String
where,String[] whereArgs) { return 0; }
撰写LinterCursor类代码
class LinterCursor implements Cursor{
ResultSet res; Connection conn;
LinterCursor( ResultSet rs, Connection con)
{ res = rs; conn = con; }
@Override public void close() {
try { res.close(); conn.close();
} catch (java.sql.SQLException e) { e.printStackTrace(); }
}
@Override
public void copyStringToBuffer(int columnIndex,
CharArrayBuffer buffer) {}
@Override public void deactivate() {}
@Override public byte[] getBlob(int columnIndex) { return null; }
@Override public int getColumnCount() { return 0; }
@Override public int getColumnIndex(String columnName)
{ return 0; }
@Override
public int getColumnIndexOrThrow(String columnName)
throws IllegalArgumentException { return 0; }
@Override public String getColumnName(int columnIndex)
{ return null; }
@Override public String[] getColumnNames() { return null; }
@Override public int getCount() { return 0; }
@Override public double getDouble(int columnIndex)
{ return 0; }
@Override public Bundle getExtras() { return null; }
@Override public float getFloat(int columnIndex) { return 0; }
@Override public int getInt(int columnIndex) { return 0; }
@Override public long getLong(int columnIndex) { return 0; }
@Override public int getPosition() { return 0; }
@Override public short getShort(int columnIndex) { return 0; }
@Override public String getString(int columnIndex) {
try { return res.getString(columnIndex + 1);
} catch (java.sql.SQLException e) { e.printStackTrace(); }
return null; }
@Override public boolean getWantsAllOnMoveCalls()
{ return false; }
@Override public boolean isAfterLast() {
try { return res.isAfterLast();
} catch (java.sql.SQLException e)
{ e.printStackTrace(); return false; }}
@Override public boolean isBeforeFirst() {
try { return res.isBeforeFirst();
} catch (java.sql.SQLException e)
{ e.printStackTrace(); return false; }}
@Override public boolean isClosed() { return false; }
@Override public boolean isFirst() { return false; }
@Override public boolean isLast() {return false; }
@Override public boolean isNull(int columnIndex) { return false; }
@Override public boolean move(int offset) { return false; }
@Override public boolean moveToFirst() {
try { return res.first();
} catch (java.sql.SQLException e)
{ e.printStackTrace(); return false; }}
@Override public boolean moveToLast() { return false; }
@Override public boolean moveToNext() {
try { return res.next();
} catch (java.sql.SQLException e)
{e.printStackTrace(); return false; }}
@Override public boolean moveToPosition(int position)
{ return false; }
@Override public boolean moveToPrevious() { return false; }
@Override public void registerContentObserver(
ContentObserver observer) {}
@Override public void registerDataSetObserver(
DataSetObserver observer) {}
@Override public boolean requery() { return false; }
@Override public Bundle respond(Bundle extras) { return null; }
@Override public void setNotificationUri(
ContentResolver cr, Uri uri) {}
@Override public void unregisterContentObserver(
ContentObserver observer) {}
@Override public void unregisterDataSetObserver(
DataSetObserver observer) {}
}}
撰写DispActivity类代码
// …………
public class DispActivity extends ListActivity {
public static final String AUTHORITY
= "com.misoo.provider.rx09-04";
public static final Uri CONTENT_URI
= Uri.parse("content://" + AUTHORITY +
/"/Stud202");
private static final String[] PROJECTION
= new String[] { "stud_no", "stud_name" };
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent.getData() == null) { intent.setData(CONTENT_URI); }
Cursor cursor = getContentResolver().query(
getIntent().getData(),PROJECTION, null, null, null);
if(cursor == null) return;
ArrayList<Map<String, Object>> coll
= new ArrayList<Map<String, Object>>();
Map<String, Object> item;
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
item = new HashMap<String, Object>();
item.put("c1", cursor.getString(0) + ", " +
cursor.getString(1));
coll.add(item);
cursor.moveToNext();
}
this.setListAdapter(new SimpleAdapter(this, coll,
android.R.layout.simple_list_item_1, new String[] { "c1" },
new int[] {android.R.id.text1}));
}
@Override
protected void onListItemClick(ListView l, View v, int position,
long id)
{ finish(); }
}
在LinterPersist类里的查詢(query)指令:
@Override public Cursor query(Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder)
{
ResultSet rs = null;
try { Statement stmt = con.createStatement();
rs = stmt.executeQuery("select * from " + LINTER_TABLE_NAME);
} catch (Exception e)
{ e.printStackTrace(); return null; }
Cursor c = new LinterCursor(rs, con);
return c;
}