1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      手機(jī)怎么玩MySQL 手機(jī)怎么玩dnf電腦端

      android怎么鏈接數(shù)據(jù)庫mysql

      有點多請耐心看完。

      成都創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的臨武網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

      希望能幫助你,還請及時采納謝謝。

      一.前言

      android連接數(shù)據(jù)庫的方式有兩種,第一種是通過連接服務(wù)器,再由服務(wù)器讀取數(shù)據(jù)庫來實現(xiàn)數(shù)據(jù)的增刪改查,這也是我們常用的方式。第二種方式是android直接連接數(shù)據(jù)庫,這種方式非常耗手機(jī)內(nèi)存,而且容易被反編譯造成安全隱患,所以在實際項目中不推薦使用。

      二.準(zhǔn)備工作

      1.加載外部jar包

      在Android工程中要使用jdbc的話,要導(dǎo)入jdbc的外部jar包,因為在Java的jdk中并沒有jdbc的api,我使用的jar包是mysql-connector-java-5.1.18-bin.jar包,網(wǎng)絡(luò)上有使用mysql-connector-java-5.1.18-bin.jar包的,自己去用的時候發(fā)現(xiàn)不兼容,所以下載了比較新版本的,jar包可以去官網(wǎng)下載,也可以去百度,有很多前人們上傳的。

      2.導(dǎo)入jar包的方式

      方式一:

      可以在項目的build.gradle文件中直接添加如下語句導(dǎo)入

      compile files('libs/mysql-connector-java-5.1.18-bin.jar')

      方式二:下載jar包復(fù)制到項目的libs目錄下,然后右鍵復(fù)制過來的jar包Add as libs

      三.建立數(shù)據(jù)庫連接

      protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_jdbc);

      new Thread(runnable).start();

      }

      Handler myHandler=new Handler(){

      public void handleMessage(Message msg) {

      // TODO Auto-generated method stub

      super.handleMessage(msg);

      Bundle data=new Bundle();

      data=msg.getData();

      //System.out.println("id:"+data.get("id").toString()); //輸出第n行,列名為“id”的值

      Log.e("TAG","id:"+data.get("id").toString());

      TextView tv= (TextView) findViewById(R.id.jdbc);

      //System.out.println("content:"+data.get("content").toString());

      }

      };

      Runnable runnable=new Runnable() {

      private Connection con = null;

      @Override

      public void run() {

      // TODO Auto-generated method stub

      try {

      Class.forName("com.mysql.jdbc.Driver");

      //引用代碼此處需要修改,address為數(shù)據(jù)IP,Port為端口號,DBName為數(shù)據(jù)名稱,UserName為數(shù)據(jù)庫登錄賬戶,Password為數(shù)據(jù)庫登錄密碼

      con =

      //DriverManager.getConnection("jdbc:mysql://192.168.1.202:3306/b2b", "root", "");

      DriverManager.getConnection("jdbc:mysql://",

      UserName,Password);

      } catch (SQLException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

      } catch (ClassNotFoundException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

      }

      try {

      testConnection(con); //測試數(shù)據(jù)庫連接

      } catch (java.sql.SQLException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

      }

      }

      public void testConnection(Connection con1) throws java.sql.SQLException {

      try {

      String sql = "select * from ecs_users"; //查詢表名為“oner_alarm”的所有內(nèi)容

      Statement stmt = con1.createStatement(); //創(chuàng)建Statement

      ResultSet rs = stmt.executeQuery(sql); //ResultSet類似Cursor

      //codeResultSet/code最初指向第一行

      Bundle bundle=new Bundle();

      while (rs.next()) {

      bundle.clear();

      bundle.putString("id",rs.getString("userid"));

      //bundle.putString("content",rs.getString("content"));

      Message msg=new Message();

      msg.setData(bundle);

      myHandler.sendMessage(msg);

      }

      rs.close();

      stmt.close();

      } catch (SQLException e) {

      } finally {

      if (con1 != null)

      try {

      con1.close();

      } catch (SQLException e) {}

      }

      }

      };

      注意:

      在Android4.0之后,不允許在主線程中進(jìn)行比較耗時的操作(連接數(shù)據(jù)庫就屬于比較耗時的操作),需要開一個新的線程來處理這種耗時的操作,沒新線程時,一直就是程序直接退出,開了一個新線程處理直接,就沒問題了。

      當(dāng)然,連接數(shù)據(jù)庫是需要網(wǎng)絡(luò)的,千萬別忘了添加訪問網(wǎng)絡(luò)權(quán)限:

      uses-permission android:name=”android.permission.INTERNET”/

      四.bug點

      1.導(dǎo)入的jar包一定要正確

      2.連接數(shù)據(jù)庫一定要開啟新線程

      3.數(shù)據(jù)庫的IP一定要是可以ping通的,局域網(wǎng)地址手機(jī)是訪問不了的

      4.數(shù)據(jù)庫所在的服務(wù)器是否開了防火墻,阻止了訪問

      ————————————————

      版權(quán)聲明:本文為CSDN博主「shuaiyou_comon」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。

      原文鏈接:

      Android手機(jī)app 鏈接服務(wù)器的mysql 讀取數(shù)據(jù)庫

      手機(jī)是不能直接去連接你服務(wù)器的mysql數(shù)據(jù)庫

      請在你的服務(wù)端寫代碼去連接mysql數(shù)據(jù)吧

      Mysql連接方法

      1. 加載數(shù)據(jù)庫驅(qū)動:?Class.forName("org.gjt.mm.mysql.Driver"); //加載數(shù)據(jù)庫驅(qū)動

      String url = "jdbc:mysql://localhost:3306/test";

      String user = "root";

      String passowrd = "123456";

      2. 獲取數(shù)據(jù)庫連接Connection con數(shù)=?DriverManager.getConnection(url,user,password)

      3. 獲取SQL執(zhí)行器 PreparedStatement prepare = con.prepareStatement("SQL語句")

      4. 執(zhí)行SQL語句,得到結(jié)果集 ResultSet result = prepare.executeQuery();

      while(result.next()){

      //讀取結(jié)果

      }

      最后不要忘記導(dǎo)入jdbc驅(qū)動包

      純工手打字,請采納哈

      android怎么連接mysql數(shù)據(jù)庫

      用Android程序去直連MySQL數(shù)據(jù)庫,覺得這樣做不好,出于安全等方面考慮。數(shù)據(jù)庫地址,用戶名密碼,查詢SQL什么的都存在程序里,很容易被反編譯等方法看到。

      建議把表示層和數(shù)據(jù)層邏輯分開,數(shù)據(jù)層對應(yīng)網(wǎng)頁的表示層提供接口,同時在為Android手機(jī)端提供一個接口,簡介訪問數(shù)據(jù)庫,這接口可以2端都保持一致,比如XML+RPC或者json等等,Android端也有現(xiàn)成的東西能直接用,既安全又省事。

      android 鏈接mysql數(shù)據(jù)庫實例:

      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();

      android手機(jī)軟件開發(fā)中 怎么連接Mysql數(shù)據(jù)庫

      一、首先要加載JDBC驅(qū)動包。

      步驟:右擊項目找到build path-configure build path-libraries——add External JARs添加驅(qū)動包

      二、寫測試類:TestCon.java

      (在此之前,首先

      1.在自己的電腦上Mysql下確定賬戶是"root",密碼是"123456";

      2.進(jìn)入賬戶,創(chuàng)建數(shù)據(jù)庫cui;

      3.在數(shù)據(jù)庫cui下面,創(chuàng)建表test1 包含_id(int 類型自動增加) 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


      名稱欄目:手機(jī)怎么玩MySQL 手機(jī)怎么玩dnf電腦端
      分享URL:http://www.ef60e0e.cn/article/dojesoe.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        洛南县| 雅安市| 莲花县| 大连市| 罗城| 息烽县| 泽州县| 姜堰市| 嘉黎县| 建宁县| 车险| 疏附县| 柳林县| 多伦县| 和硕县| 上饶县| 德庆县| 镇康县| 鸡东县| 临颍县| 灌云县| 咸丰县| 会宁县| 遂昌县| 徐州市| 确山县| 离岛区| 隆德县| 孝义市| 深水埗区| 南召县| 铜梁县| 宁津县| 昂仁县| 安西县| 扶绥县| 桂平市| 石城县| 河东区| 宁德市| 犍为县|