導航:首頁 > 操作系統 > android操作mysql資料庫

android操作mysql資料庫

發布時間:2022-08-26 19:22:34

『壹』 請問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開發中可以直接連接資料庫,但是實際中卻不建議這么做,應該使用伺服器端中轉下完成。

『貳』 android 操作mysql資料庫有哪些方法 (服務端 客戶端)

android有自帶的小型資料庫SQLite,在那裡面可以完成和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資料庫

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

android不能直接連接mysql,可以通過WebService相連

『玖』 怎樣使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自動連接指定wifi 瀏覽:491
用紙做超簡單又解壓的東西 瀏覽:596
國密2演算法是對稱的嗎 瀏覽:465
nc65伺服器地址配置 瀏覽:522
單片機實驗報告電子琴 瀏覽:744
程序員恢復微信文件代碼 瀏覽:517
有漁python 瀏覽:81
pdf字體加深 瀏覽:206
怎麼做一個minecraft伺服器 瀏覽:771
c語言實現ls命令 瀏覽:663
小洋糕解壓視頻 瀏覽:450
域名內網訪問內網伺服器地址 瀏覽:138
我的世界伺服器如何摳掉金幣 瀏覽:223
域名與ip地址通過什麼伺服器查 瀏覽:96
企業網站需要什麼雲伺服器配置 瀏覽:910
遼事通伺服器出現錯誤是什麼原因 瀏覽:766
能否將一個表格的子表加密 瀏覽:64
手機ios微信收藏怎麼加密 瀏覽:594
安卓如何改黑色 瀏覽:331
oracle資料庫導出命令 瀏覽:697