導航:首頁 > 配伺服器 > 安卓訪問mysql伺服器地址

安卓訪問mysql伺服器地址

發布時間:2022-08-29 19:33:28

android怎麼連接本地的mysql資料庫

android獲取數據不是要從伺服器上面去獲取嗎?而伺服器的數據從哪裡?肯定是是從資料庫上去讀取咯。所以你用伺服器去mysql資料庫讀取出數據!

⑵ 請問Android怎樣連接遠程MySQL資料庫

Android客戶端直接連接遠程MySQL資料庫的方法如下:

String result = "";
//首先使用NameValuePair封裝將要查詢的年數和關鍵字綁定
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));

//使用HttpPost封裝整個SQL語句
//使用HttpClient發送HttpPost對象
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/getAllPeopleBornAfter.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//將HttpEntity轉化為String
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();

result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}

//將String通過JSONArray解析成最終結果
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("id")+
", name: "+json_data.getString("name")+
", sex: "+json_data.getInt("sex")+
", birthyear: "+json_data.getInt("birthyear")
);
}
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}

雖然Android開發中可以直接連接資料庫,但是實際中卻不建議這么做,應該使用伺服器端中轉下完成。

⑶ 安卓app 怎麼連接mysql

android 鏈接mysql資料庫實例:
package com.hl;
import java.sql.DriverManager;
import java.sql.ResultSet;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class AndroidMsql extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new 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(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
}
try {
String url ="jdbc:mysql://192.168.142.128:3306/mysql?user=zzfeihua&password=12345&useUnicode=true&characterEncoding=UTF-8";//鏈接資料庫語句
Connection conn= (Connection) DriverManager.getConnection(url); //鏈接資料庫
Statement stmt=(Statement) conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from user";//查詢user表語句
ResultSet rs=stmt.executeQuery(sql);//執行查詢
StringBuilder str=new StringBuilder();
while(rs.next()){
str.append(rs.getString(1)+"\n");
}
mSetText(str.toString());

rs.close();
stmt.close();
conn.close();

} catch (Exception e) {
e.printStackTrace();
}
}
}
不過eclipse老是提示:
warning: Ignoring InnerClasses attribute for an anonymous inner class that doesn't come with an associated EnclosingMethod attribute. (This class was probably proced by a broken compiler.)

⑷ android手機軟體開發中 怎麼連接Mysql資料庫

一、首先要載入JDBC驅動包。

步驟:右擊項目找到build path->configure build path->libraries——>add External JARs添加驅動包

二、寫測試類:TestCon.java

(在此之前,首先

1.在自己的電腦上Mysql下確定賬戶是"root",密碼是"123456";

2.進入賬戶,創建資料庫cui;

3.在資料庫cui下面,創建表test1 包含_id(int 類型自動增加) username(String 類型)、password(String 類型);

4.在表中插入數據,以便顯示



1 package com.test.an;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.PreparedStatement;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8
9
10 public class TestCon1{
11 public static void main(String[] args)
12 {
13 Connection con = null;
14 String sql;
15 PreparedStatement pre;
16 ResultSet rs;
17
18 try {
19 String driver="com.mysql.jdbc.Driver";
20 Class.forName(driver);
21
22 String url="jdbc:mysql://localhost:3306/cuiuseUnicode=true&characterEncoding=latin1";//utf-8也行
23 con = DriverManager.getConnection(url, "root", "123456");
24
25 sql = "select _id,username,password from test1" ;
26 pre = con.prepareStatement(sql);
27
28 rs = pre.executeQuery();
29 while(rs.next()){
30 int id = rs.getInt(1);
31 String username = rs.getString(2);
32 String password = rs.getString(3);
33
34 System.out.println("id="+id+";username="+username+";password="+password);
35 }
36 con.close();
37 } catch (SQLException e) {
38 e.printStackTrace();
39 } catch (ClassNotFoundException e) {
40 e.printStackTrace();
41 }
42
43 }
44
45 }
運行結果:

id=1;username=ccc;password=123456
id=2;username=xxx;password=654321
id=3;username=ddd;password=123456
id=4;username=ddf÷;password=yyt
id=5;username=cuixiaodong;password=cxd
id=6;username=vv;password=cxd

⑸ android怎麼鏈接資料庫mysql

有點多請耐心看完。
希望能幫助你,還請及時採納謝謝。
一.前言

android連接資料庫的方式有兩種,第一種是通過連接伺服器,再由伺服器讀取資料庫來實現數據的增刪改查,這也是我們常用的方式。第二種方式是android直接連接資料庫,這種方式非常耗手機內存,而且容易被反編譯造成安全隱患,所以在實際項目中不推薦使用。

二.准備工作

1.載入外部jar包

在Android工程中要使用jdbc的話,要導入jdbc的外部jar包,因為在Java的jdk中並沒有jdbc的api,我使用的jar包是mysql-connector-java-5.1.18-bin.jar包,網路上有使用mysql-connector-java-5.1.18-bin.jar包的,自己去用的時候發現不兼容,所以下載了比較新版本的,jar包可以去官網下載,也可以去網路,有很多前人們上傳的。

2.導入jar包的方式

方式一:

可以在項目的build.gradle文件中直接添加如下語句導入

compile files('libs/mysql-connector-java-5.1.18-bin.jar')
方式二:下載jar包復制到項目的libs目錄下,然後右鍵復制過來的jar包Add as libs

三.建立資料庫連接

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jdbc);
new Thread(runnable).start();
}

Handler myHandler=new Handler(){

public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Bundle data=new Bundle();
data=msg.getData();

//System.out.println("id:"+data.get("id").toString()); //輸出第n行,列名為「id」的值
Log.e("TAG","id:"+data.get("id").toString());
TextView tv= (TextView) findViewById(R.id.jdbc);

//System.out.println("content:"+data.get("content").toString());
}
};

Runnable runnable=new Runnable() {
private Connection con = null;

@Override
public void run() {
// TODO Auto-generated method stub
try {
Class.forName("com.mysql.jdbc.Driver");
//引用代碼此處需要修改,address為數據IP,Port為埠號,DBName為數據名稱,UserName為資料庫登錄賬戶,Password為資料庫登錄密碼
con =
//DriverManager.getConnection("jdbc:mysql://192.168.1.202:3306/b2b", "root", "");
DriverManager.getConnection("jdbc:mysql://http://192.168.1.100/phpmyadmin/index.php:8086/b2b",
UserName,Password);

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
testConnection(con); //測試資料庫連接
} catch (java.sql.SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void testConnection(Connection con1) throws java.sql.SQLException {
try {
String sql = "select * from ecs_users"; //查詢表名為「oner_alarm」的所有內容
Statement stmt = con1.createStatement(); //創建Statement
ResultSet rs = stmt.executeQuery(sql); //ResultSet類似Cursor

//<code>ResultSet</code>最初指向第一行
Bundle bundle=new Bundle();
while (rs.next()) {
bundle.clear();
bundle.putString("id",rs.getString("userid"));
//bundle.putString("content",rs.getString("content"));
Message msg=new Message();
msg.setData(bundle);
myHandler.sendMessage(msg);
}

rs.close();
stmt.close();
} catch (SQLException e) {

} finally {
if (con1 != null)
try {
con1.close();
} catch (SQLException e) {}
}
}
};

注意:

在Android4.0之後,不允許在主線程中進行比較耗時的操作(連接資料庫就屬於比較耗時的操作),需要開一個新的線程來處理這種耗時的操作,沒新線程時,一直就是程序直接退出,開了一個新線程處理直接,就沒問題了。

當然,連接資料庫是需要網路的,千萬別忘了添加訪問網路許可權:

<uses-permission android:name=」android.permission.INTERNET」/>

四.bug點

1.導入的jar包一定要正確

2.連接資料庫一定要開啟新線程

3.資料庫的IP一定要是可以ping通的,區域網地址手機是訪問不了的

4.資料庫所在的伺服器是否開了防火牆,阻止了訪問
————————————————
版權聲明:本文為CSDN博主「shuaiyou_comon」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/shuaiyou_comon/article/details/75647355

⑹ android連接mysql資料庫必須要在同一網路下

不在同一個wifi也行,但是前提是你的mysql一定要有公網的IP地址,這樣才能被其它設備或者服務訪問,校園網當然是不行的。

安卓怎麼連接mysql資料庫數據

現在不能直接連接,不過你可以再寫一個web伺服器端,通過java網路編程遠程訪問,web伺服器端返回xml數據再解析。這樣即使換了別的非android客戶端,也容易移植。

⑻ Android 開發。。。如何連接到伺服器上的mysql資料庫

1、打開Tableau軟體。

⑼ 安卓開發如何連接MySQL Server

去看看httpget和httppost,再看一下servlet就可以實現一個簡單的連接了,連接寫在servlet裡面就好

閱讀全文

與安卓訪問mysql伺服器地址相關的資料

熱點內容
二級壓縮空壓機哪家好排行 瀏覽:741
基於單片機超聲波測距 瀏覽:127
模擬隨機紅包演算法 瀏覽:382
程序員接外包網站 瀏覽:133
哪裡可以買拆車件app 瀏覽:992
限流演算法漏桶和令牌桶區別 瀏覽:316
程序員怎麼找兼職 瀏覽:800
java編譯路徑 瀏覽:283
伺服器修改mac地址是什麼 瀏覽:581
商城訂單介面api加密 瀏覽:29
電話鍵盤加密應用 瀏覽:491
廣聯達安裝教程未檢測到加密鎖 瀏覽:343
ubuntu1404命令 瀏覽:55
rd會話主機伺服器是什麼 瀏覽:147
模擬集成電路設計pdf 瀏覽:620
數控編程培訓行業 瀏覽:43
怎麼樣自製變形折紙解壓玩具 瀏覽:933
ethtoolandroid 瀏覽:420
文件夾如何分類存檔 瀏覽:727
程序員和地產公司哪個賺的多 瀏覽:559