Kết nối giữa SQL Server với Android studio_Hệ quản trị cơ sở dữ liệu_DBMS

73 1.7K 7
Kết nối giữa SQL Server với Android studio_Hệ quản trị cơ sở dữ liệu_DBMS

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

I. CÀI ĐẶT MÔI TRƯỜNG 1. Cài đặt Java Search google: “jdk download ” Tải về: http:www.oracle.comtechnetworkjavajavasedownloadsjdk8downloads2133151.html 2. Cài đặt genymotion và vitual box (máy ảo trên PC) Search google: “ genymotion download” Link: https:www.genymotion.com Đăng ký và đăng nhập tài khoản: Bấm vào Sign In để tạo một tài khoản Tải về: Tải và cài đặt Genymotion từ trang web trên. Lưu ý là phải đăng nhập vào trang web thì mới tải được. Khuyến cáo nên tải phiên bản Genymotion kèm với VirtualBox (một máy ảo tương tự như VMWare nhưng miễn phí và nguồn mở). Cài đặt vitual box: 3. Cài đặt android studio Search google: “ android studio download” Tải về: https:developer.android.comstudioindex.html Cài đặt android studio: II. CẤU HÌNH SQL SERVER 1. Config SQL Server cho phép kết nối từ xa Mở SQL Server Configuration Manager

KẾT NỐI ANDROID – SQLSERVER 2012 Sinh viên thực hiện: Stt I Họ tên Trần Thị Thơm Huỳnh Nhật Thành MSSV 14110192 14110178 CÀI ĐẶT MÔI TRƯỜNG Cài đặt Java - Search google: “jdk download ” - Tải về: http://www.oracle.com/technetwork/java/javase/downloads/jdk8downloads-2133151.html - Cài đặt: Nhập vào thư mục mà JDK cài đặt ra, đặt là: C:\DevPrograms\Java\jdk1.8.0_45\ Ngay sau cài đặt JDK, cài đặt tiếp tục hỏi bạn vị trí JRE cài Ở chọn: C:\DevPrograms\Java\jre.8.0_45\ Java cài đặt thành công Kết bạn thư mục: - Cấu hình biến môi trường cho Java: Nhấp chuột vào This PC, chọn Properties Nhấp New để tạo môi trường tên JAVA_HOME Nhập vào đường dẫn tới thư mục JDK Variable name: JAVA_HOME Variable value: C:\DevPrograms\Java\jdk1.8.0_45 Tiếp theo sửa đổi biến môi trường path Thêm vào phía trước giá trị môi trường path: %JAVA_HOME%\bin; Bạn cài đặt cấu hình Java thành công Cài đặt genymotion vitual box (máy ảo PC) - Search google: “ genymotion download” - Link: https://www.genymotion.com/ - Đăng ký đăng nhập tài khoản: Bấm vào Sign In để tạo tài khoản + password + ";"; conn = DriverManager.getConnection(ConnURL); } catch (SQLException se) { Log.e("ERRO", se.getMessage()); } catch (ClassNotFoundException e) { Log.e("ERRO", e.getMessage()); } catch (Exception e) { Log.e("ERRO", e.getMessage()); } return conn; } } Tạo Class DangNhapDAO: viết câu truy vấn để xử lý đăng nhập public class DangNhapDAO { ConnectionDB connectionDB = new ConnectionDB(); String z = ""; Connection = connectionDB.CONN(); boolean kiemTra = false; ResultSet rs; public String Login(String userid, String password){ try{ if(con == null){ z = "Lỗi kết nối"; } else { String query = "select * from Usertbl where UserId='" + userid + "' and password='" + password + "'"; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); if (rs.next()) { z = "Đăng Nhập Thành Công"; } else { z = "Sai User Mật Khẩu"; } } }catch (Exception ex) { z = "Exceptions"; } return z; } } 1.2 Giao diện để load, kiểm tra liệu Tạo class LoadCountries: để viết câu truy vấn public class LoadCountries { ConnectionDB connectionDB = new ConnectionDB(); String z=""; ResultSet rs; Connection = connectionDB.CONN(); final ArrayList list = new ArrayList(); public ArrayList getListCountries(){ try { PreparedStatement statement = con.prepareStatement("EXEC viewAllCountries"); rs = statement.executeQuery(); while (rs.next()) { list.add(rs.getString("ProName")); } } catch (Exception ex) { z = "Exceptions"; } return list; } public ArrayList getCountriesName(String edtid) { try{ PreparedStatement statement = con.prepareStatement("EXEC viewCountry '"+edtid+"'"); rs = statement.executeQuery(); while (rs.next()) { list.add(rs.getString("ProName")); } }catch (Exception ex){ z= ex.getMessage(); } return list; } } Tạo class Countries: để xử lý public class Countries extends AppCompatActivity{ TextView lblheader; Typeface font; Button btnviewall,btnview; ListView lstcountry; EditText edtid; ArrayList list = new ArrayList(); ConnectionDB connectionDB; LoadCountries loadCountrie; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.android_countries); lblheader = (TextView) findViewById(R.id.lblheader); lstcountry = (ListView) findViewById(R.id.lstcountry); btnviewall = (Button) findViewById(R.id.btnviewall); btnview = (Button) findViewById(R.id.btnview); edtid = (EditText) findViewById(R.id.edtid); final LoadCountries loadCountries = new LoadCountries(); btnviewall.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { list = loadCountries.getListCountries(); ArrayAdapter adapter = new ArrayAdapter(Countries.this, android.R.layout.simple_list_item_1, list); lstcountry.setAdapter(adapter); } }); btnview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { list = loadCountries.getCountriesName(edtid.getText().toStr ing()); ArrayAdapter adapter = new ArrayAdapter(Countries.this, android.R.layout.simple_list_item_1,list); lstcountry.setAdapter(adapter); } }); } } Về giao diện MainActivity khai báo , ánh xạ đối tượng Viết kiện Click cho Button Login public class MainActivity extends AppCompatActivity { Button btnDongY; EditText edtTenDangNhap, edtMatKhau; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_dangnhap); init(); btnDongY.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String tendangnhap = edtTenDangNhap.getText().toString(); String matkhau = edtMatKhau.getText().toString(); DangNhapDAO dangnhapDB = new DangNhapDAO(); String z = dangnhapDB.Login(tendangnhap,matkhau); final Intent myIntent = new Intent(MainActivity.this,Countries.class); startActivity(myIntent); Toast.makeText(MainActivity.this,z,Toast.LENGTH_SHO RT).show(); } }); } private void init() { edtTenDangNhap = (EditText)findViewById(R.id.edtTenDangNhap); edtMatKhau = (EditText)findViewById(R.id.edtMatKhau); btnDongY = (Button)findViewById(R.id.btnDongY); } } Bước cuối phải cấp quyền cho ứng dụng truy cập internet để lấy liệu từ xa Vào AndroidMainFest.xml Ấn nút run để chương trình thực thi Sau chạy xong Gõ tên đăng nhập mật bên bảng Usertbl mà ta chèn vào SQLserver V MỘT SỐ LỖI GẶP PHÃI TRONG QUÁ TRÌNH LÀM BÀI VÀ CÁCH FIX Lỗi 1: E/ERRO: Network error IOException: failed to connect to /192.168.1.103 (port 1433): connect failed: ECONNREFUSED (Connection refused) Cách fix lỗi: Chỉnh lại Ip bảng TCP/Ip SLQ Configuaration (Để ý IP Address : 127.0.0.1 chỉnh lại IP Static máy) Tiếp theo bật SQL SERVER BROWSER Nếu SQL Server Browser lỗi vào service hệ thống Start lại Lỗi : java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.PreparedStatement java.sql.Connection.prepareStatement(java.lang.String)' on a null object reference  Thiếu thư viện JBDC để kết nối CSDL APP Cách fix lỗi : thêm thư viện jtds-2.1.7 vào project 02-27 20:28:43.765 12397-12436/com.example.nhatthanh.exlogin1 E/ERRO: Network error IOException: socket failed: EACCES (Permission denied)   Thiếu quyền truy cập internet Lỗi 3: Chưa cấp quyền truy cập Properties -> server Roles chọn quyền - sysadmin: làm thành phần biểu diễn hành động server - serveradmin: thành phần thay đổi lựa chọn để cấu hình server tắt server - securityadmin:là thành phần quản lý đăng nhập quyền sở hữu họ Họ giấy phép GRANT(cấp), DENY(từ chối), and REVOKE(thu hồi)quyền truy cập server Họ giấy phép GRANT(cấp, DENY(từ chối), and REVOKE(thu hồi) quyền truy cập database học truy cập đến sở liệu Ngoài ra, họ đặt lại mật cho đăng nhập SQL server - processadmin: thành phần kết thúc tiến trình chạy thể SQL server - setupadmin:là thành phần thêm xóa liên kết server - bulkadmin: thành phần chạy câu lệnh BULK INSERT - diskadmin: sử dụng cho quản lý tập tin đĩa - dbcreator: thành phần tạo, thay đổi, xóa, hồi phục lại database - public: Mỗi SQL server đăng nhập thuộc vai trò public server Khi máy chủ server không cấp từ chối cấp phép cụ thể đối tượng securable, người sử dụng kế thừa quyền cấp cho công chúng vào đối tượng Chỉ gán quyền đối tượng bạn muốn đối tượng sẵn cho tất người dùng ... CẤU HÌNH SQL SERVER Config SQL Server cho phép kết nối từ xa Mở SQL Server Configuration Manager Ở menu bên trái, chọn vào mục SQL Server 2012 Services -> Bên phải tìm mục SQL Server (MSSQLSERVER)... nhấn Next để tiếp tục Nhấn Next để tiếp tục Quá trình cài đặt ngắt khởi động lại kết nối mạng ảnh hưởng đến kết nối mạng diễn ra, bạn kiểm tra lại có thực tác vụ mạng không Chọn Yes để tiếp tục... Services -> Bên phải tìm mục SQL Server (MSSQLSERVER) (chú ý dịch vụ SQL Server, tên tùy thuộc vào Instant SQL mà bạn cài vào máy, MSSQLSERVER, Instant bạn khác) Click phải vào chọn Properties (xem

Ngày đăng: 06/05/2017, 11:34

Từ khóa liên quan

Mục lục

Tài liệu cùng người dùng

Tài liệu liên quan