Android sample code which add a record of Access database file (mdb file). It needs three edittext fields and two buttons on layout.
source code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.andaddmdb; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import android.support.v7.app.ActionBarActivity; | |
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; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
button1=(Button)findViewById(R.id.button1); | |
button1.setOnClickListener(this); | |
button2=(Button)findViewById(R.id.button2); | |
button2.setOnClickListener(this); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
@Override | |
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 == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@Override | |
public void onClick(View arg0) { | |
// TODO Auto-generated method stub | |
if(arg0.equals(button1)){ | |
String Hyouji=""; | |
edittext1=(EditText)findViewById(R.id.editText1); | |
edittext2=(EditText)findViewById(R.id.editText2); | |
edittext3=(EditText)findViewById(R.id.editText3); | |
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 { | |
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); | |
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)) { | |
finish(); | |
} | |
} | |
} |
0 件のコメント:
コメントを投稿