Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

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

Ads

 
 

Share on Google+Share on Google+

lokeshreddy
How to "Get data froom MySQL DB on giving a value in a tex-box in a JSP file."
1 Answer(s)      6 years ago
Posted in : JavaScript Questions

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;

Ads
View Answers

July 22, 2011 at 11:05 AM


1)ajax.jsp:

<[email protected] 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);
 }
 %>

Ads









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" />
how to reset JFrame
how to reset JFrame  how to reset JFrame
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 make ID - Ajax
how make ID  how make a ID in eyeball chat
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 create an input box?
How to create an input box?  How to create an input box
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 reset a cookie?
How to reset a cookie?  How to reset a cookie
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 arrays in JavaScript?
How to create arrays in JavaScript?  How to create arrays in JavaScript