當前位置:ag真人国际官网-ag旗舰厅官方网站 » 安卓系統 » 安卓如何連接資料庫

安卓如何連接資料庫-ag真人国际官网

發布時間: 2022-01-08 06:25:48

『壹』 安卓程序怎麼連接到個人電腦資料庫

jsp,我最近也正在研究,如何將數據導入到計算機資料庫中的android軟體,用電腦上網和sql,我已經建立了一個jsp環境

『貳』 android手機軟體開發中 怎麼連接mysql資料庫

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

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

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

『肆』 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();

『伍』 安卓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客戶端怎麼與伺服器資料庫連接

不能與資料庫連接的

android客戶端不能直接與伺服器資料庫連接。資料庫是需要非常大的內存,安裝之後有好幾g,連接資料庫要有一個像sqlserver里的webservice,這樣的一個橋梁來間接訪問。就是在伺服器運行一個服務端程序,該服務端程序通過接收來自android客戶端的指令,對資料庫進行操作。

客戶端的http請求可以通過 httpclient類實現,在anddroid 4.0之後,客戶端的網路請求已經不被允許在主線程中運行,所以還需注意另開啟一個子線程進行網路請求。

(6)安卓如何連接資料庫擴展閱讀:

android安全許可權機制:

android默認設置下,所有應用都沒有許可權對其他應用、系統或用戶進行較大影響的操作。這其中包括讀寫用戶隱私數據(聯系人或電子郵件),讀寫其他應用文件,訪問網路或阻止設備待機等。安裝應用時,在檢查程序簽名提及的許可權,且經過用戶確認後,軟體包安裝器會給予應用許可權。

下載一款android應用通常會要求如下的許可權:撥打電話、發送簡訊或彩信、修改/刪除sd卡上的內容、讀取聯系人的信息、讀取日程信的息,寫入日程數據、讀取電話狀態或識別碼、精確的(基於gps)地理位置、模糊的(基於網路獲取)地理位置、創建藍牙連接、

還有對互聯網的完全訪問、查看網路狀態,查看wifi狀態、避免手機待機、修改系統全局設置、讀取同步設定、開機自啟動、重啟其他應用、終止運行中的應用、設定偏好應用、震動控制、拍攝圖片等。

『柒』 請問android怎樣連接遠程mysql資料庫

沒直接連接過資料庫。我是連接的servlet然後給它查詢語句,servlet查詢後以xml形式返回數據。
安卓用httpclient連接servlet,httpclient使用詳情網路有。

『捌』 android開發資料庫怎麼連接

這種方式通常連接一個外部的資料庫,第一個參數就是資料庫文件,這個資料庫不是當前項目中生成的,通常放在項目的assets目錄下,當然也可以在手機內,如上面參數那個目錄,前提是那個文件存在且你的程序有訪問許可權。

另一種使用資料庫的方式是,自己創建資料庫並創建相應的資料庫表,參考下面的代碼:

{

//構造,調用父類構造,資料庫名字,版本號(傳入更大的版本號可以讓資料庫升級,onupgrade被調用)
publicdatabasehelper(contextcontext){
super(context,databaseconstant.database_name,null,databaseconstant.database_version);
}

//資料庫創建時調用,裡面執行表創建語句.
@override
publicvoidoncreate(sqlitedatabasedb){
db.execsql(createvouchertable());
}

//資料庫升級時調用,先刪除舊表,在調用oncreate創建表.
@override
publicvoidonupgrade(sqlitedatabasedb,intoldversion,intnewversion){
db.execsql("droptableifexists" databaseconstant.table_name);
oncreate(db);
}
//生成創建表的sql語句
(){
stringbuffersb=newstringbuffer();
sb.append("createtable").append(databaseconstant.table_name).append("(").append(「id」)
.append("textprimarykey,")
.append(「user_id」).append("integer,").append(「sms_content」).append("text)");
returnsb.tostring();
}
}

繼承sqliteopenhelper並實現裡面的方法.

之後:

//得到資料庫助手類

helper = new databasehelper(context);

//通過助手類,打開一個可讀寫的資料庫連接

sqlitedatabase database = helper.getreadabledatabase();

//查詢表中所有記錄

database.query(databaseconstant.table_name, null, null, null, null, null, null);

『玖』 安卓開發連接資料庫

android 連接資料庫
android採用關系型資料庫sqlite3,它是一個支持sql輕量級的嵌入式資料庫,在嵌入式操作上有很廣泛的,wm採用的也是sqlite3
關於過於、原理方面的東西在這篇文章里不會提到,但是如果你想能夠快速的學會操作sqlite3,那這就是你要找的文章!
首先,我們看一下api,所有資料庫相關的介面、類都在。database和android.database.sqlite兩個包下,雖然只有兩個包,但是如果你英文不好或是太懶的話也要迷茫一段時間,其實,我們真正用的到的沒有幾個!
1、sqliteopenhelper (android.database.sqlite.sqliteopenhelper)
這是一個抽象類,關於抽象類我們都知道,如果要使用它,一定是繼承它!
這個類的方法很少,有一個構造方法
sqliteopenhelper(android.content.context context, java.lang.string name,android.database.sqlite.sqlitedatabase.cursorfactory factory, int version);
參數不做過多的解釋,cursorfactory一般直接傳null就可以
public void oncreate(sqlitedatabase db)
此方法在創建資料庫是被調用,所以,應該把創建表的操作放到這個方法裡面,一會兒在後面我們會再詳細的說如何創建表
public void onupgrade(sqlitedatabase db, int oldversion, int newversion)
從方法名上我們就能知道這個方法是執行更新的,沒錯,當version改變是系統會調用這個方法,所以在這個方法里應該執行刪除現有表,然後手動調用oncreate的操作
sqlitedatabase getreadabledatabase()
可讀的sqlitedatabase對象
sqlitedatabase getwritabledatabase()
獲取可寫的sqlitedatabase對象
2、sqlitedatabase(android.database.sqlite.sqlitedatabase)
關於操作資料庫的工作(增、刪、查、改)都在這個類里
execsql(sql)
執行sql語句,用這個方法 sql語句可以非常方便的執行增、刪、查、改
除此之外,android還提供了功過方法實現增、刪、查、改
long insert(table_name, null, contentvalues)添加記錄
int delete(table_name, where, wherevalue)刪除記錄
int update(table_name, contentvalues, where, wherevalue) 更新記錄
cursor query(table_name, null, null, null, null, null, null) 查詢記錄
除此之外,還有很多方法,如:begintransaction()開始事務、endtransaction()結束事務…有興趣的可以自己看api,這里就不多贅述了
3、cursor(android.database.cursor)
游標(介面),這個很熟悉了吧,cursor里的方法非常多,常用的有:
boolean movetoposition(position)將指針移動到某記錄
getcolumnindex(contacts.people.name)按列名獲取id
int getcount()獲取記錄總數
boolean requery()重新查詢
boolean isafterlast()指針是否在末尾
boolean isbeforefirst()時候是開始位置
boolean isfirst()是否是第一條記錄
boolean islast()是否是最後一條記錄
boolean movetofirst()、 boolean movetolast()、 boolean movetonext()同movetoposition(position)
4、simplecursoradapter(android.widget.simplecursoradapter)
也許你會奇怪了,之前我還說過關於資料庫的操作都在database和database.sqlite包下,為什麼把一個adapter放到這里,如果你用過android的sqlite3,你一定會知道
,這是因為我們對資料庫的操作會經常跟列表聯系起來
經常有朋友會在這出錯,但其實也很簡單
simplecursoradapter adapter = new simplecursoradapter(
this,
r.layout.list,
mycursor,
new string[] {db.text1,db. text2},
new int[]{ r.id.list1,r.id.listtext2 });
my.setadapter(adapter);
一共5個參數,具體如下:
參數1:content
參數2:布局
參數3:cursor游標對象
參數4:顯示的欄位,傳入string[]
參數5:顯示欄位使用的組件,傳入int[],該數組中是textview組件的id
到這里,關於資料庫的操作就結束了,但是到目前為止我只做了翻譯的工作,有些同學可能還是沒有掌握,放心,下面我們一起順著正常開發的思路理清一下頭緒!
前面的只是幫沒做過的朋友做下普及,下面才是你真正需要的!
一、寫一個類繼承sqliteopenhelpe
public class databasehelper extends sqliteopenhelper
構造方法:
databasehelper(context context) {
super(context, database_name, null, database_version);
}
在oncreate方法里寫建表的操作
public void oncreate(sqlitedatabase db) {
string sql = 「create table tb_test (_id integer default '1' not null primary key autoincrement,class_jb text not null,class_ysbj text not null,title text not null,content_ysbj text not null)」;
db.execsql(sql);//需要異常捕獲
}
在onupgrade方法里刪除現有表,然後手動調用onctreate創建表
public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {
string sql = 「drop table 」 tbname;
db.execsql(sql);
oncreate(db);
}
對表增、刪、查、改的方法,這里用的是sqliteopenhelper提供的方法,也可以用sql語句實現,都是一樣的
關於獲取可讀/可寫sqlitedatabase,我不說大家也應該會想到,只有查找才會用到可讀的sqlitedatabase
/**
* 添加數據
*/
public long insert(string tname, int tage, string ttel){
sqlitedatabase db= getwritabledatabase();//獲取可寫sqlitedatabase對象
//contentvalues類似map,存入的是鍵值對
contentvalues contentvalues = new contentvalues();
contentvalues.put(「tname」, tname);
contentvalues.put(「tage」, tage);
contentvalues.put(「ttel」, ttel);
return db.insert(tbname, null, contentvalues);
}
/**
* 刪除記錄
* @param _id
*/
public void delete(string _id){
sqlitedatabase db= getwritabledatabase();
db.delete(tbname,
「_id=?」,
new string[]{_id});
}
/**
* 更新記錄的,跟插入的很像
*/
public void update(string _id,string tname, int tage, string ttel){
sqlitedatabase db= getwritabledatabase();
contentvalues contentvalues = new contentvalues();
contentvalues.put(「tname」, tname);
contentvalues.put(「tage」, tage);
contentvalues.put(「ttel」, ttel);
db.update(tbname, contentvalues,
「_id=?」,
new string[]{_id});
}
/**
* 查詢所有數據
* @return cursor
*/
public cursor select(){
sqlitedatabase db = getreadabledatabase();
return db.query(
tbname,
new string[]{「_id」,「tname」,「tage」,「ttel」,「taddr」},
null,
null, null, null, 「_id desc」);
}
關於db.query方法的參數,有很多,為了防止大家弄亂,我簡單說一下
參數1:表名
參數2:返回數據包含的列信息,string數組里放的都是列名
參數3:相當於sql里的where,sql里where後寫的內容放到這就行了,例如:tage>?
參數4:如果你在參數3里寫了?(知道我為什麼寫tage>?了吧),那個這里就是代替?的值 接上例:new string[]{「30」}
參數5:分組,不解釋了,不想分組就傳null
參數6:having,想不起來的看看sql
參數7:orderby排序
到這里,你已經完成了最多的第一步!我們來看看都用到了那些類:
sqliteopenhelper我們繼承使用的
sqlitedatabase增刪查改都離不開它,即使你直接用sql語句,也要用到execsql(sql)
二、這里無非是對databasehelper類定義方法的調用,沒什麼可說的,不過我還是對查詢再嘮叨幾句吧
android查詢出來的結果一cursor形式返回
cursor = sqlitehelper.select();//是不是很簡單?
查詢出來的cursor一般會顯示在listview中,這就要用到剛才提到的simplecursoradapter
simplecursoradapter adapter = new simplecursoradapter(
this,
r.layout.list_row,
cursor,
new string[]{「tname」,「ttel」},
new int[]{r.id.textview01,r.id.textview02}
);
裡面帶有實例。自己好好學習吧!

『拾』 安卓連接後台資料庫的應用怎麼開發

android,ios這些移動端程序,其實都和web端的是同樣的道理,並不能直接連接後台的資料庫進行操作,只能通過伺服器寫的一些介面api對資料庫進行包裝,然後客戶端通過後台api再進行數據操作。

伺服器,顧名思義,就是放服務的地方,就是發布各種服務的主機,比如說網站呀之類的,如web伺服器。資料庫的話,簡單說也就是放數據的地方,管理數據的一個系統。
舉個例子哈,比如你在伺服器上發布了個網站(一般都是動態的啦),這網站是有數據吧,數據就是從資料庫讀出來,這資料庫可以和這些網站放在同一主機,也可以放在另外一台主機。
如果把網站放一個主機,把資料庫放一個主機,這樣的話,放網站的主機就叫web伺服器,放資料庫的主機就叫資料庫伺服器。

著作權歸作者所有。
商業轉載請聯系作者獲得授權,非商業轉載請註明出處。
作者:永遇樂
鏈接:http://www.hu.com/question/22621704/answer/23216749
來源:知乎

api是否盡可能滿足restful? 設計api的時候,同時考慮url盡可能簡潔和清晰,也對怎麼分割伺服器端的功能實現有幫助。

熱點內容
發布:2024-07-17 17:13:27 瀏覽:872
phpjava交互 發布:2024-07-17 16:58:57 瀏覽:356
resin下jsp不能正常編譯 發布:2024-07-17 16:34:44 瀏覽:229
sqlserver如何切換主備伺服器 發布:2024-07-17 16:23:02 瀏覽:299
mc18伺服器ip 發布:2024-07-17 16:23:02 瀏覽:379
仙境傳說手游腳本 發布:2024-07-17 16:09:24 瀏覽:691
matlab命令窗口和新建腳本 發布:2024-07-17 15:51:26 瀏覽:375
建ftp文件夾 發布:2024-07-17 15:51:26 瀏覽:955
魔獸撿物腳本 發布:2024-07-17 15:27:56 瀏覽:130
開發ip伺服器 發布:2024-07-17 15:24:42 瀏覽:388
网站地图