当我使用本地数据库服务器时它正在连接。如果我使用在线数据库服务器我无法连接,虽然我已授予manifest.xml
访问互联网的权限。在dbconctfun(View v )
函数中,我使用线程连接到数据库。
con = DriverManager.getConnection("jdbc:mysql://localhost:9503/dyne", "toot", "tent") this is online Database connection
package com.example.loginandroid;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Looper;
public class MainActivity extends Activity{
String username,password; ResultSet rs =null;
boolean temcfag=false;
static boolean temqfag=true;
public static String tag="Lifecycle activity";
EditText user,pass;
AlertDialog.Builder dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog=new AlertDialog.Builder(this);
dialog.setNeutralButton("OK", null);
user=(EditText)findViewById(R.id.editText1);
pass=(EditText)findViewById(R.id.editText2);
}
Thread thrd1,thrd2,thrd3;
Connection con;
String result ="",queryexct;
public void dbconctfun(View v ) throws SQLException {
try {
temqfag=true;
Class.forName("com.mysql.jdbc.Driver");
// Log.v("test#######","multiple class.forname");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
thrd1 = new Thread(new Runnable() {
public void run() {
while (!Thread.interrupted()) {
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
}
if (con == null) {
try {
// con = DriverManager.getConnection("jdbc:mysql://111.111.11.11:6666/dyne", "root", "mysql");
con = DriverManager.getConnection("jdbc:mysql://localhost:9503/dyne", "toot", "tent");
// Log.v("test#######","multiple con");
} catch (SQLException e) {
e.printStackTrace();
con = null;
}
if ((thrd2 != null) && (!thrd2.isAlive()))
thrd2.start();
}
}
}
});
if ((thrd1 != null) && (!thrd1.isAlive())) thrd1.start();
thrd2 = new Thread(new Runnable() {
public void run() {
while (!Thread.interrupted()) {
if (con != null) {
if (temqfag) {
try {
// con = DriverManager.getConnection("jdbc:mysql://192.168.1.45:3306/deneme", "ali", "12345");
Statement st = con.createStatement();
// st.execute("SELECT user_name FROM super_admin s");
username=user.getText().toString().trim();
password=pass.getText().toString().trim();
queryexct="SELECT * FROM `user_registration` WHERE `email_id` = '"+username+"' AND `password` = '"+password+"'";
rs = st.executeQuery(queryexct);
Log.v("queryexct",queryexct);
// MainActivity.this.setContentView(R.layout.activity_main1);
temqfag=false;
if (rs.next()) {
Looper.prepare();
// Thread.interrupted();
Message msgObj = handler.obtainMessage();
msgObj.arg1 = 1;
handler.sendMessage(msgObj);
} else {
Looper.prepare();
Thread.interrupted();
Message msgObj = handler.obtainMessage();
msgObj.arg1 = 0;
handler.sendMessage(msgObj);
}
} catch (SQLException e) {
e.printStackTrace();
con = null;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}}
} else {
try {
Log.v("test#######","errorrrrrrrrrrr4");
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.arg1 == 1) {
setContentView(R.layout.activity_main1);
} else {
dialog.setMessage("Your username and password are not valid");
dialog.show();
}
}
};
});
}
}
//Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.motalsquarea"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.motalsquarea.MainActivity"
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="com.example.motalsquarea.LoginActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.motalsquarea.Index"
android:label="@string/title_activity_index" >
</activity>
</application>
</manifest>
//error message
** BEGIN NESTED EXCEPTION **
java.net.SocketException
MESSAGE: java.net.ConnectException: failed to connect to localhost/111.0.0.1 (port 9703): connect failed: ECONNREFUSED (Connection refused)
W/System.err(1060): STACKTRACE:
W/System.err(1060): java.net.SocketException: java.net.ConnectException: failed to connect to localhost/111.0.0.1(port 9703): connect failed: ECONNREFUSED (Connection refused)
W/System.err(1060): at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
W/System.err(1060): at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
W/System.err(1060): at com.mysql.jdbc.Connection.createNewIO(Connection.java:2666)
03-11 08:59:49.400: W/System.err(1060): at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
W/System.err(1060): at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
W/System.err(1060): at java.sql.DriverManager.getConnection(DriverManager.java:179)
W/System.err(1060): at java.sql.DriverManager.getConnection(DriverManager.java:213)
W/System.err(1060): at com.example.motalsquarea.MainActivity$2.run(MainActivity.java:108)
at java.lang.Thread.run(Thread.java:841)
** END NESTED EXCEPTION **
Last packet sent to the server was 750 ms ago.
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2741)
at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(DriverManager.java:179)
at java.sql.DriverManager.getConnection(DriverManager.java:213)
at com.example.desisquarea.MainActivity$2.run(MainActivity.java:108)
at java.lang.Thread.run(Thread.java:841)
答案 0 :(得分:0)
您永远不应该从Android应用程序直接连接到服务器数据库。在服务器上使用Web服务,它允许您连接到数据库。这样你只需要求服务器获取信息,服务器就是访问数据库并发回你请求的数据的服务器。
答案 1 :(得分:0)
<击> 我在你的代码中发现了两个问题:
Class.forName("com.mysql.jdbc.Driver");
据我所知,android不支持mysql.jdbc
。你有一个特殊的android-jdbc-mysql驱动程序吗?
击>
[更新]你的stacktrace显示你的android系统有一个mysql-jdbc驱动程序。 (对不起)/更新]
con = DriverManager.getConnection(
"jdbc:mysql://localhost:9503/dyne", "toot", "tent")
localhost
是Android设备。你真的在Android设备上运行mysql数据库吗?你可能需要一个可以通过互联网或wlan访问的服务器网址或IP地址。