How to connect to MS SQL Server database from your Android App? - complete steps

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
dear friends welcome to my video in this video I will show you how to connect to your ms SQL database from your android app so let's begin but before I get into the Android studio to deliver this look you try to show you how to set the MSA spill server in your machine so for that just go to google and - you have to install two things one is ms SQL server so you can just type a message for download and the first link whatever it will come let's open it and this is the link where you will get to download the software the SQL Server ms SQL server you get to option expresso developer I will recommend go with developer option download and install you the second thing which you will need is probably at the end of this it also asks you to whether you want to install SQL Server management or not if you have not chosen that you can separately in solid just go in search for a system is download and again you can go to the first link and in this link you can use the download to download link for SMS which is SQL Server management studio so just download and install these tools on your local machine so once you have downloaded and installed I just tell you few things which you should take note of first thing is you to start the services respective services so press start and services you just go to the services in your machine in your computer and in that please scroll down to somewhere near SQL Server and please ensure that these three services ASP will sever ms SQL Server SQL Server agent it is still sever program even browser is important because we will communicate to SQL server over tcp/ip protocol so this is important so make sure all these ti running state if not run it any festering probably never a bad idea to even restart well so once you have done it the other thing which you will have to take note of the for so the port number you can figure out by going to a still server configuration manager just go to that s cool server configuration manger and inside that go to this network configuration and in protocol M is a school server MIT so both of these are enabled by default I think this is disabled so make sure you enable them by just by right click enable and disable unless you enable go to these properties and the IP address just call it to the bottom most and there are TCP a port number 140 Hz is the default which you get but sometimes it means it may get one one for three for something else but take a lot of it so this will be a port number and the port number on which your server is running for the IP address you can what you do is you can go to your command prompt and just type IP configuration who you need the IP also uses hosted on the local machine so this will be your IP at this order this is the IP address which I will use in my ad to interact with mine yeah with my a Miss SQL database okay so once you've done this settings I think that small is fine yeah so now what we can do is we can start our studio so for that we can do a system that is a studio we will try to configure our database so when when you click this you this kind of window will come up and this kind of it will ask for login for the first time I think it will only give you an option of windows authentication so that means it will automatically take you login user ID and password from your windows credentials so please log in through windows authentication I'll show you the steps how to enable this SPL server authentication as well so once you have gone into it you will get the structure so the server address will be on the top this is your server address where your server is running and then they are different Suites database at sector so first we'll create a new user ID and probably say login ID for our server because the windows authentication security so before we do that first I'll try to show you how you can enable the SQL or Hindi isn't as well so good right click on this master and then go to the properties pane and order in the securities field please ensure that you have moved to server authentication to Server SQL Server n windows authentication mode so this will take both it will take windows or indication also in several SQL Server authentication as well to log in to your sequel studio it still server management which we call SMS and so on so once you've done it then what we can do is we can create a new login which is you ID and password for us so login name we can keep probably test and then we can go for the of course then again he choose whether the slogan ID will take windows authentication but I will get another password in a program keep it same is test test and I enforce password policy probably yes and forceful expiration no I will just disable it and that's all and then I probably will also like to use some kind of several roles so I leave it as the probably owner or something I like that I was creator let me see if it's fine and ok so why I give you know that I was created because you know creating and writing in database through this user ID and password which we have just now created so I'd like to use them so if I explain this login so we can see our test login has been created here you can even modify it again by double clicking over here and this window will pop up again if we have forgotten something we can just redo it over here okay so once done it I will close and restart and ssme so that I can log into this my test you ID and password yeah so we came back and over here we have to by default of course to come who knows authentic essence will change to sequel SQL Server authentication and over here I was expecting this test to come but let me see yes so we have no login using our tester user and now we can start creating our database so in database mighty full UK system database in etcetera and over here we have already got one database but that is not excellent at that abyss was created using some other ID that's where so we create another database using this user for test so I will just name it as test database perfect and what is I think the list of things I'll leave it as it is so once created we will get a structure of all these things so what we can do over against now we can see if we can create a table in this database to create table there are two options either I can right click over here and then create a table directly but I think I use more of a programming programmatically so I will go to the scope query and here I'll just use my create table and then you can give your table in test table I think you should capital and then column name column you'll see even something you can give anything in the type of the column I will keep it simple and your short and then just click on execute so once done that I think it's successfully completed so I was expecting a table to be created if not electricity is some fish option and yes so see this table has come up I'd know this table would be empty so there only they will be one column filled so we will also insert something in this column so let let us insert now so I will probably create a new query on this database itself so insert in two and our table name is taste underscore table and you column name which is c1 and little values and values I can give anything over there whatever I want so probably I'll just type my first table yeah so this I'm expecting to get into the table so this was also successfully completed of course to get this I will again create a new query because I could have done in one so for the timing I'll say no over here I will go for another query I could have done in one page only but I want to keep keep separate query page spit select star from table names for table name is test1 Lucifer table so if I execute I am expecting a result my first table the entry which we just entered here so that is coming up over here perfect so we will be doing the same thing we have to pull extract this stream from this table of the database which we are running on this MSS pool server from our Android app so let's no.62 our Android PC or not so we'll just minimize this and bring our Android studio we'll just start a new project and we'll keep it a simple layout I want to spend much time on studio so my SQL or as in mind ms SQL server database connection you can name anything already I am just trying to put it yeah any name audio and we'll wait for some time to our idle to load up I think it was pretty quick here so no first thing before we get into layout or Java code first thing we may have to do is include something have a Gradle file so go to the Builder freedom and inside here in the please make sure you have gone in the apt level and not a project level of builder middle so first fine over here is the project level and the second one is of the app level so once you are here then what you can do is you could implement so why why we are doing this is because we need the required tribals to connect to the database so drivers are not by default available in our and produce view so that's why we have lowered so okay so we will have a do net dot source force dot jtd is : : 23.1 and then sync know the syncing is successful that means this implementation has been included in our environment so we will be using the JDC wheel driver quality ideas there are two options one is GDS and one is from microsoft itself the LEDs is considered to be much more reliable and secure that's why I use but you can even explore the other option this database is freely available on first force so that's why you have to use this to get the required libraries you know environment but once this is done the next thing that we'll do is we'll in the manifest file will dictate declare the permission and the permission which we need is to use the internet so because why we need the internet permission is because our app will be interacting with the database although both are hosted on the same machine but even though it will use Internet the tcp/ip protocol to interact with each other so that's why access to Internet is needed and anyway most of the times this kind of applications of or hosted on some server outside in the internet and you will be needing that the tcp/ip excess to type so you need access to Internet so normally these days is sufficient to declare the internet permission here in Memphis values for this sufficient but I this also show you how you can ensure that in the older versions where it was required to take the permission from the user who this is just you declare it in manifest file and it will have the access to the internet because it doesn't needs explicitly information from a user from your Java code but despite it I will show you how you can do it so go to this Java code in the one fit metal and here you can just put request permission and we can make this will be string when you finished and permission granted this so this is what you have to add it here but in the new versions of Android you don't need to explicitly put it by default the moment it's defining manifest file it gets the exes okay so I think more or less our in from my own perspective we are done I'll just do some things in the layout before I get into layout I'll just define a dummy or a public metal which I'll call [Music] ya so this is the middle I will call from one of my button which I probably put a button here some constraints so when a user presses is button the respective output what I was expecting here it will fetch the data from test cable and so Linda and what actually switch back to oh yeah yeah and mold is I think we are done in the button over here for this text box we'll use this text move to display the our output so we will keep it will just give some ID to it by default was not even so Xu Goodwin ID and then we can make these some points friends first I this default ones in the constraints once again perfect yeah or there are some warnings which is hardcore text if it is six you buy this flip of six mixture so my oldest I'm only out perspective at and I want to spend much time over you know I don't know to Java code in Java code this this line is you may or you may not put this up to you and no we will first see ladies we need handle to ever text you or widget by what we just created so that we can take it using text you to find you by ID ID text yes so know whenever we have my output will just use this text new to write our in this text view so we let's start defining some of the local variables which will be needed in this class so test which will need is say for example will need okay IP address of the database so that was something I think would think I will go to this come on window it is 192 168 1 1 0 1 so I'll just use that one nine two six 1001 please make sure this IP and all this informations are accurate even a small mistake will lead you to errors and you spend hours fixing them so second is port the port number also we caught it it was a 1 4 3 3 if you remember but it from this s2 server configuration go to this protocol for ms SQL server right click over here and in the properties in the IP address you will get the port number over here 140 okay so this was a place where for maybe put a port IP v got it what is we will need is we need the class name which we'll use so private class will be again so we have n even included that was net for 4j TD is not JDBC driver yeah and in class probably I will have to use some other way to correct it just know what else meaning it is we have a TV report in your class we have we need database to which we let connect to so our database will be database and the database name we can get it from our system is yeah so the database name is test database so where D is capital so please I am Not sure I think we have to be case sensitive and so you type the correct case and last we need is the credentials to exist which is user ID and password so I will use your name will be it was test and the password also was test so don't your make sure that you correctly put the password as well just so that's all one thing what we can do is we can make the URL directly over here itself so I will just make another static string string and URL which will nothing but the combination of our I T it is important so for that part you have to do is go to first pad JDBC : j OT d is : SQL server [Music] and then we can use the IP which we have just defined above IP and then we will have our poor with separate by column so no we can have po and what else we can have is we can have a database so that will got a bit static so this will directly connect through using this so it will use the driver which we have installed yet it is driver and it will try to exit a spill server on this I import and at this that is over here so that's Oh what else we need is probably will need one connection so we can just initiate it here like this yeah so yeah no we can jump to our method or implementation of the logic before we proceed we shouldn't make sure that we have defined the system or policy sig more so trade policy so this will NCO that our connection is the set pattern with all so and once we have it then you can this set it check more about trade policy and what you are defined over which policy that's it yeah so make sure you include this lines and now start our intimidation so first we should although the classes are there but we should make sure that we explicitly tailor our we explicitly enforce the inclusion of the class in our act but I can see this is some problem so if I just take any renewable friends so I think the problem is happening is you have classes over there and he also so no should be better I think just was for name so make sure that this you have some different kind of name probably class yeah and once you have it then probably you can use your classes to be moved over here I think it will give some kind of exceptions so just sound like a she oh yes sufficient so once we have it and now we can start connection connection to driver get connection and over here the arguments will be the first will be URL which we've already defined the second is a username so which we have a username and the third will the password which we also have password you that's it these again some kind of exception so you may have to include it so I will always before to add cash clock because there is no flexible to understand where this is being thrown yeah so once this is done probably we can use this approach to check text and say something like success here so that we know that this connection has been successful and probably we can also put failure in the other catch progress it's a kind of like debugging technique but you can always read the log file also and you can put different text or in you can identify from where the failure is coming or issue is coming up okay so once we have done this now what else we have to do is in the create during this spill button we can just quickly use this connection what we created in our own plate metal to run our query so for that probably in this SQL but on first thing I'll check is whether this connection is null or not well if it's not no point person if it's not null then only do that below and what I will like to do is first make a statement and a statement made by mr. yeah and now in this statement I can execute the query but for that I have to first have a little set these are set equal to statement Yugi could coding and over here we could form our our query so I think I'll use our same query which we use here this is just letter stop because I want to fish the control see this data my first table and control me yeah and but I can see there are lots of fighting it's substance again so I'm strong try/catch and I would prefer to keep this off you in depth I catch I don't want to keep creating lots of type matches all over the place ok so once we have our results in this a little sip then what we can do is we can use this result set to get our end so what we can do is we can iterate through this Y reverse it next then we can use our textview do set text okay stream and we have to pass the column index which is the first one for our case and that's it is what I think and if it's about this part is not true I mean if our connection is null then you can probably again print something very simple over here like else takes you go to set text and connection these perfect so I think more or less our corazon will just quickly run it and see what happens in our emulator has come down it is started so I'll just bring it up I'll probably bring in starvation so this shows success that means this connection has been successful so this is being printed again line number 48 so this we always win but but nothing happened and the reason for that is probably the button click is not yet connected to this SQL button method so I'll just quickly go here in the layout and here in the button if scroll down unclick associative button method so if you go back to Java code you will see it has become more black and will restart and wait again so it has come up again and it is showing success so I'll pick this button and it shows my first table perfect that's the query which we had in our server so this is the NP which we did over there my first table probably yeah probably I just make one more entry here before my second data now let's see if it's able to capture this so I first put this one and then I run it so I can first check in the database if so so it gives my second data and now I go back to my app and if I click on this SQL button it gets my school data so if you can see is it's directly connected to the database and each time if there's any change in the database that we facilitated here if you make it any further entry it will need entry over here yeah so this is a very simple or easy query I have shown over here how to implement but of course you can enhance it and include other reference that is what you want like insert even you could create a table stacking from these commands over here but I just wanted to keep it short and simple over here so that's all I hope you find this video useful if you have any questions or suggestions please put in the comment section below and if you like this video then please subscribe to my channel and please like this video thanks a lot for watching this video and
Info
Channel: Programmer World
Views: 72,968
Rating: undefined out of 5
Keywords: db, data, base, sequential, application, smrtphone, oracle, sybase, ysq, mysql, informix, ibm, smartphone, query, language, update, delete, fetch, select, sql, android, os, commercial, business, internet, newteok, protocol, ip, tcp, port, number, user, nae, name, login, ud, id, password, login credential, credentials, use, java, sun, access, ms, micro, soft, microsoft, server, client, call, interrupt, address, exchange
Id: MnmEXqfV5BU
Channel Id: undefined
Length: 27min 28sec (1648 seconds)
Published: Sat Apr 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.