Advanced Java Programming
Advanced Java Programming
Advanced Java Programming :-
Introduction to advance java
As most of us already know that if we want to make normal applications it can be easily built using core Java concepts. But, when it we need to develop web applications, advanced Java fundamentals, like JSP, Servlets, JDBC etc. needed, so to add capabilities and features of the application advance java is essential for developers. Through the motive of this blog is to explain about Advanced Java, I will be giving you a complete insight into the fundamental concepts of Advance Java.
Figure - 1.2
Figure - 1.3
I hope you understood by the slide why Advanced Java is essential. For your better understanding, I have divided this article into two sections. Each of these section deals with one of the most important concepts of advanced Java:
- JDBC (Java DataBase Connectivity
- Java Servlets
So, now let’s begin our discussion and understand the concept of Java Database Connectivity, a helpful tool to interact with the database.
Figure - 1.4
Common JDBC Components
The JDBC API provides the following interfaces and classes −
- DriverManager is used to manage a list of database drivers. The first driver that recognizes a certain subprotocol under JDBC will be used to establish a database Connection.
- Driver is an interface that handles the communications with the database server. It also abstracts the details associated with working with Driver objects.
- Connection is an interface that consists all the methods required to connect to a database. The connection object represents communication context, i.e., all communication with the database is through connection object only.
Figure - 1.5
Figure - 1.6
Figure - 1.7
Figure - 1.8
Figure - 1.9
Figure - 2.0
Figure - 2.1
Now let’s move on to the next topic and look at the steps required to create a JDBC Application.
Figure - 2.2
Figure - 2.3
Figure - 2.4
Figure - 2.5
Figure - 2.6
Figure - 2.7
Figure - 2.8
Figure - 2.9
Code Example is given below
package com.jdbc.eamples;
import java.sql.*;
public class MyDatabase {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/databasename";
static final String USER = "your root username";
static final String PASS = "your root password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,"USER",PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
//first inserting data in table
String sql= "INSERT INTO emp VALUES (101, 'Taylor', 'Swift', 30)";
stmt.executeUpdate(sql);
sql= "INSERT INTO emp VALUES(102, 'Linkin', 'Park', 28)";
stmt.executeUpdate(sql);
System.out.println("Inserted records into the table...");
System.out.println("Creating statement...");
System.out.println("database connected");
//then fetching data from table
sql = "SELECT idemp, first, last, age FROM emp";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("idemp");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
} catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing can be done
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}
Introduction to Servlets
A servlet is a java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.
Servlet can be described as follows:
1. Servlet is a technology which is used to create a web application.
- It is an API that provides many interfaces and classes including documentation.
- Servlet is an interface that must be implemented for creating any Servlet.
- It is also a class that extends the capabilities of the servers and responds to the incoming requests. It can respond to any requests.
Client Server Architecture
Complete Servlet Life Cycle
Create a dynamic Web Project using Servlet
Code example is given below
There are one Servlet in this project and one html page. In WEB-INF folder there is one web.xml file is present.
In com.reshu package there is a Servlet file SumServlet present the work of this servlet is to take values of two numbers from request object given by server and then add those two numbers and show the addition to the page in the form of response to the html page
package com.reshu;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SumServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException{
int i = Integer.parseInt(req.getParameter("num1"));
int j = Integer.parseInt(req.getParameter("num2"));
int k = i+j;
PrintWriter out = res.getWriter();
out.println("result is " + k);
}
}
Here below is the code of html page by which user send request to the SumServlet
the code given below is for web.xml file for servlet-mapping. In
web.xml file their is a mapping for Servlet and html page by which when user submit the page request call the servlet according to mapping in this file. In this case SumServlet.
Web.xml
I hope you have enjoy my blog and please visit my youtube Channel for more videos in hindi.
Learn With Reshu
Thanks for Reading Please subscribe for more interesting content everyday.....
Recommended Posts:
Thanks for sharing a really needed post that i am looking for , if anyone looking for best java institute in delhi so join with us visit our website : https://www.htsindia.com/java-training-courses or contact us : +91-9311002620
ReplyDelete