Android端æ«ã§ãã¼ã¿ãã¼ã¹ãµã¼ãã¼ã«ã¢ã¯ã»ã¹
ãååãäºåããããã«ä»åã¯ç§ãã¡ãä½ã£ãDBGrid for Androidã¨JDBC Driver over HTTP for Androidã®ç´¹ä»ã§ãããããã®ããã°ã©ãã³ã°ã³ã³ãã¼ãã³ãã¯ããã¹ã¦ã®ã½ã¼ã¹ã³ã¼ããæä¾ãããéçºè ã¯ç¡åã§çç£ç©ã®åé å¸ãã§ãã¾ãã
ãééãã®ãªãããã«è¨ã£ã¦ããã¨ããã®è¨äºã¯Androidå èµã®ãã¼ã¿ãã¼ã¹SQLLiteãæä½ããæ¹æ³ãç´¹ä»ããããã®ãã®ã§ã¯ããã¾ãããOracleãMySQLãMicrosoft SQL Serverã¨ãã£ããã¼ã¿ãã¼ã¹ãµã¼ãã¼ã«ãAndroidã¯ã©ã¤ã¢ã³ãããã¢ã¯ã»ã¹ããæ¹æ³ã解説ãããã®ã§ãã
ããã¼ã¿ãã¼ã¹ãã³ãã¼ãæä¾ããJDBCãã©ã¤ãã¯ãAndroidæ©ã§ã®åä½ãæ³å®ãã¦ããªãã¨ãããã¨ã¯ååæ¸ãã¾ãããJDBC Driver over HTTP for Androidï¼ä»¥ä¸ JDBC for Androidï¼ã¯ãç¾å¨ã®ãã®ãããªç¶æ³ä¸ã§ãä»ããAndroid端æ«ãã¯ã©ã¤ã¢ã³ãã¨ãã¦ãã¼ã¿ãã¼ã¹ãµã¼ãã¼ã«ã¢ã¯ã»ã¹ããããã°ã©ã ãéçºããããã«æä¾ããã¾ãã
ãã¾ãã2010å¹´12ææç¹ã§å ¬éããã¦ããAndroidã®ãã¼ã¿ãã¼ã¹ããã°ã©ãã³ã°æ å ±ã®ã»ã¨ãã©ã¯ã表形å¼ã®ãã¼ã¿ç·¨éãæ±ã£ã¦ãã¾ããããããå®ç¾ããã®ãDBGrid for Androidã§ãããã®DBã°ãªããã¯JDBCã®ResultSetãªãã¸ã§ã¯ãã¨ã®é£çµæ©è½ãæã¤ããã2è¡ã®ããã°ã©ã ã³ã¼ãã§çµæã»ããã®è¡¨ç¤ºã¨ç·¨éãå¯è½ã«ãªãã¾ãã
ãµã³ãã«ããã°ã©ã ãæ§æããã¯ã©ã¹ç¾¤
ããããªãã§ããããµã³ãã«ããã°ã©ã ãè¦ã¦ãã ããããªã¹ã1ãå®è¡ããç»é¢ãå³1ã§ããä»åæä¾ãããµã³ãã«ããã°ã©ã ã¯ä»¥ä¸ã®ã¯ã©ã¹ããæ§æããã¾ãã
- ExDBã¯ã©ã¹ï¼ãµã³ãã«ããã°ã©ã ï¼
- DBGridã¯ã©ã¹ï¼DBGrid for Androidï¼
- droidbã¯ã©ã¹ï¼ãµã¼ãã¼ã³ã³ãã¼ãã³ãï¼
- Connectionã¯ã©ã¹ï¼ä»¥ä¸ JDBC Driver over HTTP for Androidï¼
- ResultSetã¯ã©ã¹
- DriverManagerã¯ã©ã¹
- ResultSetMetaDataã¯ã©ã¹
- Statemenã¯ã©ã¹
ããªã¹ã1ã¯ãExDBã¯ã©ã¹ã«å«ã¾ããããã°ã©ã ã³ã¼ãã®å ¨æã§ãã
ãã¼ã¿ãã¼ã¹ããã°ã©ãã³ã°ãããã«ã¯ãã©ã®ã¯ã©ã¹ãä¿®æ£ããã°ããã®ã
ãæåã®ExDBã¯ã©ã¹ããã¼ã¿ãã¼ã¹ããã°ã©ãã³ã°ã®ãµã³ãã«ããã°ã©ã ã§ããèªè ããã¼ã¿ãã¼ã¹ã¯ã©ã¤ã¢ã³ãããã°ã©ã ãä½ãã¨ãã¯ããã®ExDBã¯ã©ã¹ãæ¹å¤ããã¨ããã§ããããããã§æä½éã®ãã¼ã¿ãã¼ã¹ããã°ã©ã ã¯ä½æã§ãã¾ããã§ããã2çªç®ã®DBGridã¯ã©ã¹ã«å®è£ ããã¦ããDBGrid for Androidã¯ãååãªããã°ã©ãã³ã°ã¤ã³ã¿ã¼ãã§ã¤ã¹ãåãã¦ãã¾ãããã ãããå®ç¨ã«ãªãããã°ã©ã ãä½ããã¨ãããããã®DBã°ãªããã®ã½ã¼ã¹ãæ¹å¤ãããã¨ãå¿ é ã¨ãªãã¾ãã
DBGrid for Androidã¯ã½ã¼ã¹ã³ã¼ãã®æ¹å¤ã§æ©è½ãå¤æ´å¯è½
ãDBGrid for Androidã®ã½ã¼ã¹ã³ã¼ãã¯ã³ã¡ã³ããå«ã400è¡ç¨åº¦ã®ãã®ãªã®ã§ããããç解ãã¦ããã°ã©ãã³ã°ããã°ããã¼ã¿ã°ãªããç³»ã®ã³ã³ãã¼ãã³ããããæè»ãªããã°ã©ãã³ã°ãå¯è½ã§ããããã«ç§ãã¡ã¯ããã®ãããªããã°ã©ãã³ã°ã¹ã¿ã¤ã«ãææ¡ãã¾ããã«ãã»ã«åãããã¨ã§ããã°ã©ãã³ã°ã®æè»æ§ã«å¶éãå ããå¿ è¦ããªãã§ãããã
ã大ä½ãã¡ãã£ã¨é«æ©è½ãªãã¼ã¿ã°ãªããã³ã³ãã¼ãã³ãã®ããã¥ã¢ã«ã¯ä½ç¾ãã¼ã¸ããã£ããããã®ã§ããããç¿å¾ããå¦ç¿ã³ã¹ãã決ãã¦å°ãªãããã¾ãããæ¬é£è¼ãç§ãã¡ã®ãã¼ã ãã¼ã¸ã§ããDBGrid for Androidãæ¹å¤ããããã°ã©ãã³ã°æ å ±ãæä¾ãã¦ããã¾ãã
ãµã¼ãã¼ã³ã³ãã¼ãã³ã
ãdroidbã¯ã©ã¹ã¯ãWebãµã¼ãã¼ã«é ç½®ããããµã¼ãã¬ããã§ããJDBC for Androidã¯Webãã©ã¦ã¶ã®ãµãããã¦ããã®ãµã¼ãã¬ããã«ã¯ã©ã¤ã¢ã³ãã®è¦æ±ãéä¿¡ãã¾ããè¦æ±ãåãåã£ãdroidbãµã¼ãã¬ããã¯ãé常ã®JDBCãã©ã¤ããã¤ãã£ã¦ãã¼ã¿ãã¼ã¹ã«ã¢ã¯ã»ã¹ãã¾ãããã®ã¯ã©ã¹ã¯ããã¼ã¿ãã¼ã¹ããã°ã©ã ãéçºããããã«ã¯ãé常å¤æ´ããå¿ è¦ã¯ããã¾ããã
JDBC for Androidãå®è£ ããã¯ã©ã¹
ãConnectionã¯ã©ã¹ä»¥éã®ã¯ã©ã¹ã¯JDBC for Androidã®å®è£ ã§ãããããã¯ä¸å ·åãæ©è½ä¸è¶³ããªãéããéçºè ã¯è§¦ããå¿ è¦ã¯ããã¾ããããã¼ã¿ãã¼ã¹ãã³ãã¼ã®JDBCãã©ã¤ãã¨ã®äºææ§ãèããã°ãåæãªæ©è½æ¡å¼µã¯ããªãã»ããããã§ããããã¨ã¯ãããJDBC for Androidãç¬èªå®è£ ã®é¨åã¯ããã¾ããä¾ãã°JDBC for Androidã¯ãä»ã®JDBCãã©ã¤ãã¨éããã¹ãã¼ãã¬ã¹ã®HTTPãéãã¦åä½ãã¾ãããã®ãããã¼ã¿ãã¼ã¹ã®ããã¯ãã§ãã¾ãããããã§ãã¼ã¿åå¾å¾ããç·¨éãããã¼ã¿ãæ¸ãè¾¼ãã¾ã§ã®ããã ã«ãããä»ã®ã¦ã¼ã¶ã¼ããã¼ã¿ãç·¨éãã¦ãããããã¼ã¿ãã¼ã¹ã¸ã®å¤æ´ãæå¦ããä»çµã¿ãããã¾ãã
ãJDBC for Androidã®ã¯ã©ã¹ã¯ã½ã¼ã¹ã³ã¼ãã®æ¹å¤ãåæã¨ãã¦ããªãã®ã§ããããã®ã¯ã©ã¹ãå¥ããã±ã¼ã¸ã«ãã¦ããã¼ã¿ãã¼ã¹ãã³ãã¼ãæä¾ããJDBCãã©ã¤ãã¨åæ§ã«æ±ã£ã¦ãããã§ããã§ãããã£ããã½ã¼ã¹ã³ã¼ããããã®ã ãããããã¸ã§ã¯ãã«çµã¿è¾¼ãã ã»ããè¦éããããã¦ããã§ããããããã«ãã®ã»ããã³ã³ãã¼ãã³ãã®ãã°ã«å¯¾ããä¿®æ£ãæ©ããªãã¾ãã
ã§ããã ãæ°ãããµã³ãã«ã
ãä»åãæä¾ãããããã®ã¯ã©ã¹ãå¸è²©ã®ã³ã³ãã¼ãã³ãã®ããã«ããããªããä»ããç°¡åã«ç¢ºå®ã«ä½¿ãããã®ã ã¨æ³åãã¦ãããªãã°ããã®æå¾ ã¯æ©ãæ¨ã¦ãæ¹ãããã§ãã大ä½ãä»ããã«Androidç¨ã®ãã¼ã¿ãã¼ã¹ã¯ã©ã¤ã¢ã³ãããã°ã©ã ãæ¸ããªããã°ãããªãã¨ãã人ãå¤ãã¯ãªãã§ãããããã®é£è¼ãç¶ãã¦ããéã«ãã¤ã¾ãããã®ãããªããã°ã©ã ãä½ããªããã°ãªããªããªãããã«ã¯ãã¿ãªããã®ã¹ãã«ã®åä¸ãå«ããååã«ä½¿ãããããªãã®ã«ãªã£ã¦ããã¯ãã§ãã
ãã¾ããããä»ããã«Androidç¨ã®ãã¼ã¿ãã¼ã¹ã¯ã©ã¤ã¢ã³ãããã°ã©ã ãä½ããªãã¨ãããªãã¨ãã¦ããä»ã«ããé¸æè¢ããªãã¯ãã§ããè¦å´ãã¦ãããããã®ã³ã³ãã¼ãã³ãã使ãããªã価å¤ããããã¨ã¯ç´æãã¾ããå°ãªãã¨ãã¹ã¯ã©ããããæ¸ãèµ·ããããã¯ãæéã®ç¯ç´ã«ãªãã¯ãã§ãã
ããããã®ã³ã³ãã¼ãã³ãã®ææ°æ å ±ã¯ã次ã®Webãã¼ã¸ã§æä¾ãã¾ãã
ãµã³ãã«ããã°ã©ã ã®ãã¦ã³ãã¼ã
ããã®è¨äºã®ãµã³ãã«ããã°ã©ã ããã¦ã³ãã¼ããã¦å®è¡ããã«ã¯ãWebãµã¼ãã¼ããã¼ã¿ãã¼ã¹ãµã¼ãã¼ãããã³ãã¼ã¿ãã¼ã¹ï¼ãã¼ãã«ãå«ãï¼ãç¨æããå¿ è¦ãããã¾ããã¾ããã¼ãã«ããã¼ã¿ãã¼ã¹ãµã¼ãã¼ã«åããã¦ããµã³ãã«ããã°ã©ã ï¼ãªã¹ã1ï¼ã®æ¥ç¶æååãSELECTã¹ãã¼ãã¡ã³ãã®å 容ãå¤ããå¿ è¦ãããã¾ãã
ããã¦ã³ãã¼ãå¯è½ãªãµã³ãã«ããã°ã©ã ã®ã¤ã³ã¹ãã¼ã«æ¹æ³ã説æããåã«ã次ã®ãã¼ã¸ã§ã¯ãã¼ã¿ãã¼ã¹ããã°ã©ã ã®å®è£ ã§ããExDBã¯ã©ã¹ï¼ãªã¹ã1ï¼ã®å 容ã説æãã¾ãã
package exDB.jar; import android.app.Activity; import android.os.Bundle; import android.widget.LinearLayout; import android.widget.EditText; import android.widget.HorizontalScrollView; import android.view.View; import android.widget.Button; import android.widget.ScrollView; import android.widget.Toast; //import java.sql.DriverManager; //import java.sql.Connection; //import java.sql.ResultSet; public class ExDB extends Activity implements View.OnClickListener { public static Button cmdButton; public static EditText editText; private Connection conn; private ResultSet rSet = null; private DBGrid dBGrid; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout linearLayout=new LinearLayout(this); linearLayout.setOrientation(linearLayout.VERTICAL); cmdButton = new Button(this); cmdButton.setText("ãããæ´æ°"); cmdButton.setHeight(15); cmdButton.setOnClickListener(this); try { // Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/northwind;http://10.0.2.2/droiddb/servlet/jdbchttp", "user", "password"); conn.setAutoCommit(false); rSet = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE) .executeQuery("SELECT CustomerID,CompanyName,ContactName,Address FROM Customers"); } catch(Exception e) { } dBGrid = new DBGrid(this); dBGrid.ColumnTitleList = "ID,åå¼å å,æ å½è ,ä½æ"; dBGrid.CreateGrid(rSet); linearLayout.addView(cmdButton); HorizontalScrollView hScrollView = new HorizontalScrollView(this); ScrollView scrollView = new ScrollView(this); hScrollView.addView(dBGrid); scrollView.addView(hScrollView); linearLayout.addView(scrollView); setContentView(linearLayout); }//onCreate(Bundle savedInstanceState) public void onClick(View view) { if (view==cmdButton) { //ç¾å¨ç·¨éä¸ã®åãDBã«åæ ãããããã dBGrid.update(); conn.commit(); Toast.makeText(this, "ãã¼ã¿ã®æ´æ°ãå®äºãã¾ãã", Toast.LENGTH_SHORT).show(); } }//onClick(View view) }