我尝试了很多次.....我无法连接到我本地主机的mysql数据库....当我在emulator-android中运行这个编码
这是我的java编码
package com.example.pravien.madattendence;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Register1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
try {
TextView e=(TextView)findViewById(R.id.textView6);
e.setText("waiting");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://10.0.2.2:8080/MadAttendence/", "root", "");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select name from colinfo where code='3106'");
rs.next();
String s = "" + rs.getString(1);
e.setText(s);
}
catch(Exception e){e.printStackTrace();};
}
}
这是我的xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.pravien.madattendence.MainActivity2"
android:background="#ff89ffd7"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="REGISTER"
android:id="@+id/textView3"
android:textSize="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID Number: "
android:textSize="30dp"
android:id="@+id/textView4"
android:layout_marginTop="86dp"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="400dip"
android:layout_height="40dip"
android:id="@+id/editText3"
android:layout_below="@+id/textView4"
android:layout_alignStart="@+id/textView3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="collage code"
android:textSize="30dp"
android:id="@+id/textView5"
android:layout_centerHorizontal="true"
android:layout_below="@+id/editText3" />
<EditText
android:layout_width="400dip"
android:layout_height="40dip"
android:id="@+id/editText4"
android:text="3106"
android:layout_below="@+id/textView5"
android:layout_alignStart="@+id/editText3" />
<TextView
android:layout_width="400dip"
android:layout_height="40dip"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView6"
android:layout_below="@+id/editText4"
android:layout_marginTop="77dp"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next-->"
android:id="@+id/button3"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignEnd="@+id/editText4"
android:background="#fffaff55" />
</RelativeLayout>
这是我的机器人清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pravien.madattendence" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".LoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Register1"
android:label="@string/title_activity_main_activity2" >
</activity>
</application>
</manifest>
这是我在日志中的错误显示
02-22 19:18:48.140 1135-1135/com.example.pravien.madattendence W/System.err﹕ com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
02-22 19:18:48.140 1135-1135/com.example.pravien.madattendence W/System.err﹕ [ 02-22 19:18:48.140 1135: 1135 W/System.err ]
Last packet sent to the server was 1 ms ago.
02-22 19:18:48.150 1135-1135/com.example.pravien.madattendence W/System.err﹕ at java.lang.reflect.Constructor.newInstance(Native Method)
02-22 19:18:48.150 1135-1135/com.example.pravien.madattendence W/System.err﹕ at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
02-22 19:18:48.158 1135-1135/com.example.pravien.madattendence W/System.err﹕ at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
02-22 19:18:48.159 1135-1135/com.example.pravien.madattendence W/System.err﹕ at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
02-22 19:18:48.159 1135-1135/com.example.pravien.madattendence W/System.err﹕ at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2104)
02-22 19:18:48.159 1135-1135/com.example.pravien.madattendence W/System.err﹕ at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:729)
02-22 19:18:48.159 1135-1135/com.example.pravien.madattendence W/System.err﹕ at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
02-22 19:18:48.159 1135-1135/com.example.pravien.madattendence W/System.err﹕ at java.lang.reflect.Constructor.newInstance(Native Method)
02-22 19:18:48.159 1135-1135/com.example.pravien.madattendence W/System.err﹕ at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
02-22 19:18:48.159 1135-1135/com.example.pravien.madattendence W/System.err﹕ at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
02-22 19:18:48.160 1135-1135/com.example.pravien.madattendence W/System.err﹕ at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
02-22 19:18:48.160 1135-1135/com.example.pravien.madattendence W/System.err﹕ at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283)
02-22 19:18:48.160 1135-1135/com.example.pravien.madattendence W/System.err﹕ at java.sql.DriverManager.getConnection(DriverManager.java:179)
02-22 19:18:48.160 1135-1135/com.example.pravien.madattendence W/System.err﹕ at java.sql.DriverManager.getConnection(DriverManager.java:213)
02-22 19:18:48.160 1135-1135/com.example.pravien.madattendence W/System.err﹕ at com.example.pravien.madattendence.Register1.onCreate(Register1.java:26)
02-22 19:18:48.175 1135-1135/com.example.pravien.madattendence W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5933)
02-22 19:18:48.176 1135-1135/com.example.pravien.madattendence W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-22 19:18:48.176 1135-1135/com.example.pravien.madattendence W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
02-22 19:18:48.176 1135-1135/com.example.pravien.madattendence W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-22 19:18:48.176 1135-1135/com.example.pravien.madattendence W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-22 19:18:48.176 1135-1135/com.example.pravien.madattendence W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-22 19:18:48.176 1135-1135/com.example.pravien.madattendence W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
02-22 19:18:48.177 1135-1135/com.example.pravien.madattendence W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
02-22 19:18:48.177 1135-1135/com.example.pravien.madattendence W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5221)
02-22 19:18:48.177 1135-1135/com.example.pravien.madattendence W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
02-22 19:18:48.177 1135-1135/com.example.pravien.madattendence W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
02-22 19:18:48.177 1135-1135/com.example.pravien.madattendence W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-22 19:18:48.192 1135-1135/com.example.pravien.madattendence W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-22 19:18:48.196 1135-1135/com.example.pravien.madattendence W/System.err﹕ Caused by: java.net.SocketException: android.os.NetworkOnMainThreadException
02-22 19:18:48.197 1135-1135/com.example.pravien.madattendence W/System.err﹕ at com.mysql.jdbc.StandardSocketFactory.unwrapExceptionToProperClassAndThrowIt(StandardSocketFactory.java:407)
02-22 19:18:48.198 1135-1135/com.example.pravien.madattendence W/System.err﹕ at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:268)
02-22 19:18:48.198 1135-1135/com.example.pravien.madattendence W/System.err﹕ at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
02-22 19:18:48.198 1135-1135/com.example.pravien.madattendence W/System.err﹕ at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2027)
02-22 19:18:48.198 1135-1135/com.example.pravien.madattendence W/System.err﹕ ... 23 more
02-22 19:18:48.249 1135-1135/com.example.pravien.madattendence I/Choreographer﹕ Skipped 120 frames! The application may be doing too much work on its main thread.
02-22 19:18:48.662 1135-1155/com.example.pravien.madattendence W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-22 19:18:48.662 1135-1155/com.example.pravien.madattendence W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6911a00, error=EGL_SUCCESS
请帮助我.......
答案 0 :(得分:0)
你的android模拟器必须在同一个I.P.作为你的主人。尝试首先从模拟器访问10.0.2.2(通过ping或浏览器),或者更好,第二步:检查MySQL服务器权限并授予其他主机(即模拟器的I.P.)的访问权限。