日期:2014-05-16  浏览次数:20528 次

Android中模拟器如何访问本地mysql数据库
   package com.game.music;

import java.io.UnsupportedEncodingException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.game.GuessMusicName.R;
import com.game.music.entity.MusicInfo;
import com.game.music.entity.db.DBHelper;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class MainActivity extends Activity {
	private Button startButton;
	private Button createDatabaseButton;
	private Button insertButton;
	private Button queryButton;
	private List musicList = new ArrayList();
	private Button mysqlButton = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		startButton = (Button)findViewById(R.id.start);
		startButton.setOnClickListener(new StartButtonListener());
		createDatabaseButton = (Button)findViewById(R.id.create);
		insertButton = (Button)findViewById(R.id.insert);
		queryButton = (Button)findViewById(R.id.query); 
		
		createDatabaseButton.setOnClickListener(new CreateDatabaseListener());
		insertButton.setOnClickListener(new InsertDatabaseListener());
		queryButton.setOnClickListener(new QueryDatabaseListener());
		//测试连接mysql
		mysqlButton = (Button) findViewById(R.id.mysql);
		mysqlButton.setOnClickListener(new MysqlButtonListener());
		
	}
	
	class MysqlButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			 sqlCon(); 
		}
		
	}
	
	
//	private void mSetText(String str){
//    	 TextView txt=(TextView)findViewById(R.id.txt);
//    	 txt.setText(str);
//    }
    
    private void sqlCon(){
    	System.out.println("0000000000000");
    	try {
        	Class.forName("com.mysql.jdbc.Driver");	
		} catch (Exception e) {
			e.printStackTrace();
		}
//    	10.76.160.212 10.76.161.206
		try {
			System.out.println("1111111111111111111111111111111111");
//            String url ="jdbc:mysql://10.0.0.2:3306/music_info?username=root&password=123456&useUnicode=true&characterEncoding=UTF-8";//链接数据库语句
//            Connection conn= (Connection) DriverManager.getConnection(url); //链接数据库
            Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://10.0.2.2:3306/music_info","root","123456");
            Statement stmt=(Statement) conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            String sql="select * from music";//查询music表语句
            ResultSet rs=stmt.executeQuery(sql);//执行查询
            StringBuilder str=new StringBuilder();
            while(rs.next()){
            	str.append(rs.getString(2)+"\n");	
            }
//            mSetText(str.toString());
            System.out.println(str.toString());
        
            rs.close();    
            stmt.close();
            conn.close();
            
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	
	
	
	class StartButtonListener implements OnClickListener{
		
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(MainActivity.this, GetMusicItemActivity.class);
			Bundle bundle = new Bundle();
			bundle.putString("name", "春暖花开");
			intent.putExtras(bundle);
			startActivity(intent);
		}
		
	}
	
	class  QueryDatabaseListener implements OnClickListener{
		
		@Override
		public void onClick(View v) {
			Log.d("name", "---------------1111");
			DBHelper dbHelper = new DBHelper(MainActivity.this, "", 1);
			Cursor c = dbHelper.queryObject();
			dbHelper.getReadableDatabase();
			if(c.moveToNext()) {
				for (int i = 0; i < c.getCount(); i++) {
					in