Skip to content

Instantly share code, notes, and snippets.

Created October 19, 2014 23:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Android appli sample code which add a field of Access mdb database, needs three edittext fileds and two buttons.
package com.example.andaddmdb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import android.text.SpannableStringBuilder;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnClickListener{
private Button button1;
private Button button2;
private EditText edittext1;
private EditText edittext2;
private EditText edittext3;
protected void onCreate(Bundle savedInstanceState) {
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == {
return true;
return super.onOptionsItemSelected(item);
public void onClick(View arg0) {
// TODO Auto-generated method stub
String Hyouji="";
SpannableStringBuilder sb1 = (SpannableStringBuilder)edittext1.getText();
SpannableStringBuilder sb2 = (SpannableStringBuilder)edittext2.getText();
SpannableStringBuilder sb3 = (SpannableStringBuilder)edittext3.getText();
String str1 = sb1.toString();
String str2 = sb2.toString();
String str3 = sb3.toString();
// Toast.makeText(this, "ボタンが押されました。"+str, Toast.LENGTH_LONG).show();
// String dbStr = Environment.getExternalStorageDirectory() + "/dropbox/mdb/foma0.mdb";
String dbStr = Environment.getExternalStorageDirectory() + "/dropbox/mdb/foma0.mdb";
//String dbStr = "sdcard/foma.mdb";
Hyouji = dbStr;
try {
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://" + dbStr + ";memory=true");
// Connection conn=DriverManager.getConnection("jdbc:ucanaccess://" + dbStr );
PreparedStatement stmt = conn.prepareStatement("INSERT into foma1 (name, number,comment) VALUES (?, ?, ?)");
stmt.setString(1, str1);
stmt.setString(2, str2);
stmt.setString(3, str3);
int rowsUpdated = stmt.executeUpdate();
Hyouji=Hyouji + "\n" + "rowsUpdated code is " + rowsUpdated;
}catch (Exception e){
Hyouji=Hyouji + "\n" + e.getMessage();
Toast.makeText(this, Hyouji, Toast.LENGTH_LONG).show();
}else if(arg0.equals(button2)) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment