How to "Get data froom MySQL DB on giving a value in a tex-box in a JSP file."

How to "Get data froom MySQL DB on giving a value in a tex-box in a JSP file."

Hi, How to get data from MYSQL Database tables on giving a "text" in a text-box in a JSP file. Ex:dept table; if we give dept_no in a text-box in JSP file,we have to retrieve the other fields from the dept table and display it in the same JSP file.

Help me plz;

View Answers

July 22, 2011 at 11:05 AM

1)ajax.jsp:

<%@page import="java.sql.*"%>
<html>
<head>
<script type="text/javascript">
function showData(id){ 
xmlHttp=GetXmlHttpObject()
    var url="getdata.jsp";
url=url+"?id="+id;
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged(){ 
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
     var showdata = xmlHttp.responseText; 
     var strar = showdata.split(":");
      if(strar.length>1){
        var strname = strar[1];
        document.getElementById("name").style.visibility= "visible";
        document.getElementById("address").style.visibility= "visible";
        document.getElementById("contactNo").style.visibility= "visible";
        document.getElementById("email").style.visibility= "visible";

        document.getElementById("lab1").style.visibility= "visible";
        document.getElementById("lab2").style.visibility= "visible";
        document.getElementById("lab3").style.visibility= "visible";
        document.getElementById("lab4").style.visibility= "visible";

        document.getElementById("name").value= strar[1];
        document.getElementById("address").value= strar[2];
        document.getElementById("contactNo").value= strar[3];
        document.getElementById("email").value= strar[4];
        }
       } 
     } 
    function GetXmlHttpObject(){
var xmlHttp=null;
try{
  xmlHttp=new XMLHttpRequest();
 }
catch(e){
 try{
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch(e){
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
</script>
</head>
<body>
<form name="employee" >
<br><br>
<table >
<div id="mydiv"></div>
<tr><td>Enter Id:</td><td><input type="text" id="id" onkeyup="showData(this.value);"></td></tr>
<tr><td><label id="lab1" style="visibility:hidden">Name:</label></td><td><input type="text" id="name" style="visibility:hidden"></td></tr>
<tr><td><label id="lab2" style="visibility:hidden">Address:</label></td><td><input type="text" id="address" style="visibility:hidden"></td></tr>
<tr><td><label id="lab3" style="visibility:hidden">Contact No:</label></td><td><input type="text" id="contactNo" style="visibility:hidden"></td></tr>
<tr><td><label id="lab4" style="visibility:hidden">Email:</label></td><td><input type="text" id="email" style="visibility:hidden"></td></tr>
</table>
</body>
</html>

2)getdata.jsp:

<%@ page import="java.sql.*" %> 
<%
String emp_id = request.getParameter("id").toString();
 String buffer="";  
 try{
  Class.forName("com.mysql.jdbc.Driver");
  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
 Statement stmt = con.createStatement();  
 ResultSet rs = stmt.executeQuery("Select * from employee where id='"+emp_id+"'");  
   while(rs.next()){
   buffer=":" +rs.getString("name") +":"+ rs.getString("address") +":"+rs.getString("contactNo")+":"+  rs.getString("email");
   }
 out.println(buffer);
 System.out.println(buffer);
 }
 catch(Exception e){
 System.out.println(e);
 }
 %>









Related Tutorials/Questions & Answers:
How to "Get data froom MySQL DB on giving a value in a tex-box in a JSP file."
How to "Get data froom MySQL DB on giving a value in a tex-box in a JSP file."  Hi, How to get data from MYSQL Database tables on giving a "text" in a text-box in a JSP file. Ex:dept table; if we give dept_no in a text-box in JSP
how to code
how to code  how to create mysql coding in php
Advertisements
how to update
how to update   conditional update
how to display?
how to display?  How to write a select query for displaying data where i have author as multivalued attribute
how to display?
how to display?  How to write a select query for displaying data where i have author as multivalued attribute
how to display?
how to display?  How to write a select query for displaying data where i have author as multivalued attribute
How to include?
How to include?  How can we include a JSP file in one application to the JSP file in another application which are in the same server?? please help me out with code
How to connect
How to connect  how to connect to a remote host using jsp? We need to update files to the database of the application on the local machine from the updated database on our webpage
how to solve
how to solve   log4j:WARN No appenders could be found for logger (org.apache.struts.util.PropertyMessageResources). log4j:WARN Please initialize the log4j system properly
How to add
How to add   Hi, I want to make a website which will recommend users books according to the genre selected by them. I am using PHP, JavaScript and mySQL. The problem is that there will me almost more than 100 books
how to solve this..
how to solve this..  Given a list of students and their marks Name... dimensional arrays for the marks. From this creation get : a. Convert marks... in that class. d. List of student that get 100 in each subject
how to solve this..
how to solve this..  Given a list of students and their marks Name... dimensional arrays for the marks. From this creation get : a. Convert marks... in that class. d. List of student that get 100 in each subject
how to do this?
how to do this?   Given any integer 2D array, design then implement a Java program that will add to each element in the array the corresponding column number and the corresponding row number. Then, it prints the array before
How to? - IoC
How to?  I am having problems coding my LOC to count constructors and methods?  Hi , Methods and constructors both use super to refer to a superclass, but in different ways. Methods use super to execute
how to retrive
how to retrive  this my servlet page.. @Override protected void... (Exception e) { System.out.println("......."+e); } } and this is my jsp..." value="Edit" style="background-color:#49743D
how to display?
how to display?  I have to write a code for searcing books in library...:8080/examples/jsp/searchbook.jsp" method="post"> <table align="center"> <...; </tr> <tr> <td> <input type="submit" value="Search" />
VoIP HOW TO
to the telephone companies box is in order.     How does VoIP... How to work It is very easy to get into a discussion that is very technical... VoIP HOW TO How to Distribute VoIP We recommend using
Hibernate How To
Hibernate How To Adding the ordering ability in your Hibernate Application while using hibernateTemplate In this section you will learn how... hibernateTemplate.. Searching Result using criteriaIn this tutorial you will learn how
How to capture video and then how to store it in mobile memory
How to capture video and then how to store it in mobile memory  Hello there, I'm developing a mobile application where i'm capturing video from mobile camera and uploading it to my own video portal.. I'm trying many codes
how to reset JFrame
how to reset JFrame  how to reset JFrame
how make ID - Ajax
how make ID  how make a ID in eyeball chat
How to create an input box?
How to create an input box?  How to create an input box
How to reset a cookie?
How to reset a cookie?  How to reset a cookie
How to create arrays in JavaScript?
How to create arrays in JavaScript?  How to create arrays in JavaScript
How to set the cursor to wait?
How to set the cursor to wait?  How to set the cursor to wait
How to make elements invisible ?
How to make elements invisible ?   How to make elements invisible
how to configure the hibernate in netbean
how to configure the hibernate in netbean  how to configure the hibernate in netbean 6.5
how to show popup in javascript
how to show popup in javascript  How to show popup in javascript
how to make this pattern???
how to make this pattern???  how to make following pattern in javascript
how to normalize a database schema
how to normalize a database schema  how to normalize a database schema
how to download oracle 9i
how to download oracle 9i  how to download oracle 9i
How to Become a Business Analyst
How to Become a Business Analyst  How to Become a Business Analyst
how to create notepad in java
how to create notepad in java  how to create notepad in java
How to: generic array creation
How to: generic array creation  How to: generic array creation
How to put an image on a JButton?
How to put an image on a JButton?  How to put an image on a JButton
How to use Servlet and Ajax?
How to use Servlet and Ajax?  How to use Servlet and Ajax
How to add JTable in JPanel
How to add JTable in JPanel  How to add JTable in JPanel
how it maps to Struts
how it maps to Struts  What is MVC and how it maps to Struts
how to conduct the test in java
how to conduct the test in java  how to conduct the test in java
how formBackingObject() method will work.
how formBackingObject() method will work.  How the formBackingObject method will behave
How To Turn On the Session Support?
How To Turn On the Session Support?  How To Turn On the Session Support
How to reload the current page?
How to reload the current page?  How to reload the current page
How to set cookies?
How to set cookies?  How to set cookies
How to destroy a cookie?
How to destroy a cookie?  How to destroy a cookie
How to Handle Event Handlers?
How to Handle Event Handlers?  How to Handle Event Handlers
How to disable an HTML object?
How to disable an HTML object?  How to disable an HTML object
How to comment javascript code?
How to comment javascript code?  How to comment javascript code
How to create a confirmation box?
How to create a confirmation box?  How to create a confirmation box
How to call servlet in JSP?
How to call servlet in JSP?  How to call servlet in JSP
how to use calender in php
how to use calender in php  how to put calender in php

Ads