android,app通过jdbc连接mysql

java.sql.SQLException: Access denied for user '192.168.1.101'@'root' (using password: YES),

以前试了一次,调试了很久,还是以失败而告终,最近因为项目需要又重新调试了一次,终于把他拿下,

GRANT ALL PRIVILEGES ON 数据库名.* TO root@'%'  IDENTIFIED BY '密码' WITH GRANT OPTION;

在mysql的cmd下运行这句话就行了


MainActivity.java

package com.example.db_test_1;

import com.dao.Manipulate;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Handler handler;
	Manipulate manipulate = new Manipulate();

	Button btn_insert, btn_select;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn_insert = (Button) findViewById(R.id.btn_insert);
		btn_select = (Button) findViewById(R.id.btn_select);

		btn_insert.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根

				// manipulate.insertData();
				// Toast.makeText(MainActivity.this, "插入完成", Toast.LENGTH_SHORT)
				// .show();
				// ;
				fun_insert();

			}
		});

		btn_select.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根

				// manipulate.selectData();
				// Toast.makeText(MainActivity.this, "查询完成", Toast.LENGTH_SHORT)
				// .show();
				fun_select();
			}
		});
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO 自动生成的方法存根
				// super.handleMessage(msg);
				switch (msg.what) {
				case 1: // //插入
					manipulate.insertData();
					Toast.makeText(MainActivity.this, "插入完成",
							Toast.LENGTH_SHORT).show();
					;
					break;

				case 2: // /查询
					manipulate.selectData();
					Toast.makeText(MainActivity.this, "查询完成",
							Toast.LENGTH_SHORT).show();
					;
					break;
				}

			}

		};

	}// //onCreate

	public void fun_insert() {

		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO 自动生成的方法存根
				manipulate.insertData();
				Message m = new Message();
				m.what = 1;
				// handler.sendMessage(m);
			}
		}).start();
	}// //fun_insert

	public void fun_select() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO 自动生成的方法存根
				manipulate.selectData();
				Message m = new Message();
				m.what = 2;
				// handler.sendMessage(m);
			}
		}).start();
		

	}

}
Manipulate.java

package com.dao;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.util.Log;

public class Manipulate {
	static Connection con; // 声明Connection对象
	static Statement sql; // 声明Statement对象
	static ResultSet res;
	public static StringBuilder sb = new StringBuilder();
	SimpleDateFormat sdf = new SimpleDateFormat();
	String date_str = sdf.format(new Date());

	public Connection getConnection() { // 连接数据库方法
		try {
			Class.forName("org.gjt.mm.mysql.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://192.168.1.102:3306/db_11", "root", "0000");
			// con = DriverManager.getConnection(
			// "jdbc:mysql://192.168.0.154:3306/test", "root", "YES"); /////报错

		} catch (Exception e) {
			e.printStackTrace();
		}
		return con; // 返回Connection对象
	}

	// ///////////////

	public static void insertData() {
		Manipulate c = new Manipulate();
		con = c.getConnection();

		try {
			sql = con.createStatement();

			sql.executeUpdate("  insert  into  stu  values(1,'Shara' ) ;");
			Log.i("插入信息", "(1,'Shara' )");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}// /insertdata
		// ////////////////////

	public static void selectData() {

		Manipulate c = new Manipulate();
		con = c.getConnection();

		try {
			sql = con.createStatement();
			res = sql.executeQuery("select  *  from  stu;");
			while (res.next()) {
				// System.out.println(res.getString("id"));
				// System.out.println(res.getString("name"));
				// System.out.println("-------------------\n"); /////均正确

				sb.append(res.getString("id") + "   " + res.getString("name")
						+ "\n"); // /////////均正确
				Log.i("查询信息", sb.toString());
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}// /Test

    <uses-permission android:name="android.permission.还需要添加权限和mysql的jdbc(可自行下载)。前提是手机和电脑连接的都是同一个wifi

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_insert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="插入" />

    <Button
        android:id="@+id/btn_select"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询" />

</LinearLayout>

android,app通过jdbc连接mysql_第1张图片 android,app通过jdbc连接mysql_第2张图片



你可能感兴趣的:(android,数据库,mysql)