Q)What is Java? 1)Object Oriented Programming Language 2)Platform 3)Technology Q)Where is Java mostly used? =>In web application development. For eg. online banking application, online shopping application, online reservation application etc. Q)Which is the basis for a company to provide services to customers? =>data Q)Which features should data possess in a web application? 1)persistence(permanence) 2)Security 3)easy accessibility Note:- If we store data in a database we get all these features for data. Q)What is the role of Java in a web application? 1)communicating with the database and performing CRUD operations 2)processing of data 3)making processed data available to the front-end Q)What is the obstacle for Java-database communication? =>To perform any task, a Java program can make only a method call. But, DBMS can't understand Java method calls.It understands only SQL. =>Java compiler doesn't accept SQL directly. =>Java and database are hetrogenious to each other. Q)How Java-database communication? =>by using JDBC Q)What is JDBC? =>JDBC is a Java based data access technology from Sun Microsystems. ************************************************************** Q)What is the architecture of JDBC? =>JDBC follows client-server architecture =>A software/program that requests for resources is a client. =>A software that provides requested resources to clients is a server. Q)What is a JDBC client? =>Any Java program that uses JDBC technology to communicate with the database is known as a JDBC client. Q)What are the JDBC client side requirements to communicate with the database? 1)JDBC API 2)JDBC Driver Q)What is JDBC API? =>A collection of Library methods( that belong to the library classes and library interfaces )of java.sql package that is used to perform CRUD operations with the database is nothing but JDBC API. Q)What is a JDBC Driver? =>JDBC driver is a translation software written in Java according to JDBC specification. =>JDBC driver performs the following duties to enable Java-database communication. 1)Receives JDBC API(method call) call from the JDBC program, translates into SQL call and passes on to the DBMS. 2)Receiving SQL (type) response from the DBMS, translating the same into Java format and handing over to the JDBC client. *********************************************** Q)Explain about JDBC Programming Model. Requesting for connection ----------------------------- =>Java program being a client should get connected to the database server. =>Java program makes use of DriverManager OR DataSource =>By calling getConnection() method on DriverManager or DataSource, JDBC client requests for database connection. Connection connection=DriverManager.getConnection(...); Note:- Object Oriented representation of JDBC client's session with the database server is nothing but Connection object. =>As long as this session is active(i.e. this connection is open) so long, Java program can perform CRUD operations with the database. Creating the Statement object ---------------------------------- =>By calling createStatement() method on the connection object,Statement object is created. Statement statement=connection.createStatement(); =>Statement object is the designatory object to submit an SQL statement from a Java program to the DBMS. Submitting the SQL statement ----------------------------------- =>Statement object has 2 methods to submit an SQL statement to the DBMS. 1)executeUpdate(String dml) 2)executeQuery(String drl) =>executeUpdate() method is used to submit INSERT,UPDATE and DELETE SQL statements to the DBMS. =>executeQuery() method is used to sbmit SELECT statement to the DBMS. closing the Statement ------------------------- =>By calling close() method on the Statement object, Statement is closed. statement.close(); =>To release JDBC resources of the JDBC client maintained by the database server, Statement should be closed in JDBC client. closing the Connection -------------------------- =>By calling close() method on the Connection object, connection is closed in the JDBC Client. connection.close(); =>To release networking resources of the JDBC client maintained by the database server, connection should be closed in JDBC client. *********************************************************** Q)Write a Java program to connect to MySQL DBMS(Database Server). //ConnectionApplication.java import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; class ConnectionApplication { public static void main(String[] args) throws SQLException { Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306","root","ashokit"); System.out.println("JDBC client connected to MySQL server"); connection.close(); System.out.println("After performing database operations, connection is closed"); } } Q)Write a Java program to store an account's data(accno,name,balance) into the database. Note:- Before writing and executing this program, the following things should have been done in MySQL DBMS. Step 1:- Login to MySQL DBMS. Step 2:- create a database mysql>CREATE DATABASE hdfcdb; Step 3:- enter into the database. mysql>USE hdfcdb; STEP 4:- create the table mysql>CREATE TABLE ACCOUNT(ACCNO INT(11),NAME VARCHAR(20),BALANCE DOUBLE(10,2)); //StoreAccount.java import java.sql.*;//making JDBC API available class StoreAccount { public static void main(String[] args) throws SQLException { Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/hdfcdb","root","ashokit"); Statement statement=connection.createStatement(); int re=statement.executeUpdate("INSERT INTO ACCOUNT VALUES(10001,'Rama',50000)"); System.out.println(re+" account stored into the database successfully"); statement.close(); connection.close(); } } Q)Write a Java program to increase the balance of all the accounts by Rs.1000. //DepositApplication.java //StoreAccount.java import java.sql.*;//making JDBC API available class DepositApplication { public static void main(String[] args) throws SQLException { Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/hdfcdb","root","ashokit"); Statement statement=connection.createStatement(); int re=statement.executeUpdate("UPDATE ACCOUNT SET BALANCE=BALANCE+1000"); System.out.println(re+" accounts updated"); statement.close(); connection.close(); } }