JDBC動作確認ができない。。。。
- 評価
- クリップ 0
- VIEW 1,103
http://web.sfc.wide.ad.jp/~tinaba/tutorials/AndroidCS/
JDBCを始めてつかうためこちらのサンプルコード実行してみました。
package com.example.ryo.test2;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MainActivity extends Activity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button)this.findViewById(R.id.btn1);
Button btn2 = (Button)this.findViewById(R.id.btn2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// ボタン押下時の動作
if(v.getId()==R.id.btn1){ // ボタン1が押下されたら
TaskDbInsert task1 = new TaskDbInsert(MainActivity.this);
task1.execute();
}
else if(v.getId()==R.id.btn2){ // ボタン2が押下されたら
TaskDbQuery task2 = new TaskDbQuery(MainActivity.this);
task2.execute();
}
}
}
class TaskDbInsert extends AsyncTask <Void,Void,Void >{
// この中をこれから記述
Activity activity = null;
// コンストラクタ
public TaskDbInsert(Activity act){
activity = act;
}
@Override
protected Void doInBackground(Void... voids) {
// 入力部の定義
EditText ed =(EditText)activity.findViewById(R.id.inputTxt);
String inputTxt=ed.getText().toString();
// DB接続と書き込み
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/memo_db","root","自分で設定したPASSWORD");
Statement stmt=conn.createStatement();
String sql="insert into memo_db.memo_tbl (memo) values ('"+inputTxt+"')";
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}catch(Exception e){
}
return null;
}
protected void onPostExecute(){
Toast.makeText(activity,"登録を終了しました", Toast.LENGTH_LONG).show();
}
}
class TaskDbQuery extends AsyncTask<Void, Void, String> {
// この中をこれから記述
Activity activity = null;
// コンストラクター
public TaskDbQuery(Activity act) {
activity = act;
}
@Override
protected String doInBackground(Void... params) {
// 検索語の入力部分の定義
EditText ed = (EditText) activity.findViewById(R.id.searchTxt);
String qryText = ed.getText().toString();
// 検索結果用の文字列
String rsText = "";
// DB接続と検索実行
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/memo_db", "root", "自分で設定したPASSWORD");
Statement stmt = conn.createStatement();
String sql = "select memo from memo_tbl where memo like '%" + qryText + "%'";
ResultSet rs = stmt.executeQuery(sql);
int rsCnt = 0;
while (rs.next()) {
rsText += rs.getString(1) + "\n";
rsCnt++;
}
// 検索結果が多すぎるとき、少なすぎるときの処理
if (rsCnt == 0) {
rsText = "検索結果がありません。キーワードを変更してください。";
} else if (rsCnt > 5) {
rsText = "5個以上見つかりました。キーワードを変更してください。";
}
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
rsText = e.getMessage();
}
return rsText;
}
protected void onPostExecute(String result){
TextView tv = (TextView)activity.findViewById(R.id.result);
tv.setText(result);
}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ryo.test2.MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="メモ記入" />
<EditText
android:id="@+id/inputTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ems="10"
android:inputType="textMultiLine" />
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="記録" />
<TextView
android:id="@+id/output"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="表示" />
<EditText
android:id="@+id/searchTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ems="10"
android:inputType="textMultiLine" />
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="検索" />
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ここにメモが表示されます" />
</LinearLayout>
</ScrollView>
です。
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
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
0
少なくともmysqlサーバーに接続する部分の設定で、アドレス:ポート番号が"localhost:3306"になっていますので、これではmysqlサーバーに繋がらないと思います。
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/memo_db", "root", "自分で設定したPASSWORD");
localhostは自分自身、つまりAndroid実機本体を指します。
まずはmysqlサーバーが稼働しているマシンのアドレス:ポート番号を正しく指定しましょう。
また、
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/memo_db","root","自分で設定したPASSWORD");
・・・省略
}catch(Exception e){
}
となっていて、Exceptionがthrowされても何もせず、エラーが分からなくなっています。Logを出力するなどして、エラーの原因が分かり易くすることをおすすめします。(今のコードでExceptionが発生しているはずです)
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
0
ご回答ありがとうございます!
mysqlサーバーが稼働しているマシンのアドレス:ポート番号というのはどこで確認できますか?
ご教授おねがいします!
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 90.00%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2017/10/29 00:30 編集
mysqlサーバーが稼働しているマシンのアドレス:ポート番号というのはどこで確認できますか?
ご教授おねがいします!
ちなみにworkbenchを使用しております!
2017/10/29 03:29
10-29 03:27:29.946 12426-12426/? E/Zygote: accessInfo : 0
10-29 03:27:29.946 12426-12426/? W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[2], Con:u:r:zygote:s0 RAM:SEPF_SECMOBILE_7.0_0009, [-1 -1 -1 -1 0 1]
10-29 03:27:30.277 12426-12426/com.example.ryo.test2 W/System: ClassLoader referenced unknown path: /data/app/com.example.ryo.test2-2/lib/arm64
10-29 03:27:36.679 12426-12480/com.example.ryo.test2 E/ContentValues: doInBackground:
WARNINGの部分です
2017/10/29 03:46 編集
2017/10/29 03:48 編集
2017/10/29 04:23
初心者で上記の部分がわかりません泣
IPアドレスを設定しました!
message from server: "Host'IPアドレスが下一桁が2ずれている'is not allowed to connect to this MySQL server"
という風にメッセージがでてきました!
2017/10/29 04:26
Log.e(TAG, "doInBackground:", e);
を追加し、記録用ボタンを押したときに現れた、WARNINGの部分です!
2017/10/29 04:30
2017/10/29 04:42
としました!
http://qa.nifty.com/cs/catalog/faq_nqa/qid_15334/1.htm
上記のサイトを見ながら、IPアドレスをしてしました!
2017/10/29 04:49
2017/10/29 14:50
android実機のIPアドレスを指定するのですか?
2017/10/29 14:55
2017/10/29 14:57
無知すぎてすみません。。。
2017/10/29 15:05
2017/10/29 15:20
無料WIFIで動作確認を試みましたが
doInBackground:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at java.lang.reflect.Constructor.newInstance0(Native Method)
というエラーが表示されます。。。。泣
2017/10/29 15:27 編集
2017/10/29 15:42
はい!確認済みです!
2017/10/29 17:29
原因は外での無料WIFIが原因でした!
自宅でIPアドレスを指定すると問題なくうごきました!
2017/10/29 17:32 編集