android 鏈接mysql數(shù)據(jù)庫(kù)實(shí)例:
創(chuàng)新互聯(lián)建站主要從事成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)甕安,十多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):18982081108
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();
}
}
}
不過(guò)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ù)庫(kù),覺(jué)得這樣做不好,出于安全等方面考慮。數(shù)據(jù)庫(kù)地址,用戶名密碼,查詢SQL什么的都存在程序里,很容易被反編譯等方法看到。
建議把表示層和數(shù)據(jù)層邏輯分開(kāi),數(shù)據(jù)層對(duì)應(yīng)網(wǎng)頁(yè)的表示層提供接口,同時(shí)在為Android手機(jī)端提供一個(gè)接口,簡(jiǎn)介訪問(wè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、打開(kāi)Tableau軟件。
2、在連接中,找到紅框位置的MySQL,點(diǎn)擊開(kāi)始連接Mysql。
3、在彈出的連接界面,輸入Mysql服務(wù)器地址、端口、用戶名、密碼。
4、輸入完成后,點(diǎn)擊紅框位置 確認(rèn) 進(jìn)行連接。
5、此時(shí)已經(jīng)連接到MySQL服務(wù)器上,為了測(cè)試 我們點(diǎn)擊紅框位置 選擇數(shù)據(jù)庫(kù)查看一下。
一、首先要加載JDBC驅(qū)動(dòng)包。
步驟:右擊項(xiàng)目找到build path-configure build path-libraries——add External JARs添加驅(qū)動(dòng)包
二、寫測(cè)試類:TestCon.java
(在此之前,首先
1.在自己的電腦上Mysql下確定賬戶是"root",密碼是"123456";
2.進(jìn)入賬戶,創(chuàng)建數(shù)據(jù)庫(kù)cui;
3.在數(shù)據(jù)庫(kù)cui下面,創(chuàng)建表test1 包含_id(int 類型自動(dòng)增加) username(String 類型)、password(String 類型);
4.在表中插入數(shù)據(jù),以便顯示
)
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=truecharacterEncoding=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 }
運(yùn)行結(jié)果:
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
當(dāng)前名稱:app怎么連接mysql,行車記錄儀app怎么連接
轉(zhuǎn)載來(lái)源:http://m.rwnh.cn/article24/dsdjice.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)站策劃、服務(wù)器托管、用戶體驗(yàn)、軟件開(kāi)發(fā)、全網(wǎng)營(yíng)銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容