Android To MySql Database (Without JSON)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let us see an example of connecting a MySQL database from an android application in this example you are just trying to connect to a database mysql database which is installed anywhere in our scenario I will install it in my laptop and I will try to connect from an Android application the important points are you can connect to an Android you can connect from an Android application to a MySQL database or any other database using JSON you have to write a PHP code or any other server-side script which generates JSON to which the client application which is the mobile application connects and gets the data we have seen an example of this a video of this before now we are going to see how to connect to a MySQL database from a mobile application and Android application without using JSON first and foremost we need to run a MySQL server how to install a MySQL server in you can download what is called the samp you can go to the internet you can google it you can use zamp is x a MTP if you go here you will find zamp installers and downloads you can download the version which is appropriate to your machine if it is Windows Linux or OSX or whatever install it after you install your what is called zamp control panel in this panel zamp is made up of apache mysql and others we need the Apache which is a webserver we also need the MySQL so we are starting these two services running on your machine if suppose your machine is already having a web server installed if you start this it might not run it will complain that the server cannot start so in my case it is started how to confirm it is started I can go to the browser I can type localhost and press Enter ok so you see how we'll get this page and I also have to go to my control panel sorry my command prompt type ipconfig provided I have some internet connectivity in my case this is my IP address 10 6 73 64 I also have a wireless connect which is this IP I'm going to use my wired connection 10 673 64 so if I go here and type 10 673 64 press Enter I should get this page around the next step to do to go to your a VD which is your emulator in your Android studio I'm going to use Android studio in this example okay I open a browser there in the browser type this IP address 10.6 dot 73 dot 64 this is to confirm whether I am able to access this server from my emulator or not similarly if you are going to test this application from your mobile phone connect your mobile phone to any Wi-Fi and make sure you can access this IP address which is my Wi-Fi is IP address okay if I can access it that means I'll be able to access the database if I'm not able to access it I won't be able to access my database now so my server is running my server is running you can see the MySQL runs on port number 3 3:06 and from the browser I am able to access my Apache server so I can see it is running heads amp is running now going back to my Android application it is similar to any other Android application you write a program the Java program which extends activity you have oncreate and other methods I'm not going to explain this detail I will only explain those that are relevant to MySQL database you need what is called as a MySQL connector this one you need this file MySQL connected Java some version we need this file what you have to do is you go here click on the app the project right click it click select new create a directory in this directory you put libs ok then click OK here automatically to create this directory then if you have downloaded this MySQL connector file somewhere just copy and paste it here so you can right click and paste the file here it will paste it so now the file the library is already included in your package this is done number two you got to go to what is called build.gradle which is this file in this file go to the end of the file and add this line you say dependencies compiled blank space files within bracket lips which is this folder lips MySQL connector whatever the whole name close the quote and the bracket okay you might be asked to sink this gradall you can accept sinking the grad so once this is done now if I go to my Java program I can import Java dot SQL dot connection and all these things remember these classes are available in that package you can see here if I go into my app if I go into Lib I have this jar file I open it using 7-zip let's open it as an archive file you see it contains the necessary packages inside ok it has the MySQL JDBC all these packages which I need are already inside it's available I can use it so now if you go to your program you have to say this you need an URL you need the username you need the password to do this what you can do go to your browser and open what is called PHP my admin' from the same ip address PHP my admin here you can go to databases if you want to create a brand new database you can type the name here create I have already created which is test database in the test database if you go to privileges you will see who can access it ok so you can create a new user provide him with access remember if you click the new user this field you can say it's accessible only from localhost if you leave it any host it can be accessible by anybody ok so that is a point we want anybody to access it give the proper username password permit them to do all these access and you then click a go here ok so then automatically the user will be enabled you will have a user like this in my case I have used a username test one two three and the password is test and so in my code I write the URL is JDBC : MySQL colon double slash this is my IP address if you notice this is my IP address and the port number is three three oh six because I see here this is three 3d t o6 my SQL is running on three three zero six then this test is my database name name of my DB so if you go here I told you this is my database databases test so I'm going to see the structure of the database it has two tables one is instructor one is test I don't bother about rest I will bother about only the instructor this is the instructor and it has my data inside okay some name some ID some Department you can check the structure of the database and all this if you want to so you can change you can create it's pretty easy here you can also run your SQL commands here okay let's go back to our Android code so this is my database name this is my username this is my password this is JDBC MySQL and this is my IP address this is the port number in which my SQL server is running if I am going to host my server somewhere else you have to change this part with that domain name plus the DB name the database name and all of this remain the same now inside my code this is the main point here okay this line here I am saying class that for name you load this driver you can see this is available here in my MySQL remember Java dot SQL is not in that library my mysql connector library it is this one the Conn dot MySQL is available here you can see calm MySQL JDBC all these things are here so use a class name put connection driver manager get connection with the URL which is the URL you define here and the username and the password then you can see you can issue konk dot create statement then you can start executing it okay you can execute if it is successful you will execute otherwise it won't so you can see execute Cori I am saying select star dot star from instructor because in my database I have instructor which contains this table it has some data also okay let's go in you know this is my table instructor you can see if I click browse I have some data I can add more data if you want so now when it is ready we just click it will run actually what I'm doing here is this here I'm reading the metadata okay and then I'm getting the column name let me run it column name I'm getting the actual value also remember if I want to get a string I put get string if I want to get integer I put integer and so on so here you can see what I am doing I am creating a statement I am executing the execute the query with the statement then getting a resultant set the result set using the result set I can iterate one row by row okay so here you can see I am getting the metadata as RS MD and I'm displaying the column name so if you go to my program here so it has to run and I'm getting this and I am displaying in my text view okay TV is accessing my text view from my from my layout five you can go to the layout file you will see that is text underscore view okay text underscore view so I am accessing it from here so these are the steps you got to do when you want to run your application okay so let me just run my application again so these are basic and you can see I have also introduced what is called this strict mode thread policy because if I want to access the database and things like this I am supposed to run it I can only access it using a background task which means I have to write what is called a sync task and do the job just to make things easy right now I'm just using this option but it's not compulsory always that you should use this actually it is not advisable to use strict policy but just to use you know what is called as a sync task that will do the job for you while the program is being run on my emulator here are few points to note just a recap quickly you need the library this is very important number two in your bread got Gradle you need to add that library inside number three in your program you have to change to the respective IP address and the port number in which you are service is running okay so you can see it has connected now it says database connection success my name departments and whatsoever so this is the basic of accessing a database from an Android application reminding you can do further thank you
Info
Channel: MI Buhari's Academic Channel
Views: 72,381
Rating: undefined out of 5
Keywords: Android, MySQL, JSON
Id: cDGc28XQnRo
Channel Id: undefined
Length: 11min 21sec (681 seconds)
Published: Mon Feb 23 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.