Chọn một trang web để nhận nội dung đã dịch nếu có và xem các sự kiện và ưu đãi tại địa phương. Dựa trên vị trí của bạn, chúng tôi khuyên bạn nên chọn.
Bạn cũng có thể chọn một trang web từ danh sách sau
Làm thế nào để có được hiệu suất trang web tốt nhất
Chọn trang Trung Quốc [bằng tiếng Trung hoặc tiếng Anh] để có hiệu suất trang tốt nhất. Các trang web quốc gia khác của MathWorks không được tối ưu hóa cho các lượt truy cập từ vị trí của bạn
Trong bài đăng này, chúng ta sẽ xem Cách chúng ta có thể Kết nối Android với Cơ sở dữ liệu MySQL bằng trình kết nối mysql. cái lọ. Bạn có thể tải xuống Thư viện Jar bằng Liên kết này
Ghi chú. Tôi không sử dụng bất kỳ loại Webservice hoặc thư viện web api nào khác
Vì vậy, hãy bắt đầu
Android kết nối cơ sở dữ liệu MySQL theo lập trình
Tạo Ứng dụng Android mới trong Android Studio với
Tên gói hàng. mã song song. ứng dụng mysql
tên ứng dụng. Ứng dụng MySQL
Bạn luôn có thể đặt tên Gói và tên Ứng dụng theo yêu cầu của bạn
ứng dụng làm việc
Trước tiên, ứng dụng sẽ thử kết nối với Cơ sở dữ liệu MySQL và khi kết nối Cơ sở dữ liệu thành công, ứng dụng Android sẽ hiển thị dữ liệu từ một bảng trên TextView của Android. Nó sẽ kết nối qua địa chỉ IP của Cơ sở dữ liệu với Thông tin xác thực Cơ sở dữ liệu chính xác
Tải xuống mã nguồn
Tập lệnh cơ sở dữ liệu MySQL
create schema myDB use myDB create table tblCountries [ ID int NOT NULL AUTO_INCREMENT primary key, Country varchar[255] NOT NULL ] Insert into tblCountries [Country] values ['India'] Insert into tblCountries [Country] values ['Australia'] Insert into tblCountries [Country] values ['Mauritius'] Insert into tblCountries [Country] values ['USA'] Insert into tblCountries [Country] values ['England'] Insert into tblCountries [Country] values ['New Zealand'] Insert into tblCountries [Country] values ['Spain'] Select * from tblCountries select distinct Country from tblCountries
Bây giờ, hãy mở Activity_main của bạn. xml và chỉnh sửa nó như bên dưới
hoạt động_chính. xml
Bố cục chứa một TextView của Android để hiển thị dữ liệu Bảng của chúng tôi từ Cơ sở dữ liệu MySQL và hai nút, một để kết nối với Cơ sở dữ liệu và một để xóa nội dung TextView
Bây giờ, hãy mã hóa nó
Hoạt động chủ yêu. java
Khai báo các biến này trong lớp Java của bạn
private static final String url = "jdbc:mysql://192.168.0.192:3306/myDB"; private static final String user = "hitesh"; private static final String pass = "1234"; Button btnFetch,btnClear; TextView txtData;
Và khởi tạo chúng theo phương thức onCreate
txtData = [TextView] this.findViewById[R.id.txtData]; btnFetch = [Button] findViewById[R.id.btnFetch]; btnClear = [Button] findViewById[R.id.btnClear];
url, user, pass phải là thông tin đăng nhập hợp lệ và chính xác của cơ sở dữ liệu mySql của bạn để kết nối Android-MySQL thành công
192. 168. 0. 192 là Địa chỉ IP của Máy tính xách tay của tôi và 3306 là số Cổng của tôi để kết nối trên
Tạo một Android ASyncTask. Điều này sẽ chấp nhận lệnh và cố gắng kết nối với Cơ sở dữ liệu của bạn và khi kết nối thành công với MySQL sẽ gán một biến Chuỗi với dữ liệu bảng
Mã số
private class ConnectMySql extends AsyncTask { String res = ""; @Override protected void onPreExecute[] { super.onPreExecute[]; Toast.makeText[MainActivity.this, "Please wait...", Toast.LENGTH_SHORT] .show[]; } @Override protected String doInBackground[String.. params] { try { Class.forName["com.mysql.jdbc.Driver"]; Connection con = DriverManager.getConnection[url, user, pass]; System.out.println["Databaseection success"]; String result = "Database Connection Successful\n"; Statement st = con.createStatement[]; ResultSet rs = st.executeQuery["select distinct Country from tblCountries"]; ResultSetMetaData rsmd = rs.getMetaData[]; while [rs.next[]] { result += rs.getString[1].toString[] + "\n"; } res = result; } catch [Exception e] { e.printStackTrace[]; res = e.toString[]; } return res; } @Override protected void onPostExecute[String result] { txtData.setText[result]; } }
Mã đầy đủ của MainActivity. tập tin java
Hoạt động chủ yêu. java
package parallelcodes.mysqlapp; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; public class MainActivity extends AppCompatActivity { private static final String url = "jdbc:mysql://192.168.0.192:3306/myDB"; private static final String user = "hitesh"; private static final String pass = "1234"; Button btnFetch,btnClear; TextView txtData; @Override protected void onCreate[Bundle savedInstanceState] { super.onCreate[savedInstanceState]; setContentView[R.layout.activity_main]; txtData = [TextView] this.findViewById[R.id.txtData]; btnFetch = [Button] findViewById[R.id.btnFetch]; btnClear = [Button] findViewById[R.id.btnClear]; btnFetch.setOnClickListener[new View.OnClickListener[] { @Override public void onClick[View v] { // TODO Auto-generated method stub ConnectMySql connectMySql = new ConnectMySql[]; connectMySql.execute[""]; } }]; btnClear.setOnClickListener[new View.OnClickListener[] { @Override public void onClick[View view] { txtData.setText[""]; } }]; } private class ConnectMySql extends AsyncTask { String res = ""; @Override protected void onPreExecute[] { super.onPreExecute[]; Toast.makeText[MainActivity.this, "Please wait...", Toast.LENGTH_SHORT] .show[]; } @Override protected String doInBackground[String.. params] { try { Class.forName["com.mysql.jdbc.Driver"]; Connection con = DriverManager.getConnection[url, user, pass]; System.out.println["Databaseection success"]; String result = "Database Connection Successful\n"; Statement st = con.createStatement[]; ResultSet rs = st.executeQuery["select distinct Country from tblCountries"]; ResultSetMetaData rsmd = rs.getMetaData[]; while [rs.next[]] { result += rs.getString[1].toString[] + "\n"; } res = result; } catch [Exception e] { e.printStackTrace[]; res = e.toString[]; } return res; } @Override protected void onPostExecute[String result] { txtData.setText[result]; } } }
Và bây giờ chạy ứng dụng của bạn. Khi nhấn vào nút “Tìm nạp dữ liệu”, nó sẽ tìm nạp Dữ liệu từ bảng Cơ sở dữ liệu MySQL