Android appli development sample code which acccess to the Access database mdb file.
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.andviewmdb; | |
import android.support.v7.app.ActionBarActivity; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.app.Activity; | |
import android.view.Menu; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
import java.io.IOException; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import net.ucanaccess.converters.TypesMap.AccessType; | |
import net.ucanaccess.ext.FunctionType; | |
import net.ucanaccess.jdbc.UcanaccessConnection; | |
import net.ucanaccess.jdbc.UcanaccessDriver; | |
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
String Hyouji=""; | |
// LinearLayout layout = new LinearLayout(this); | |
// layout.setOrientation(LinearLayout.VERTICAL); | |
// setContentView(layout); | |
// 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 ); | |
Statement s = conn.createStatement(); | |
ResultSet rs = s.executeQuery("SELECT [name],[number],[comment] FROM [foma1]"); | |
while (rs.next()) { | |
// System.out.println(rs.getString(1)); | |
Hyouji=Hyouji + "\n" + rs.getString(1) + " " + rs.getString(2)+ " " + rs.getString(3); | |
} | |
} catch (Exception e) { | |
// e.printStackTrace(); | |
Hyouji=Hyouji + "\n" + e.getMessage(); | |
} | |
//System.out.println(Hyouji); | |
/* | |
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) VALUES (?, ?)"); | |
stmt.setString(1, "山田 太郎"); | |
stmt.setString(2, "0120-111-2222"); | |
int rowsUpdated = stmt.executeUpdate(); | |
Hyouji=Hyouji + "\n" + "rowsUpdated code is " + rowsUpdated; | |
}catch (Exception e){ | |
Hyouji=Hyouji + "\n" + e.getMessage(); | |
} | |
*/ | |
// TextView tv = new TextView(this); | |
// tv.setText(String.format("%s", Hyouji)); | |
// layout.addView(tv); | |
TextView textSetting = (TextView) findViewById(R.id.textView1); | |
textSetting.setText(Hyouji); | |
} | |
@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; | |
} | |
} |
0 件のコメント:
コメントを投稿