Ajava Unit III
Ajava Unit III
Ajava Unit III
JDBC PRINCIPLES
JDBC stands for Java Database Connectivity, which is a standard Java API
for database-independent connectivity between the Java programming
language and a wide range of databases.
The JDBC library includes APIs for each of the tasks mentioned
Java Applications
Java Applets
Java Servlets
Java ServerPages (JSPs)
Enterprise JavaBeans (EJBs).
All of these different executables are able to use a JDBC driver to access a
database, and take advantage of the stored data.
JDBC Architecture
The JDBC API supports both two-tier and three-tier processing models for
database access but in general, JDBC Architecture consists of two layers −
The JDBC driver manager ensures that the correct driver is used to access
each data source. The driver manager is capable of supporting multiple
concurrent drivers connected to multiple heterogeneous databases.
DATABASE ACCESS
Where oracle is the database used, thin is the driver used, @localhost is
the IP Address where a database is stored, 1521 is the port number and xe
is the service provider. All 3 parameters above are of String type and are to
be declared by the programmer before calling the function. Use of this can
be referred to form the final code.
Step 4: Create a statement
Once a connection is established you can interact with the database. The
JDBCStatement, CallableStatement, and PreparedStatement interfaces
define the methods that enable you to send SQL commands and receive
data from your database.
Use of JDBC Statement is as follows:
Statement st = con.createStatement();
*7. close
*/
import java.io.*;
import java.sql.*;
class GFG {
String url
String query
Class.forName(
System.out.println(
Statement st = con.createStatement();
ResultSet rs
= st.executeQuery(query); // Execute query
rs.next();
String name
System.out.println("Connection Closed....");
Output:
Example:
Java
import java.sql.*;
import java.util.*;
// Main class
class Main {
System.out.println("enter name");
System.out.println("enter class");
try {
// Registering drivers
DriverManager.registerDriver(
new oracle.jdbc.OracleDriver());
pass);
// Creating a statement
Statement st = con.createStatement();
// Executing query
int m = st.executeUpdate(sql);
if (m == 1)
System.out.println(
else
System.out.println("insertion failed");
con.close();
System.err.println(ex);
}
Output after importing data in the database:
Interface Interaction
Method Summary
void clearWarnings()
Clears all the warning reported by this Interaction
instance.
void close()
Closes the current Interaction and release all the
resources held for this instance by the resource adapter.
Record execute(InteractionSpec ispec, Record input)
Executes an interaction represented by the
InteractionSpec.
boolean execute(InteractionSpec ispec, Record input, Record out
put)
Executes an interaction represented by the
InteractionSpec.
Connection getConnection()
Gets the Connection associated with the
Interaction.
ResourceWarnin getWarnings()
g Gets the first ResourceWarning from the chain of
warnings associated with this Interaction instance.
close
void close()
throws ResourceException
Closes the current Interaction and release all the resources held for
this instance by the resource adapter. The close of an Interaction
instance does not close the associated Connection instance. It is
recommended that Interaction instances be closed explicitly to free
any held resources.
Throws:
getConnection
Connection getConnection()
Gets the Connection associated with the Interaction.
Returns:
Parameters:
Returns:
true if execution of the EIS function has been successful and output
Record has been updated; false otherwise
Throws:
execute
Record execute(InteractionSpec ispec,
Record input)
throws ResourceException
Executes an interaction represented by the InteractionSpec. This form
of invocation takes an input Record and returns an output Record if
the execution of the Interaction has been successfull.
Parameters:
Returns:
Throws:
getWarnings
ResourceWarning getWarnings()
throws ResourceException
Gets the first ResourceWarning from the chain of warnings associated
with this Interaction instance.
Returns:
Throws:
clearWarnings
void clearWarnings()
throws ResourceException
Clears all the warning reported by this Interaction instance. After a
call to this method, the method getWarnings will return null until a
new warning is reported for this Interaction.
Throws:
Web Application
A web application is computer software that can be accessed using any web
browser. Usually, the frontend of a web application is created using the
scripting languages such as HTML, CSS, and JavaScript, supported by
almost all web browsers. In contrast, the backend is created by any of the
programming languages such as Java, Python, Php, etc., and databases.
Unlike the mobile application, there is no specific tool for developing web
applications; we can use any of the supported IDE for developing the web
application.
The web server is a process that handles the client's request and responds.
It processes the request made by the client by using the related protocols.
The main function of the webserver is to store the request and respond to
them with web pages. It is a medium between client and server. For
example, Apache is a leading webserver.
URL
URL stands for Universal Resource Locator used to locate the server and
resource. It is an address of a web page. Every web page on a project must
have a unique name.
http://localhost:8080/SimpleWebApplication/
Where,
http or https: It is the starting point of the URL that specifies the protocol
to be used for communication.
Localhost: The localhost is the address of the server. When we run our
application locally, it is called localhost; if we deployed our project over the
web, then it is accessed by using the domain name like "javatpoint.com".
The domain name maps the server to IP addresses.
8080: This is the port number for the local server; it is optional and may
differ in different machines. If we do not manually type the port number in
the URL, then by default, the request goes to the default port of the protocol.
Usually, the port no between 0 to 1023 are reserved for some well-known
services such as HTTP, HTTPS, FTP, etc.
What is Servlet
A Servlet is a Java program that runs within a web server; it receives the
requests and responds to them using related protocols (Usually HTTP). The
Servlets are capable enough to respond to any type of request; they are
commonly used to make the application functional.
We can create a static website using only HTML and CSS, but when it comes
to dynamic, we need a server-side programming language. For these
applications, Java provides Servlet technology, which contains HTTP-specific
servlet classes.
TestServlet.java:
1. import java.io.IOException;
2. import javax.servlet.ServletException;
3. import javax.servlet.annotation.WebServlet;
4. import javax.servlet.http.HttpServlet;
5. import javax.servlet.http.HttpServletRequest;
6. import javax.servlet.http.HttpServletResponse;
7.
8. /**
9. * Servlet implementation class TestServlet
10. */
11. @WebServlet("/TestServlet")
12. public class TestServlet extends HttpServlet {
13. private static final long serialVersionUID = 1L;
14.
15. /**
16. * @see HttpServlet#HttpServlet()
17. */
18. public TestServlet() {
19. super();
20. // TODO Auto-generated constructor stub
21. }
22.
23. /**
24. * @see HttpServlet#doGet(HttpServletRequest request, HttpServlet
Response response)
25. */
26. protected void doGet(HttpServletRequest request, HttpServletResp
onse response) throws ServletException, IOException {
27. // TODO Auto-generated method stub
28. response.getWriter().append("Served at: ").append(request.getCo
ntextPath());
29. }
30.
31. /**
32. * @see HttpServlet#doPost(HttpServletRequest request, HttpServlet
Response response)
33. */
34. protected void doPost(HttpServletRequest request, HttpServletRes
ponse response) throws ServletException, IOException {
35. // TODO Auto-generated method stub
36. doGet(request, response);
37. }
38.
39. }