當前位置:首頁 » 安卓系統 » android資料庫mysql資料庫

android資料庫mysql資料庫

發布時間: 2022-08-07 18:23:04

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

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

⑵ android怎麼連接mysql資料庫

用Android程序去直連MySQL資料庫,覺得這樣做不好,出於安全等方面考慮。資料庫地址,用戶名密碼,查詢SQL什麼的都存在程序里,很容易被反編譯等方法看到。
建議把表示層和數據層邏輯分開,數據層對應網頁的表示層提供介面,同時在為Android手機端提供一個介面,簡介訪問資料庫,這介面可以2端都保持一致,比如XML+RPC或者json等等,Android端也有現成的東西能直接用,既安全又省事。

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();

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

1、打開Tableau軟體。

⑷ 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資料庫裡面的數據

一、首先要載入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資料庫

1.首先需要安裝MySQL Server 5.1和navicat for mysql。這個安裝是很簡單的,網上很多教程,和安裝一般軟體差不多。只有在安裝MySQL Server 5.1時,要注意選擇字元編碼為gb2312(中文)那個選項。

⑺ 在Android平台如何上訪問mysql資料庫

直接使用普通的 java 資料庫連接方式即可。

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

照你的說法應該是將手機作為客戶端,然後你的本機作為伺服器端,那你直接下個mysql的資料庫驅動,用jdbc連接不就行了,跟android本身沒太大關系。

⑼ android 如何連接mysql資料庫,並且往資料庫裡面插入數據

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

熱點內容
壓縮文件暴力破解 發布:2025-04-08 04:09:03 瀏覽:422
linux菜鳥 發布:2025-04-08 04:05:24 瀏覽:982
如何關掉私密保險箱的密碼oppo 發布:2025-04-08 03:56:50 瀏覽:515
導出和編譯數據 發布:2025-04-08 03:41:05 瀏覽:789
如何查到電視機的配置 發布:2025-04-08 03:40:55 瀏覽:115
Java模2 發布:2025-04-08 03:29:32 瀏覽:96
如何查看ip地址和伺服器所在地 發布:2025-04-08 03:20:28 瀏覽:453
安卓前置攝像頭跟眼鏡哪個高清 發布:2025-04-08 03:10:16 瀏覽:94
怎麼配置家庭音響 發布:2025-04-08 03:03:05 瀏覽:693
蝴蝶演算法圖 發布:2025-04-08 02:52:33 瀏覽:614