http://web.sfc.wide.ad.jp/~tinaba/tutorials/AndroidCS/
JDBCを始めてつかうためこちらのサンプルコード実行してみました。
java
1package com.example.ryo.test2; 2 3import android.app.Activity; 4import android.os.AsyncTask; 5import android.os.Bundle; 6import android.view.View; 7import android.widget.Button; 8import android.widget.EditText; 9import android.widget.TextView; 10import android.widget.Toast; 11import java.sql.Connection; 12import java.sql.DriverManager; 13import java.sql.ResultSet; 14import java.sql.Statement; 15 16public class MainActivity extends Activity implements View.OnClickListener{ 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 Button btn1 = (Button)this.findViewById(R.id.btn1); 23 Button btn2 = (Button)this.findViewById(R.id.btn2); 24 btn1.setOnClickListener(this); 25 btn2.setOnClickListener(this); 26 } 27 28 @Override 29 public void onClick(View v) { 30 // ボタン押下時の動作 31 if(v.getId()==R.id.btn1){ // ボタン1が押下されたら 32 TaskDbInsert task1 = new TaskDbInsert(MainActivity.this); 33 task1.execute(); 34 } 35 else if(v.getId()==R.id.btn2){ // ボタン2が押下されたら 36 TaskDbQuery task2 = new TaskDbQuery(MainActivity.this); 37 task2.execute(); 38 } 39 40 } 41 42} 43class TaskDbInsert extends AsyncTask <Void,Void,Void >{ 44 // この中をこれから記述 45 Activity activity = null; 46 // コンストラクタ 47 public TaskDbInsert(Activity act){ 48 activity = act; 49 } 50 51 @Override 52 protected Void doInBackground(Void... voids) { 53 // 入力部の定義 54 EditText ed =(EditText)activity.findViewById(R.id.inputTxt); 55 String inputTxt=ed.getText().toString(); 56 // DB接続と書き込み 57 try{ 58 Class.forName("com.mysql.jdbc.Driver"); 59 Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/memo_db","root","自分で設定したPASSWORD"); 60 Statement stmt=conn.createStatement(); 61 String sql="insert into memo_db.memo_tbl (memo) values ('"+inputTxt+"')"; 62 stmt.executeUpdate(sql); 63 stmt.close(); 64 conn.close(); 65 }catch(Exception e){ 66 67 } 68 return null; 69 } 70 71 protected void onPostExecute(){ 72 Toast.makeText(activity,"登録を終了しました", Toast.LENGTH_LONG).show(); 73 } 74 75} 76 77class TaskDbQuery extends AsyncTask<Void, Void, String> { 78 // この中をこれから記述 79 Activity activity = null; 80 81 // コンストラクター 82 public TaskDbQuery(Activity act) { 83 activity = act; 84 } 85 86 @Override 87 protected String doInBackground(Void... params) { 88 // 検索語の入力部分の定義 89 EditText ed = (EditText) activity.findViewById(R.id.searchTxt); 90 String qryText = ed.getText().toString(); 91 // 検索結果用の文字列 92 String rsText = ""; 93 // DB接続と検索実行 94 try { 95 Class.forName("com.mysql.jdbc.Driver"); 96 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/memo_db", "root", "自分で設定したPASSWORD"); 97 Statement stmt = conn.createStatement(); 98 String sql = "select memo from memo_tbl where memo like '%" + qryText + "%'"; 99 ResultSet rs = stmt.executeQuery(sql); 100 int rsCnt = 0; 101 while (rs.next()) { 102 rsText += rs.getString(1) + "\n"; 103 rsCnt++; 104 } 105 // 検索結果が多すぎるとき、少なすぎるときの処理 106 if (rsCnt == 0) { 107 rsText = "検索結果がありません。キーワードを変更してください。"; 108 } else if (rsCnt > 5) { 109 rsText = "5個以上見つかりました。キーワードを変更してください。"; 110 } 111 rs.close(); 112 stmt.close(); 113 conn.close(); 114 } catch (Exception e) { 115 rsText = e.getMessage(); 116 } 117 return rsText; 118 } 119 120 protected void onPostExecute(String result){ 121 TextView tv = (TextView)activity.findViewById(R.id.result); 122 tv.setText(result); 123 } 124 125} 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
xml
1<?xml version="1.0" encoding="utf-8"?> 2<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="com.example.ryo.test2.MainActivity"> 8 9 10 <LinearLayout 11 android:layout_width="fill_parent" 12 android:layout_height="fill_parent" 13 android:orientation="vertical"> 14 15 <TextView 16 android:id="@+id/input" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:text="メモ記入" /> 20 21 <EditText 22 android:id="@+id/inputTxt" 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:layout_weight="3" 26 android:ems="10" 27 android:inputType="textMultiLine" /> 28 29 <Button 30 android:id="@+id/btn1" 31 android:layout_width="match_parent" 32 android:layout_height="wrap_content" 33 android:layout_weight="1" 34 android:text="記録" /> 35 36 <TextView 37 android:id="@+id/output" 38 android:layout_width="match_parent" 39 android:layout_height="wrap_content" 40 android:text="表示" /> 41 42 <EditText 43 android:id="@+id/searchTxt" 44 android:layout_width="match_parent" 45 android:layout_height="wrap_content" 46 android:layout_weight="3" 47 android:ems="10" 48 android:inputType="textMultiLine" /> 49 50 <Button 51 android:id="@+id/btn2" 52 android:layout_width="match_parent" 53 android:layout_height="wrap_content" 54 android:layout_weight="1" 55 android:text="検索" /> 56 57 <TextView 58 android:id="@+id/result" 59 android:layout_width="match_parent" 60 android:layout_height="wrap_content" 61 android:text="ここにメモが表示されます" /> 62 63 </LinearLayout> 64 65 66</ScrollView> 67
です。
android studio上ではエラー警告はありませんでしたが、
APP上では、
記録用ボタンを押した際
protected void onPostExecute(){
Toast.makeText(activity,"登録を終了しました", Toast.LENGTH_LONG).show();
}
が表示されなかったり、、
検索部分では、
communications linl failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
とアプリ上で表示されます。
ご教授お願いします。
追加情報
環境
JAVA,androidstudio,mysql
MYSQL Workbench
windows
上記はすべて最新
IPアドレス:10.25.93.90
MySQL [(none)]> use memo_db;
Database changed
MySQL [memo_db]> select*from memo_db.memo_tbl;
+----+--------+
| id | memo |
+----+--------+
| 1 | りんご |
+----+--------+
1 row in set (0.03 sec)
MySQL [memo_db]>
log.eで表示されているWRNING
10-29 16:40:23.936 9887-9887/com.example.ryo.test2 W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
10-29 16:40:23.936 9887-9887/com.example.ryo.test2 W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
10-29 16:40:23.936 9887-9887/com.example.ryo.test2 W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
10-29 16:40:23.936 9887-9887/com.example.ryo.test2 W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。