0% found this document useful (0 votes)
19 views

Java Sec-D Database Connectivity

Java notes

Uploaded by

ritick.singh08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
19 views

Java Sec-D Database Connectivity

Java notes

Uploaded by

ritick.singh08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 14
JAVA DATABASE CONNECTIVITY WITH Noer| 13.1 INTRODUCTION 132 JDBC 133 JAVASQL PACKAGE 134 WORKING WITH JDBC 13.4.1 CREATING A CONNECTION WITH THE DATABASE 13.4.2 INSERTING A RECORDING DATABASE 13.43 UPDATING AN EXISTING RECORD IN DATABASE 13.44 ACCESSING RECORDS FROM DATABASE. 13.4.5 DELETE A RECORD FROM DATABASE REVIEW QUESTIONS 131 Bz 133 15 335 136 188 339 1310 1313 JAVA. DATABAS ASE CONNECTIVITY WITH | MY sql DODO Ee os00000000 131 INTRODUCTION 12 ypBc 183 JAVASQL PACKAGE 134 WORKING WITH JDBC 18.44 Creating a Connection withthe Datab 13.42 Inserting a Recordin Database 1945 Updating an Exiting Record in Database 134 Accessing Records from Database 1245 Delete a Record from Database REVIEW QUESTIONS ESE wrropuction ton ‘Adatabase isa collection of related information that is organized in such» way #8 ‘managed, and updated easily. Database touches all aspects of ou lives Some ofthe applications of database are as follows: Banking: all transactions * Airlines: reservations, schedules * Univenitis: registration, grades Jora Database Comeesvty Wh ain f lmtp taon soccer tote panemes nani | «paloma eocsug trmn ~ Npeeiiitadtiatsaean © lon el ead ed ls Den sas seme of en Qt mf ae Tiscali fw hues fe at Sted ns eh on has phe ae ers ac Saeco ald cua wef oman ld Te flown on =n ee rates : tone | Recom | tothe [aaa | a a ie fr | a a Eo] co a a a seam [ ea | — oon [actor rome rest nme ie tnt dope we et kang how to cen spas or idan dh Sn as a i 0 Meat ure po Daa " Thee are folowing steps to be performed or connecting, Java application {) Impor a package named javasal in you avs appl ne (@) Lond andreister the JDBC driver Gi) Define the connection URL (x) Establish the connection withthe database (0) Createan objet of type Statement (+) ecute an SQL query using the methods of Statement objet (vi). Process the result (si) Close the connection | SUMMER 100 iad for Fava Database Connectivity. OAC oa sadn ve | Application Programming Interface (API) that allows the Programmers to access and manipulate _tashise management system through Java code, MER] vava.sou packace singmptpakage provides various iteraces and ass tht ae wad ty he JDBC AM os Meant #94 Processing data stored in a data source using the Java code, Some ofthe importa {and interface ofthis package areas follows Retister the Driver Class J method i we Ai ftName method of Cass clas is used to register the drive clas. Thi Samia oad te driver clas —— Tagg Ye to lad and register the diver to esublsh datakase cones NS code is used in Java program: as ¥ ARORA DriverManager Class ae Connection Interface ae, ranection witha database, This can be done as follows jocalhost:3806 database" "username on=DriverManager getCannection(dbemysq: can communicate with the database. This can ement interface. Ths interface enables you to pert using the methods and properties of St nls to the database. You can perform insertion, deletion, updating, and searchi ResultSet Interface vides access to a table of data which is generated by executing a Statemen ct per Statement, It retrieves the rows from table in a sequc *, pointing to its current row of data, The ‘next()’ method is used to 1 ursor tothe next row of data. For example ezeating a statement ‘Statement stmt = con cteateSatement() Hesecoting a statement o retrieve the records from a table eoulSt = tt executsQuery eect alNo, studertName fom tabStuden's; Mint the es while. next) System out prints geting) + *), accessing by column index ‘tem out prints geShorstuden’Nam), accessing by col EET WORKING Wire spRc 124.1 CREATING A CONNECTION WitH THe pag ‘ © DATABASE, 2 Now use this database in MySql =: al for further man 5 command in MySql . Step: Now create a table named as tabStudents” in your database, containing the els No, studentName, and marks by executing the following command in Ni fe Ciprogram Fees ySQVASO Sener SDD mma 4 (Tete te same database and able for est ofthe pr Step: Now write the to establish MySql database 'studentDB ke Sepia sa farCemesdatinae Pb setic voi Juve Database Connectivity Wid i : Systm utp Problem wile extaishing the connection: Output of the Program 7 13.4.2 INSERTING A RECORD IN DATABASE sep |Crents ea ae ear ving Mya Sees and we ave cesta dtu named asstadem DB" by executing the flowin inna eo 2 Now use this database in MySql for furt Seri ——calhahaedoen Sal for further manipulations by executing th Lee sa paste Connect » N a table named as “tabsi sve MdentName, and marks tudents" in your daa student a by executing be sin a lds oN, step: Now write the following code in your java application wo inant beens "ecord in "ahStudenty Inport vasa ls InsetRecord public state oid main(String ang) I Connection con = nl; Seatement tt = ll ( ‘Moad and Register the Driver Cas foeame( com mysql jdbe Drive’); Ss ‘establishing the connection with database ‘con=DriverManager getConnection("jdbc-mysqi//localhost3306/studestDB rot Wereating a statement stmt=con createStatement); Mexecuting a statement to insert a record into a table SimtexecuteUpdate( ‘insert into tabStudents values(/ Avinash’ 450) ‘WClosing the connection con close), _ — eas jews 13.4.3 UPDATING AN EXISTING RECORD IN DATABASE agement System. Here, we are using MySql Server 50 Step-I: Create a database in a Database M ave ceated a database named asstudentDB Step-2: Now use this database in MySql fr further manipulations, steps “ e named as "abStudents" in your database, containing three fields rolNo, studentName, and marks. - ’ te cme goes rename ys Step-4: Insert some records in tabStudents table such as: Step-5: Now write the following code in your java application to update or tabStudents’ able : oo import oval ‘dass UpdateRecord 4s t Ste pes ee, i ‘Connection con null; = aac. 5 i Ste eq) fstblishing the connection with daa creating 2 statement Closing the connection System out print "Recond Upuated Successful cateh(Exceptione) ‘Systemoutprintn(Thee isa problem: “4 Output of the Program i | BB C\Windows\system32\cmdexe Record Updated Successfully Press any key to continue « 13.4.4 ACCESSING RECORDS FROM DATABASE Re. system. Here, we are sing MySql SemerS2 Step-t: Create a database in a Database Management S and we have created a database named as “studentDB¥ S12 Now use this database in MySql fr further manipulations SHEPS: Now create a table named as “tabStudents’ in your database, containing studentName, and marks. (ote: Step-1 to Step-3 are sam aS a te the following code in your java application t bli tate void mainString arg) ResultSet r= nl (MLoad and Register the Driver lass. for Name com mysql bc Dever" ‘establishing the connection with database ‘consDriverManager getConnection(jdbemysq:/ localhost 3906 /studentDBY00t"s) Hereating a statement tint=con creteStatement) ‘Texecuting a statement to acess records from tabStudes rssstmtexecuteQuery( select from laStudents) Display Records System out printing”———- Roll No \t Student Name \t Marks") a System out printin¢ Systemout printing hile(rs.nextO) i int mers getint"olINo’) String sName=rsgottring(studentName"); lint m= rs getin'marks") Systemoutprint(\" +m); System.outprint(\t”+ sName}: Sr oe 549 iol ro i re ren ri eer ra) foment’ Marks 134.8 DELETE A RECORD FROM DATABASE Step: Crate a database in a Database Management Syste and we have created a database named as “stude Step Now use this database in MySql for further manipulations Step Now create a table named as "tabStudents" in your database, conta studentName, and marks DB thre feds rollNo, (Note Step-1 to Step Sep Insert some records in tabStudent footer eer cies Cetin Peery coco Establishing the connection with database DriverManager,getConnection de mysql lecalhost’306/studentD8" ro ‘xecuting 4 satement o access records from tabStudents lat delete from tba where rllNe-2" {csecutng a statement to access remaining records from tbStudents ‘-stmt-executeQueryselet* frm tite) Srstem out print Records after deleting a record of Roll No: Display Records Roll No \t Student Name \t Mark”) = % System out printing wher. nent) int mars gettntrolNo*), String sName=rs getStringstudentName’) intm » rs getint’ marks", System out print(\t +m) Systemoutprint?\t" + sName) Systemoutprintn(” \t "+ my, ‘Closing the connection a joo of the Program — output Po after deleting «rect Se Paat you can access and manipulate databases using [DBC ee ee = = = What is database? Wha are the applications of databases? Whats DBC? What is the use of java.sql package? Explain 1 2 5 ‘ 5. Whats the use of various classes and interfaces provided by java sql package? 6 Writ the steps to establish a connection with the database using JDBC 7, Wisite the steps to insert a recordin database using JDBC © White the steps to update an existing record in database using JDBC iq ng JDBC steps to delete apartcular record from database using JDBC Write the steps to access the records from datal 10. Write the gag sisase Connectivity WH

You might also like