DELETING RECORDS

Deleting records from table servlet example

The Servlet example code below demonstrates on how to delete records from database table. Consider a situation where we have entered some wrong data and in later situation it starts giving problem to the organization. Rather than go through with that data its better to delete that data. We can do it very easily through our program, what we need is a simple query.
In this example we are going to exactly the same as we said above. To make this possible we need to make a class named ServletDeletingRowsFromTable, the name of the program should be such, if in future there is any need to make any change in the program, you can easily understand in which program you have to make a change. As we know that in Servlet the main logic of the program is written inside the service method and in turn the service method calls the doGet() method. Now inside the doGet() method use the getWriter() method of the response object and its returns the PrintWriter object, which helps us to write on the browser. To delete the unwanted data from our table there is a need for the connection between the database and the java program. After the establishment of the connection with the database, fire a query for deleting the unwanted row in the database table. This query will be fired inside the prepareStatement() method of the Connection object and returns the PreparedStatement object. If the rows has been deleted from the database table then print the message “row is deleted” otherwise “no rows has been deleted”.
The code of the program is given below:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class ServletDeletingRowsFromTable extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException{
int rows;
response.setContentType(“text/html”);
PrintWriter pw = response.getWriter();
String connectionURL = “jdbc:mysql://localhost/zulfiqar”;
Connection connection;
try{
Class.forName(“org.gjt.mm.mysql.Driver”);
connection = DriverManager.getConnection(connectionURL, “root”, “admin”);
PreparedStatement pst = connection.prepareStatement
(“delete from emp_sal where EmpName = ‘vinod'”);
int i = pst.executeUpdate();
if (i==0){
pw.println(“Row has been deleted”);
}
else{
pw.println(“No rows has been deleted”);
}
}
catch(Exception e){
pw.println(“The exception is ” + e);
}
}
}
web.xml file for this program:

Zulfiqar
ServletDeletingRowsFromTable

Zulfiqar
/ServletDeletingRowsFromTable

The Java servlet code shown above illustrates on how to delete records from database table. The example code is useful to the beginner Java programmer who is searching for the example servlet code to delete records from table.

Deleting records from table servlet example

The Servlet example code below demonstrates on how to delete records from database table. Consider a situation where we have entered some wrong data and in later situation it starts giving problem to the organization. Rather than go through with that data its better to delete that data. We can do it very easily through our program, what we need is a simple query.

In this example we are going to exactly the same as we said above. To make this possible we need to make a class named ServletDeletingRowsFromTable, the name of the program should be such, if in future there is any need to make any change in the program, you can easily understand in which program you have to make a change. As we know that in Servlet the main logic of the program is written inside the service method and in turn the service method calls the doGet() method. Now inside the doGet() method use the getWriter() method of the response object and its returns the PrintWriter object, which helps us to write on the browser. To delete the unwanted data from our table there is a need for the connection between the database and the java program. After the establishment of the connection with the database, fire a query for deleting the unwanted row in the database table. This query will be fired inside the prepareStatement() method of the Connection object and returns the PreparedStatement object. If the rows has been deleted from the database table then print the message “row is deleted” otherwise “no rows has been deleted”.

The code of the program is given below:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class ServletDeletingRowsFromTable extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException{
int rows;
response.setContentType(“text/html”);
PrintWriter pw = response.getWriter();
String connectionURL = “jdbc:mysql://localhost/zulfiqar”;
Connection connection;
try{
Class.forName(“org.gjt.mm.mysql.Driver”);
connection = DriverManager.getConnection(connectionURL, “root”, “admin”);
PreparedStatement pst = connection.prepareStatement
(“delete from emp_sal where EmpName = ‘vinod’”);
int i = pst.executeUpdate();
if (i==0){
pw.println(“Row has been deleted”);
}
else{
pw.println(“No rows has been deleted”);
}
}
catch(Exception e){
pw.println(“The exception is ” + e);
}
}
}

web.xml file for this program:

Zulfiqar
ServletDeletingRowsFromTable

Zulfiqar
/ServletDeletingRowsFromTable

The Java servlet code shown above illustrates on how to delete records from database table. The example code is useful to the beginner Java programmer who is searching for the example servlet code to delete records from table.

Getting Number of Rows Servlet Example

The Java servlet example code below demonstratea on how to get the number of rows in the database table. Consider a situation where we want to know about the number of rows in the particular database table without touching our database. As we are the programmers so why we should worry about the database complexities. We want to find out the number of rows without going touching our back- end.

In this example we are going to exactly the same as we said above. To make this possible we need to make a class named ServletGettingNoOfRows, the name of the program should be such, if in future there is any need to make any change in the program, you can easily understand in which program you have to make a change. As we know that in Servlet the main logic of the program is written inside the service method and in turn the service method calls the doGet() method. Now inside the doGet() method use the getWriter() method of the response object and its returns the PrintWriter object, which helps us to write on the browser. To get the number of rows from the database table there is a need for the connection between the database and the java program. After the establishment of the connection with the database, fire a query for selecting the number of rows from the database table inside the executeQuery() method of the PreparedStatement object and returns the ResultSet object. Now we have the ResultSet object, by the help of this object we can get the number of rows we have in the database table. The number of rows we have in the database table will be displayed on the browser by the PrintWriter object.

The code of the program is given below:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletGettingNoOfRows extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException{
int rows=0;
response.setContentType(“text/html”);
PrintWriter pw = response.getWriter();
String connectionURL = “jdbc:mysql://localhost/zulfiqar”;
Connection connection;
try{
Class.forName(“org.gjt.mm.mysql.Driver”);
connection = DriverManager.getConnection(connectionURL, “root”, “admin”);
PreparedStatement pst = connection.prepareStatement(“”);
ResultSet rs = pst.executeQuery(“select count(*) from emp_sal”);
while (rs.next()){
rows = rs.getInt(1);
}
pw.println(“The number of rows are ” + rows);
}
catch(Exception e){
pw.println(“The exception is ” + e);
}
}
}

XML File for this program:

Zulfiqar
ServletGettingNoOfRows

Zulfiqar
/ServletGettingNoOfRows

The servlet example code above illustrates on how to get the number of rows in table. The example code is useful to those individual who is searching for the example code on how to get the number of rows in database table.

Getting Number of Columns in Table Servlet Example

The Java servlet example below demonstrates on how to Get Number of Columns in Table. Consider a situation where there is a need to know about the number of columns in the table without touching our database. As we are the programmers so why we should worry about the database. We want to do the manipulation by sitting on our computer through our program without going into the database.

In this example we are going to exactly the same as we said above. To make this possible we need to make a class named ServletGettingNoOfColumns, the name of the program should be such that if in future there is any need to make any change in the program, you can easily understand in which program you have to make a change. As we know that in Servlet the main logic of the program is written inside the service method and in turn the service method calls the doGet() method. Now inside the doGet() method use the getWriter() method of the response object and its returns the PrintWriter object, which helps us to write on the browser. To get the number of columns from the database table there is a need for the connection between the database and the java program. After the establishment of the connection with the database, pass a query for retrieving all the records from the database and this will return the PreparedStatement object. To get the number of columns from the database we firstly need a reference of ResultSetMetaData object and we will get it only when if we have the ResultSet object with us. To get the object of the ResultSet we will call the method executeQuery() of the PreparedStatement interface. Now we have the object of the ResultSet. By the help of the ResultSet we can get the object of ResultSetMetaData. We will get it by calling the method getMetaData() of the ResultSet interface. The number of columns in the databasd table will be retrieved by the method getColumnsCount() of the ResultSetMetaData interface. This method will return the integer type of value. The number of columns will be displayed on the browser by the PrintWriter object.

The code of the program is given below:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletGettingNoOfColumns extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException{
response.setContentType(“text/html”);
PrintWriter pw = response.getWriter();
String connectionURL = “jdbc:mysql://localhost/zulfiqar”;
Connection connection=null;
try{
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
connection = DriverManager.getConnection(connectionURL, “root”, “admin”);
PreparedStatement pst = connection.prepareStatement(“select * from emp_details”);
ResultSet rs = pst.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int noOfColumns = rsmd.getColumnCount();
//It shows the number of columns
pw.println(“The number of columns are ” + noOfColumns);
}
catch(Exception e){
pw.println(“The exception is ” + e);
}
}
}

web.xml file for this program:

Zulfiqar
ServletGettingNoOfColumns

Zulfiqar
/ServletGettingNoOfColumns

The servlet example code below illustrates on how to get the numbers of columns in the table. The example code is useful to a beginner programmer who is searching for the servlet example code to get the number of columns in database table.

Getting Columns Names Servlet Example

The Servlet example code below show how to get column names from database table. Consider a situation where there is a need to know about the name of the columns without touching our database. As we are the programmers so why we need to worry about the database. We want to do the manipulation by sitting on our computer through our program without going into the database.

In this example we are going to exactly the same as we said above. To make this possible we need to make a class named ServletGettingColumnsNames, the name of the program should be such that if in future there is any need to make any change in the program, you can easily understand in which program you have to make a change. Now inside the doGet() method use the getWriter() method of the response object and its returns the PrintWriter object, which helps us to write on the browser. To get a column names from the database there is a need for the connection between the database and the java program. After the establishment of the connection with the database pass a query for retrieving all the records from the database and this will return the PreparedStatement object. To get the column names from the database we firstly need a reference of ResultSetMetaData object and we will get it only when if we have the ResultSet object. To get the object of the ResultSet we will call the method executeQuery() of the PreparedStatement interface. Now we have the object of the ResultSet. By the help of the ResultSet we can get the object of ResultSetMetaData. We will get it by calling the method getMetaData() of the ResultSet interface. The names of the columns will be retrieved by the method getColumnsNames() of the ResultSetMetaData interface. The output will be displayed to you by the PrintWriter object.

The code of the program is given below:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class ServletGettingColumnsNames extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType(“text/html”);
PrintWriter pw = response.getWriter();
String connectionURL = “jdbc:mysql://localhost/zulfiqar”;
Connection connection=null;
try{
Class.forName(“org.gjt.mm.mysql.Driver”);
connection = DriverManager.getConnection(connectionURL, “root”, “admin”);
PreparedStatement pst = connection.prepareStatement(“select * from emp_details”);
ResultSet rs = pst.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int noOfColumns = rsmd.getColumnCount();
//It shows the number of columns
pw.println(“The number of columns are ” + noOfColumns + “
”);
//It shows the name of the columns
pw.println(“The name of the columns are:
”);
for(int i =1; i<=noOfColumns;i++){
String names = rsmd.getColumnName(i);
pw.println(names);
}
}
catch(Exception e){
pw.println(“The exception is ” + e);
}
}
}

XML File for this program:

Zulfiqar
ServletGettingColumnsNames

Zulfiqar
/ServletGettingColumnsNames

The Java servlet example code above demonstrates on how to get column names from database table. It is useful to a beginner Java programmer who is looking for the example code on how to get column names using servlets.

Source: http://learnhow2program.blogspot.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s