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++) { int id = c.getInt(c.getColumnIndex("id")); String musicName = c.getString(c.getColumnIndex("musicName")); System.out.println("mid----------------->" +id); System.out.println("mname----------------->" +musicName); MusicInfo musicInfo = new MusicInfo(); musicInfo.setMusicId(id); musicInfo.setMusicName(musicName); musicList.add(musicInfo); } } for (int i = 0; i < musicList.size(); i++) { MusicInfo mi = new MusicInfo(); mi = (MusicInfo)musicList.get(i); } } } class CreateDatabaseListener implements OnClickListener{ @Override public void onClick(View v) { Log.d("name", "---------------"); DBHelper dbHelper = new DBHelper(MainActivity.this, "", 1); dbHelper.getReadableDatabase(); } } class InsertDatabaseListener implements OnClickListener{ @Override public void onClick(View v) { Log.d("name", "---------------"); DBHelper dbHelper = new DBHelper(MainActivity.this, "", 1); dbHelper.getReadableDatabase(); ContentValues values = new ContentValues(); String s [] = {"千里之外","春暖花开","黑色幽默","威廉古堡","世界末日", "三年二班","以父之名", "想象之中", "断桥残雪", "清明雨上", "星月神话", "单身情歌", "千千阙歌", "同桌的你", "你的样子"}; // String s [] = {"aaaa","bbbb","cccc","dddd","eeee" // }; String string = ""; for(int i = 1; i<=15 ;i++){ values.put("id", i); byte[] val = new byte[s[i-1].length()]; try { val = s[i-1].getBytes("UTF-8"); string = new String(val,"UTF-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } values.put("musicName", string); dbHelper.insert(values); } // values.put("id", "千里之外"); // values.put("2", "春暖花开"); // // values.put("3", "黑色幽默"); // values.put("4", "威廉古堡"); // values.put("5", "世界末日"); // values.put("6", "三年二班"); // values.put("7", "以父之名"); // values.put("8", "想象之中"); // values.put("9", "断桥残雪"); // values.put("10", "清明雨上"); // values.put("11", "星月神话"); // values.put("12", "单身情歌"); // values.put("13", "千千阙歌"); // values.put("14", "同桌的你"); // values.put("15", "你的样子"); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
android虚拟机 连接本地pc上的Mysql
Android中模拟器如何访问本地mysql数据库