java4all@1986 java. Powered by Blogger.

Adding numbers to text box and providing output in text box?

>> Wednesday, June 8, 2011

<html>
<SCRIPT language = JavaScript>

function calculate() {
A = document.frmOne.txtFirstNumber.value
B = document.frmOne.txtSecondNumber.value
C = (A + B)
document.frmOne.txtThirdNumber.value = C
}

</SCRIPT>
<FORM NAME = frmOne>

Number One: <INPUT TYPE = Text NAME = txtFirstNumber SIZE = 5 value ="">//enter value=5;

Number Two: <INPUT TYPE = Text NAME = txtSecondNumber SIZE = 5 value ="">//enter value=6;
<P>
Total: <INPUT TYPE = Text NAME = txtThirdNumber SIZE = 5 value = "">//we will get total as 56 when we submit the button.
<P>
<Input Type = Button NAME = b1 VALUE = "Add Numbers" onClick = calculate()>

</FORM>
</html>

Read more...

How to make a page to move back page?

Use the below code to make the page to go  back
window.history.back()

Read more...

Different background colors by clicking buttpns?

<HTML>
<BODY>

<INPUT TYPE = Button VALUE = "Colour One" Onclick = "document.bgColor = 'Red'">

<INPUT TYPE = Button VALUE = "Colour Two" Onclick = "document.bgColor = 'Blue'">

<INPUT TYPE = Button VALUE = "Colour Three" Onclick = "document.bgColor = 'Green'">

</BODY>//The background colour of your web page should have changed colour when you clicked a button.
It did this because of the bgColor poperty of the document object.
</HTML>

Read more...

Sample JavaScript with onClick attribute?

<HTML>
<HEAD>
<TITLE>A First Script</TITLE>

<SCRIPT LANGUAGE = JavaScript>

document.write("Hello World")

</SCRIPT>

</HEAD>

<BODY>
<INPUT TYPE = Button VALUE = "Click Me" OnClick = "document.write('Hello World')">
//a OnClick is an event that can be applied to buttons
</BODY>
</HTML>


Read more...

How to change the column name in databae table?

We make a table for storing some type of data. Table keeps the data in the form of rows and columns.
Column indicates the field while row indicate the data of the field. Now consider a scenario where we have
a table and it consists some data and a situation arises where there is a need to change the name of the column.
As this is not the work of the programmer to change the name of the field, but as a programmer we should be aware how we can,
change the name of the column.
The name of the column of the column will be changed by using the simple query. But before going into it,
we should see what are the initial steps to get the desired results. First of all make a database connection with your program.
When the connection has been established pass a query for changing the column name in the preparedStatement().
This will return the PreparedStatement object.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class ServletChangingColumnName 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;
  try{
  Class.forName("org.gjt.mm.mysql.Driver");
  connection = DriverManager.getConnection(connectionURL,
    "root","admin");
  PreparedStatement pst = connection.prepareStatement
  ("alter table emp_details change firstname Name varchar(10)");
  int i = pst.executeUpdate();
  pw.println("The name of the column has been changed");
      }
  catch(Exception e){
  pw.println("The exception is " + e);
     }
  }
}
web.xml:

<web-app>
 <servlet>
 <servlet-name>bhaskar</servlet-name>
 <servlet-class>ServletChangingColumnName</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>bhaskar</servlet-name>
 <url-pattern>/ServletChangingColumnName</url-pattern>
 </servlet-mapping>
</web-app>

Read more...

How to disable refresh button in your application?

Copy the below code and paste in our appliction


<html>
<script type="text/javascript">
function showKeyCode(e)
{
alert("Inside function showKeyCode(e)");
var keycode =(window.event) ? event.keyCode : e.keyCode;

if(keycode == 116)
{
alert("refresh button will not work");
event.keyCode = 0;
event.returnValue = false;
return false;
}
}
</script>

With Body sectiong

<body  onKeyDown ="showKeyCode();">
</body>
</html>

Read more...

How to add a column into the table?

Consider a situation where the requirement of the client gets changed and you have asked to modify the structure of the table.
In reality it is the work of the database administrator but as a Java programmer you should know how you can modify the structure
of the table. The problem is that we have to add a new column to our database by using the java program.
There is no need to get panic. What we simply need is to use a query for adding a new column in the database table.
To get the desired result firstly we need to make a connection with our database. After connection has been established
pass the query in the prepareStatement() for adding new column in the database. This method will return the PreparedStatement object.
By the object of the PreparedStatement we will call the executeUpdate() which will tell the status of  the table.

ServletAddingNewColumn class
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletAddingNewColumn 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;
  try{
  Class.forName("org.gjt.mm.mysql.Driver");
  connection = DriverManager.getConnection(connectionURL, "root", 
  "admin");
  PreparedStatement pst = connection.prepareStatement
  ("alter table emp_details add column sal int(5)");
  int i = pst.executeUpdate();
  if (i==1){
  pw.println("Column has been added");
  }
  else{
  pw.println("No column has been added");
  }
  }
  catch(Exception e){
  pw.println("The exception is " + e);
  }
  }
}
web.xml file

<web-app>
 <servlet>
 <servlet-name>bhaskar</servlet-name>
 <servlet-class>ServletAddingNewColumn</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>bhaskar</servlet-name>
 <url-pattern>/ServletAddingNewColumn</url-pattern>
 </servlet-mapping>
</web-app>

Read more...

How to get number of rows in a 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.

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);
  }
  }
}
  
web.XML

<web-app>
 <servlet>
 <servlet-name>santosh</servlet-name>
 <servlet-class>ServletGettingNoOfRows</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>santosh</servlet-name>
 <url-pattern>/ServletGettingNoOfRows</url-pattern>
 </servlet-mapping>
</web-app>

Read more...

How to get number of columns ina table using Servlets?

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.

ServletGettingNoOfColumns class
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

<web-app>
 <servlet>
 <servlet-name>Zulfiqar</servlet-name>
 <servlet-class>ServletGettingNoOfColumns</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>Zulfiqar</servlet-name>
 <url-pattern>/ServletGettingNoOfColumns</url-pattern>
 </servlet-mapping>
</web-app>

Read more...

How to get the column names from DataBase using Servlets?

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.

ServletGettingColumnsNames class :
web.XML


Zulfiqar
ServletGettingColumnsNames


Zulfiqar
/ServletGettingColumnsNames













Read more...

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP