An ArrayList is an object that is used to store a group of the other objects and allows us to do manipulation on these objects one by one. Java ArrayList class basically uses a dynamic array for storing the elements.
The basic features of Java ArrayList class are given below:
int len = ar.size();
Document : data
Created on : Jan 18, 2017, 5:40:40 PM
Author : Bsb
--%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
The basic features of Java ArrayList class are given below:
- Java ArrayList class can contain duplicate elements to store.
- Java ArrayList class maintains insertion order.
- In Java ArrayList class, the manipulation is slow because there is lot of shifting to be occurred if any element is removed from the array list from any index.Suppose there is an arraylist of 4 elements having indices 0,1,2,3. If we want to delete the element placed at the index no, 1, then there will be shifting of elements from down as all the remaining elements goes up covering all the indices, so this is the reason the manipulation in arraylist is slow.
- Java ArrayList class is non synchronized.It mean that there can be case when there can be multiple threads that can work together to modify the list at same time.
1. addAll
2. clear
3. isEmpty
4. get
5. remove
ArrayList is a class that can be created using new keyword.The basic syntax to create a new and empty arraylist can be defined as:
ArrayList ar=new ArrayList();
To add values to the newly created arraylist we have to use add() method. add() method helps in adding values that can be strings. It is defined as:
ar.add("HELLO");
ar.add("BALWANT");
ar.add("SINGH");
So, it is clear from the above syntax that the three values are now added to your arraylist named as ar. You can name it anything you want. You can add any number of elements to your arraylist you want. Also if you want to check or get the length of your arraylist you can get using size() method describes as given below;
Example: Working of ArrayList
First you have to make a page in html like this. In this page you can create one search bar in which users search for a particular data or record. And there is one submit button to submit your record to the jsp page. The submitted data or record is then check on the jsp page that the particular record is there in the database or not.
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="data.jsp">
<center><input type="text" name="n1">
<button>SUBMIT</button>
</center>
</form>
</body>
</html>
First you have to make a page in html like this. In this page you can create one search bar in which users search for a particular data or record. And there is one submit button to submit your record to the jsp page. The submitted data or record is then check on the jsp page that the particular record is there in the database or not.
Index.html page:
<!DOCTYPE html><!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="data.jsp">
<center><input type="text" name="n1">
<button>SUBMIT</button>
</center>
</form>
</body>
</html>
Data.jsp page:
<%--Document : data
Created on : Jan 18, 2017, 5:40:40 PM
Author : Bsb
--%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>INFORMATION SHOWN BELOW:</h1>
<%
ArrayList<String> ar=new ArrayList<String>();
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/database","root","");
Statement s=con.createStatement();
String str=request.getParameter("n1");
ResultSet rs=s.executeQuery("select * from student1 where Name='"+str+"'");
while(rs.next())
{
String s1=rs.getString(1);
String s2=rs.getString(2);
String s3=rs.getString(3);
String s4=rs.getString(4);
//out.println(s1+"</br>"+s2+"</br>"+s3+"</br>"+s4);
ar.add(s1);
ar.add(s2);
ar.add(s3);
ar.add(s4);
}
for(String s9:ar)
{
out.println(s9);
}
%>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>INFORMATION SHOWN BELOW:</h1>
<%
ArrayList<String> ar=new ArrayList<String>();
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/database","root","");
Statement s=con.createStatement();
String str=request.getParameter("n1");
ResultSet rs=s.executeQuery("select * from student1 where Name='"+str+"'");
while(rs.next())
{
String s1=rs.getString(1);
String s2=rs.getString(2);
String s3=rs.getString(3);
String s4=rs.getString(4);
//out.println(s1+"</br>"+s2+"</br>"+s3+"</br>"+s4);
ar.add(s1);
ar.add(s2);
ar.add(s3);
ar.add(s4);
}
for(String s9:ar)
{
out.println(s9);
}
%>
</body>
</html>
In this jsp page, we have used ArrayList to create ArrayList, then there is jdbc connection for the database. The value which we pass through the html page can be easily get using request.getParameter() method, then that value is passed in the sql query to fetch the records from the database. If there is value it can print that on the browser otherwise not. The fetched records are then store in the strings type variables and then these variables are added to the arraylist using add method as i said above. After adding all the elements to the arraylist, we need to fetch or print them. To retrieve them we have to use enhanced for loop for iterating the values contained in the arraylist. After iterating all the values get printed on the web browser using out method.
The output or screenshot is given below:
No comments:
Post a Comment