我在我的电脑上安装了mysql。还有码头.. 我的要求: - 我有一个数据库学生,表名为ari ; ari表是: -
+----+-------+
| id | name |
+----+-------+
| 22 | Sandy |
+----+-------+
来自我的Android程序我想从数据库中获取它..我已经这样做了: - 的 MainActivity.java
public class MainActivity extends Activity {
private static final String url = "jdbc:mysql://localhost:3306/student";
private static final String user = "root";
private static final String pass = "root";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testDB();
}
public void testDB() {
TextView tv = (TextView)this.findViewById(R.id.textView1);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, pass);
String result = "Database connection success\n";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from ari");
ResultSetMetaData rsmd = rs.getMetaData();
while(rs.next()) {
result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
}
tv.setText(result);
}
catch(Exception e) {
e.printStackTrace();
tv.setText(e.toString());
}
}
}
和 activity_main.xml 是: -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
和manifestxt文件是:---
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.TourBus.info"
android:versionCode="1"
android:versionName="1.0-SNAPSHOT" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<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=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
但问题是当我在我的Android手机或模拟器中运行该程序时,它显示: -
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
Could not create connection to database server.
我也有jar文件: -
mysql-connector-java
任何人都可以帮助我吗??请
答案 0 :(得分:0)
如果您希望模拟器客户端与运行在同一主机上的服务器联系,请使用别名10.0.2.2
来引用主机计算机的环回接口。从模拟器的角度来看,localhost
或127.0.0.1
指的是它自己的环回接口。所以改变
private static final String url = "jdbc:mysql://localhost:3306/student";
到
private static final String url = "jdbc:mysql://10.0.2.2/student";
选中此link
在/etc/mysql/my.cnf
尝试通过在该行的开头添加skip-networking
来评论#
。
如果这不能解决您的问题,请尝试检查此Solving a “communications link failure” with JDBC and MySQL,以便在更改com.mysql.jdbc.exceptions.jdbc4.CommunicationException:communication link failure
后解决url
错误,如上所示。