Practical :-3 and 4 ADBMS What is Java Server Pages? Java Server ...

4 downloads 92 Views 23KB Size Report
Practical :-3 and 4. ADBMS. Problem Statement:-Develop Java Application to manitain any single table of your system.The application should have ...
Practical :-3 and 4

ADBMS

Problem Statement:-Develop Java Application to manitain any single table of your system.The application should have buttons(links)to 1.Display(select) records. 2.Insert the records. 3.Update the records. 4.delete the record. 5.Exit.

What is Java Server Pages? Java Server Pages or JSP for short is Sun's solution for developing dynamic web sites. JSP provide excellent server side scripting support for creating database driven web applications. JSP enable the developers to directly insert java code into jsp file, this makes the development process very simple and its maintenance also becomes very easy. JSP pages are efficient, it loads into the web servers memory on receiving the request very first time and the subsequent calls are served within a very short period of time. In today's environment most web sites servers dynamic pages based on user request. Database is very convenient way to store the data of users and other things. JDBC provide excellent database connectivity in heterogeneous database environment. Using JSP and JDBC its very easy to develop database driven web application. Java is known for its characteristic of "write once, run anywhere." JSP pages are platform independent. Your port your .jsp pages to any platform. What is Java DataBase Connectivity? JDBC is Java application programming interface that allows the Java programmers to access database management system from Java code. It was developed by JavaSoft, a subsidiary of Sun Microsystems. Definition Java Database Connectivity in short called as JDBC. It is a java API which enables the java programs to execute SQL statements. It is an application programming interface that defines how a java programmer can access the database in tabular format from Java code using a set of standard interfaces and classes written in the Java programming language. JDBC has been developed under the Java Community Process that allows multiple implementations to exist and be used by the

same application. JDBC provides methods for querying and updating the data in Relational Database Management system such as SQL, Oracle etc. The Java application programming interface provides a mechanism for dynamically loading the correct Java packages and drivers and registering them with the JDBC Driver Manager that is used as a connection factory for creating JDBC connections which supports creating and executing statements such as SQL INSERT, UPDATE and DELETE. Driver Manager is the backbone of the jdbc architecture. Generally all Relational Database Management System supports SQL and we all know that Java is platform independent, so JDBC makes it possible to write a single database application that can run on different platforms and interact with different Database Management Systems. Java Database Connectivity is similar to Open Database Connectivity (ODBC) which is used for accessing and managing database, but the difference is that JDBC is designed specifically for Java programs, whereas ODBC is not depended upon any language. In short JDBC helps the programmers to write java applications that manage these three programming activities: 1. It helps us to connect to a data source, like a database. 2. It helps us in sending queries and updating statements to the database and 3. Retrieving and processing the results received from the database in terms of answering to your query. What is JDBc driver? The JDBC API defines the Java interfaces and classes that programmers use to connect to databases and send queries. A JDBC driver implements these interfaces and classes for a particular DBMS vendor . A Java program that uses the JDBC API loads the specified driver for a particular DBMS before it actually connects to a database. The JDBC DriverManager class then sends all JDBC API calls to the loaded driver . The four types of JDBC drivers are: •

JDBC-ODBC bridge plus ODBC driver, also called Type 1. Translates JDBC API calls into Microsoft Open Database Connectivity (ODBC) calls that are then passed to the ODBC driver. The ODBC binary code must be loaded on every client computer that uses this type of driver.



Native-API, partly Java driver, also called Type 2. Converts JDBC API calls into DBMS-specific client API calls. Like the bridge driver, this type of driver requires that some binary code be loaded on each client computer.



JDBC-Net, pure Java driver, also called Type 3.

Sends JDBC API calls to a middle-tier net server that translates the calls into the DBMSspecific network protocol. The translated calls are then sent to a particular DBMS. •

Native-protocol, pure Java driver, also called Type 4. Converts JDBC API calls directly into the DBMS-specific network protocol without a middle tier . This allows the client applications to connect directly to the database server .

What is Apache-T omcat? Apache Tomcat (or Jakarta Tomcat or simply Tomcat) is an open source servlet container developed by the Apache Software Foundation (ASF). Tomcat implements the Java Servlet and the JavaServer Pages (JSP) specifications from Sun Microsystems, and provides a "pure Java" HTTP web server environment for Java code to run.

Tomcat should not be confused with the Apache web server, which is a C implementation of an HTTP web server; these two web servers are not bundled together. Apache Tomcat includes tools for configuration and management, but can also be configured by editing XML configuration files.

Catalina Catalina is Tomcat's servlet container . Catalina implements Sun Microsystems' specifications for servlet and JavaServer Pages (JSP). The architect for Catalina was Craig McClanahan. What is Java Prepared Statements? The PreparedStatement interface inherits from Statement and differs from it in two ways: 1. Instances of PreparedStatement contain an SQL statement that has already been compiled. This is what makes a statement "prepared." 2. The SQL statement contained in a PreparedStatement object may have one or more IN parameters. An IN parameter is a parameter whose value is not specified when the SQL statement is created. Instead, the statement has a question mark ("?") as a placeholder for each IN parameter. The "?" is also known as a parameter marker. An application must set a value for each question mark in a prepared statement before executing the prepared statement. Because PreparedStatement objects are precompiled, their execution can be faster than that of Statement objects. Consequently, an SQL statement that is executed many times is often created as a PreparedStatement object to increase efficiency. Being a subclass of Statement, PreparedStatement inherits all the functionality of Statement. In addition, it adds a set of methods that are needed for setting the values to be sent to

the database in place of the placeholders for IN parameters. Also, the three methods execute, executeQuery, and executeUpdate are modified so that they take no argument. The Statement forms of these methods (the forms that take an SQL statement parameter) should never be used with a PreparedStatement object

Passing IN Parameters Before a PreparedStatement object is executed, the value of each ? parameter must be set. This is done by calling the setter method (setInt, setString, and so on), that is appropriate for the parameter type. For example, if the parameter is of typelong in the Java programming language, the method to use is setLong. The first argument to the setter methods is the ordinal position of the parameter to be set, with numbering starting at 1. The second argument is the value to which the parameter is to be set. For example, the following code sets the first parameter to 123456789 and the second parameter to 100000000: pstmt.setLong(1, 123456789); pstmt.setLong(2, 100000000);

Once a parameter value has been set for

What is JDBC Prepared Statement Update The Update Statement in SQL updates the existing records in a table. In JDBC the Prepared Statement Update is used to update the SQL statement , using where clause, that specify which records is updated.

The example include a class Jdbc Prepared Statement, this class include a main method ( ), Inside the main method we have a list of steps Importing a package java.sql – This package provides you a network interface , that enables you to communicate between front end application-back end. Loading a driver by calling a class.forName ( ),that accept a driver class as argument. DriverManager.get Connection ( ) – The Driver Manager call a getConnection ( ) method, return you a connection object, built a connection between url and database. The connection object call a prepareStatement ( ),that is used to update the table Teachers using where clause, specify the name of record to be updated at runtime. The set String XXX return you a set value in the place of question mark holder . TheexecuteUpdate() return you the record set from a updated table in the database.

Finally the println print the updated table from the database in output. The catch block caught the exception in try block.

Deleting Records using the Prepared Statement To delete the records from the database table by using thePreparedStatement interface of the java.sql package. We can do it very easily by using the simple Sql query. The given example provides the facility for deleting the records from the database table.

Source code print outs

Ouput Printouts