android 鏈接mysql數(shù)據(jù)庫(kù)實(shí)例:
創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供茶陵網(wǎng)站建設(shè)、茶陵做網(wǎng)站、茶陵網(wǎng)站設(shè)計(jì)、茶陵網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、茶陵企業(yè)網(wǎng)站模板建站服務(wù),十年茶陵做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
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ù)庫(kù)語(yǔ)句
Connection conn= (Connection) DriverManager.getConnection(url); //鏈接數(shù)據(jù)庫(kù)
Statement stmt=(Statement) conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from user";//查詢user表語(yǔ)句
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客戶端直接連接遠(yuǎn)程MySQL數(shù)據(jù)庫(kù)的方法如下:
String result = "";
//首先使用NameValuePair封裝將要查詢的年數(shù)和關(guān)鍵字綁定
ArrayListNameValuePair nameValuePairs = new ArrayListNameValuePair();
nameValuePairs.add(new BasicNameValuePair("year","1980"));
//使用HttpPost封裝整個(gè)SQL語(yǔ)句
//使用HttpClient發(fā)送HttpPost對(duì)象
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ù)庫(kù),但是實(shí)際中卻不建議這么做,應(yīng)該使用服務(wù)器端中轉(zhuǎn)下完成。
用Android程序去直連MySQL數(shù)據(jù)庫(kù),覺得這樣做不好,出于安全等方面考慮。數(shù)據(jù)庫(kù)地址,用戶名密碼,查詢SQL什么的都存在程序里,很容易被反編譯等方法看到。
建議把表示層和數(shù)據(jù)層邏輯分開,數(shù)據(jù)層對(duì)應(yīng)網(wǎng)頁(yè)的表示層提供接口,同時(shí)在為Android手機(jī)端提供一個(gè)接口,簡(jiǎn)介訪問數(shù)據(jù)庫(kù),這接口可以2端都保持一致,比如XML+RPC或者json等等,Android端也有現(xiàn)成的東西能直接用,既安全又省事。
android 鏈接mysql數(shù)據(jù)庫(kù)實(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ù)庫(kù)語(yǔ)句
Connection conn= (Connection) DriverManager.getConnection(url); //鏈接數(shù)據(jù)庫(kù)
Statement stmt=(Statement) conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from user";//查詢user表語(yǔ)句
ResultSet rs=stmt.executeQuery(sql);//執(zhí)行查詢
StringBuilder str=new StringBuilder();
while(rs.next()){
str.append(rs.getString(1)+"\n");
}
mSetText(str.toString());
rs.close();
1、打開Tableau軟件。
2、在連接中,找到紅框位置的MySQL,點(diǎn)擊開始連接Mysql。
3、在彈出的連接界面,輸入Mysql服務(wù)器地址、端口、用戶名、密碼。
4、輸入完成后,點(diǎn)擊紅框位置 確認(rèn) 進(jìn)行連接。
5、此時(shí)已經(jīng)連接到MySQL服務(wù)器上,為了測(cè)試 我們點(diǎn)擊紅框位置 選擇數(shù)據(jù)庫(kù)查看一下。
解決Navicat無(wú)法連接到MySQL的問題
解決Navicat無(wú)法連接到MySQL的問題
問題一:本地IP(xxx.xxx.xxx.xxx)沒有訪問遠(yuǎn)程數(shù)據(jù)庫(kù)的權(quán)限。于是下面開啟本地IP(xxx.xxx.xxx.xxx)對(duì)遠(yuǎn)程mysql數(shù)據(jù)庫(kù)的訪問權(quán)限。
這里開啟權(quán)限,要在遠(yuǎn)程數(shù)據(jù)庫(kù)服務(wù)器上開:
解決辦法:
1、首先在遠(yuǎn)程服務(wù)器上連接進(jìn)入mysql,在cmd中輸入mysql -u root -p,然后回車,輸入密碼后回車進(jìn)入mysql命令行。
2、輸入下面命令,授權(quán)所有的客戶端都可以訪問服務(wù)器的權(quán)限,下面的‘123456’為你數(shù)據(jù)庫(kù)的實(shí)際密碼,記得修改:
grant all privileges on *.* to root@'%' identified by '123456';
3、輸入下面命令,刷新權(quán)限
flush privileges;
4、進(jìn)入相應(yīng)目錄修改ssh_config和sshd_config文件
cd /etc/ssh
vi ssh_config
將 #PasswordAuthentication yes改為?PasswordAuthentication yes,我們?nèi)サ袅怂懊娴?
編輯sshd_config 做同樣的去掉‘#’操作
5、然后重新啟動(dòng)mysql服務(wù)
/etc/init.d/mysqld restart
這里如果加入了服務(wù),就直接可以用service。