1、打開Tableau軟件。
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序設(shè)計(jì)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了前郭免費(fèi)建站歡迎大家使用!
2、在連接中,找到紅框位置的MySQL,點(diǎn)擊開始連接Mysql。
3、在彈出的連接界面,輸入Mysql服務(wù)器地址、端口、用戶名、密碼。
4、輸入完成后,點(diǎn)擊紅框位置 確認(rèn) 進(jìn)行連接。
5、此時(shí)已經(jīng)連接到MySQL服務(wù)器上,為了測試 我們點(diǎn)擊紅框位置 選擇數(shù)據(jù)庫查看一下。
Android客戶端直接連接遠(yuǎn)程MySQL數(shù)據(jù)庫的方法如下:
String result = "";
//首先使用NameValuePair封裝將要查詢的年數(shù)和關(guān)鍵字綁定
ArrayListNameValuePair nameValuePairs = new ArrayListNameValuePair();
nameValuePairs.add(new BasicNameValuePair("year","1980"));
//使用HttpPost封裝整個(gè)SQL語句
//使用HttpClient發(fā)送HttpPost對象
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("");
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轉(zhuǎn)化為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解析成最終結(jié)果
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;ijArray.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開發(fā)中可以直接連接數(shù)據(jù)庫,但是實(shí)際中卻不建議這么做,應(yīng)該使用服務(wù)器端中轉(zhuǎn)下完成。
1.首先需要安裝MySQL Server 5.1和navicat for mysql。這個(gè)安裝是很簡單的,網(wǎng)上很多教程,和安裝一般軟件差不多。只有在安裝MySQL Server 5.1時(shí),要注意選擇字符編碼為gb2312(中文)那個(gè)選項(xiàng)。
android 鏈接mysql數(shù)據(jù)庫實(shí)例:
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=zzfeihuapassword=12345useUnicode=truecharacterEncoding=UTF-8";//鏈接數(shù)據(jù)庫語句
Connection conn= (Connection) DriverManager.getConnection(url); //鏈接數(shù)據(jù)庫
Statement stmt=(Statement) conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from user";//查詢user表語句
ResultSet rs=stmt.executeQuery(sql);//執(zhí)行查詢
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 produced by a broken compiler.)
用Android程序去直連MySQL數(shù)據(jù)庫,覺得這樣做不好,出于安全等方面考慮。數(shù)據(jù)庫地址,用戶名密碼,查詢SQL什么的都存在程序里,很容易被反編譯等方法看到。
建議把表示層和數(shù)據(jù)層邏輯分開,數(shù)據(jù)層對應(yīng)網(wǎng)頁的表示層提供接口,同時(shí)在為Android手機(jī)端提供一個(gè)接口,簡介訪問數(shù)據(jù)庫,這接口可以2端都保持一致,比如XML+RPC或者json等等,Android端也有現(xiàn)成的東西能直接用,既安全又省事。
android 鏈接mysql數(shù)據(jù)庫實(shí)例:
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=zzfeihuapassword=12345useUnicode=truecharacterEncoding=UTF-8";//鏈接數(shù)據(jù)庫語句
Connection conn= (Connection) DriverManager.getConnection(url); //鏈接數(shù)據(jù)庫
Statement stmt=(Statement) conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from user";//查詢user表語句
ResultSet rs=stmt.executeQuery(sql);//執(zhí)行查詢
StringBuilder str=new StringBuilder();
while(rs.next()){
str.append(rs.getString(1)+"\n");
}
mSetText(str.toString());
rs.close();
網(wǎng)站名稱:android連接mysql,android連接mysql用的驅(qū)動包
文章鏈接:http://chinadenli.net/article18/dsdejgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、軟件開發(fā)、微信小程序、網(wǎng)站維護(hù)、搜索引擎優(yōu)化、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)