我想从同一个jsp文件中的数据库中检索数据到表中

时间:2022-09-25 21:14:57

hi frnds i am using jsp and servlets and my problem is to retrieve the data from database into the same jsp file i had two tables in jsp file.i want to get the data into the second table.so please help me frnds.below is my code

嗨frnds我使用jsp和servlets,我的问题是从数据库中检索数据到同一个jsp文件我在jsp file.i中有两个表。我想把数据放到第二个表。所以请帮我frnds.below是我的代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="com.sun.xml.internal.txw2.Document"%><html>
<%@page import="java.io.IOException" %>
<%@page import="java.io.PrintWriter" %>
<%@page import="java.sql.Connection" %>
<%@page import="java.sql.DriverManager" %>
<%@page import="java.sql.Statement" %>
<%@page import="java.sql.ResultSet" %>
<%@page import="javax.servlet.ServletException" %>
<%@page import="javax.servlet.http.HttpServlet" %>
<%@page import="javax.servlet.http.HttpServletRequest" %>
<%@page import="javax.servlet.http.HttpServletResponse" %>

<head>
<title>DEALER/SUPPLIER</title>

<link rel="stylesheet" type="text/css" href="css/jquery.autocomplete.css" />
<script src="js/jquery-1.7.2.js"></script>  
<script src="js/jquery.autocomplete.js"></script>  
<style type="text/css">

input, textarea,select {
  background-color : lightgray;
}
table
{
border: 2px solid gray;
}
</style>
<script type="text/javascript">

function check(){

    var dealId = document.getElementById('dealer_id').value;
    var dealerName=document.getElementById('dealer_name').value;
    var doorNo=document.getElementById('door_no').value;
    var city=document.getElementById('city').value;
    var disc=document.getElementById('district').value;
    var state=document.getElementById('state').value;
    var pin=document.getElementById('pin_code').value;
    var mobile=document.getElementById('mobile').value;
    var phone=document.getElementById('phone_no').value;
    var credit=document.getElementById('credit_offered_i').value;


    if(dealId ==""||isNaN(dealId))
    {
         var el1 = document.createElement("div");
         el1.setAttribute("style","position:absolute;top:15%;left:80%;background-color:white;height:50px;width:150px;");
         el1.innerHTML = "please enter dealer id";
         setTimeout(function(){
          el1.parentNode.removeChild(el1);
         },2000);
         document.body.appendChild(el1);
    return false;
    }
    else if (dealerName=="") {
        var el = document.createElement("div");
         el.setAttribute("style","position:absolute;top:15%;left:80%;background-color:white;height:50px;width:150px;");
         el.innerHTML = "please enter dealer name";
         setTimeout(function(){
          el.parentNode.removeChild(el);
         },2000);
         document.body.appendChild(el);
    return false;
    }
    else if (doorNo=="") {
        var el = document.createElement("div");
         el.setAttribute("style","position:absolute;top:15%;left:80%;background-color:white;height:50px;width:150px;");
         el.innerHTML = "please enter door no";
         setTimeout(function(){
          el.parentNode.removeChild(el);
         },2000);
         document.body.appendChild(el);
    return false;
    }
    else if (city=="") {
        var el = document.createElement("div");
         el.setAttribute("style","position:absolute;top:15%;left:80%;background-color:white;height:50px;width:150px;");
         el.innerHTML = "please enter the name of city";
         setTimeout(function(){
          el.parentNode.removeChild(el);
         },2000);
         document.body.appendChild(el);
    return false;
    }
    else if (disc=="") {
        var el = document.createElement("div");
         el.setAttribute("style","position:absolute;top:15%;left:80%;background-color:white;height:50px;width:150px;");
         el.innerHTML = "please enter the name of district";
         setTimeout(function(){
          el.parentNode.removeChild(el);
         },2000);
         document.body.appendChild(el);
    return false;
    }

    else if (state=="") {
        var el = document.createElement("div");
         el.setAttribute("style","position:absolute;top:15%;left:80%;background-color:white;height:50px;width:150px;");
         el.innerHTML = "please enter the state name";
         setTimeout(function(){
          el.parentNode.removeChild(el);
         },2000);
         document.body.appendChild(el);
    return false;
    }

    else if (pin=="") {
        var el = document.createElement("div");
         el.setAttribute("style","position:absolute;top:15%;left:80%;background-color:white;height:50px;width:150px;");
         el.innerHTML = "please enter pin code";
         setTimeout(function(){
          el.parentNode.removeChild(el);
         },2000);
         document.body.appendChild(el);
    return false;
    }
    else if (mobile==""||isNaN(mobile)) {
        var el = document.createElement("div");
         el.setAttribute("style","position:absolute;top:15%;left:80%;background-color:white;height:50px;width:150px;");
         el.innerHTML = "please enter correct mobile number";
         setTimeout(function(){
          el.parentNode.removeChild(el);
         },2000);
         document.body.appendChild(el);
    return false;
    } 
    else if (phone=isNaN(phone)) {
        var el = document.createElement("div");
         el.setAttribute("style","position:absolute;top:15%;left:80%;background-color:white;height:50px;width:150px;");
         el.innerHTML = "please enter correct phone number";
         setTimeout(function(){
          el.parentNode.removeChild(el);
         },2000);
         document.body.appendChild(el);
    return false;
    } 

    else if(credit =="" ||isNaN(credit))
    {
        var el = document.createElement("div");
         el.setAttribute("style","position:absolute;top:15%;left:80%;background-color:white;height:50px;width:150px;");
         el.innerHTML = "please enter credit offered";
         setTimeout(function(){
          el.parentNode.removeChild(el);
         },2000);
         document.body.appendChild(el);
    return false;
    }




    else
    {
            return true;
    }   
}


function editValues(){


    var dealId = document.getElementById('dealer_id').value;

    if(dealId ==""||isNaN(dealId))
    {
         var el = document.createElement("div");
         el.setAttribute("style","position:absolute;top:15%;left:80%;background-color:white;height:50px;width:150px;");
         el.innerHTML = "please enter dealer id";
         setTimeout(function(){
          el.parentNode.removeChild(el);
         },2000);
         document.body.appendChild(el);
    return false;
    }
    else
    {
        document.forms[0].action="editDealer?dealer_id="+document.getElementById('dealer_id').value;
        document.forms[0].submit();
        return true;
    }   
}
function deleteValues()
{


    var dealId = document.getElementById('dealer_id').value;

    if(dealId ==""||isNaN(dealId))
    {
         var el = document.createElement("div");
         el.setAttribute("style","position:absolute;top:15%;left:80%;background-color:white;height:50px;width:150px;");
         el.innerHTML = "please enter dealer id";
         setTimeout(function(){
          el.parentNode.removeChild(el);
         },2000);
         document.body.appendChild(el);
    return false;
    }
    else
    {
        if(confirm("do you want to delete the record") == true)
        {
        document.forms[0].action="deleteDealer?dealer_id="+document.getElementById('dealer_id').value;
        document.forms[0].submit();
        return true;
        }   
        else
        {
            document.forms[0].reset();
            return false;
        }

    }

}
</script>

</head>
<body bgcolor="#DDDDDD">
<p align="center"><b>DEALER/SUPPLIER</b></p>
<form method="post" name="dealerMasters" action="dealerMasters">
<%
String status=(String)session.getAttribute("status1");
if(status =="success")
{
    %><h3>successfully inserted</h3> <% 
}
else if(status =="updatesuccess")
{
    %><h3>updated successfully</h3> <%
}
else if(status =="fail")
{
    %><h3>already existing user</h3> <%
}
String deleteStatus=(String)session.getAttribute("deleteStatus");
if(deleteStatus =="success")
{
    %><h3>deleted  successfully</h3> <% 
}
else if(deleteStatus =="fail")
{
    %><h3>There are no records with this dealer id</h3> <%
}

String editStatus =(String)session.getAttribute("edit");
if(editStatus =="fail")
{
     %><h3> there are no records with this dealer id </h3> 
     <% 
}
%>

<table align="center" cellpadding="2" cellspacing="2">
<tr>
<td><font color="maroon">Dealer Id</font>
</td>
<td >
<input type="text" maxlength="10" name="dealer_id" id="dealer_id">
</td>
<%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("dealer_id").value='<%=session.getAttribute("deal_id")%>';

    </script>
    <%
}
%>

  <td >
           <font color="maroon">Dealer Name</font>
   </td>

    <td colspan="4">
        <input type="text" maxlength="50" size="60" name="dealer_name" id="dealer_name">
   </td>
<%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("dealer_name").value='<%=session.getAttribute("deal_name")%>';
    </script>
    <%
}
%>
</tr>

 <tr>
            <td><font color="maroon">Door no</font>
      </td>
      <td  >
             <input type="text" maxlength="25" name="door_no" id="door_no">

      </td>
      <%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("door_no").value='<%=session.getAttribute("doorNo")%>';
    </script>
    <%
}
%>
      <td >   <font color="maroon">Street</font>

      </td>
      <td>
               <input type="text" maxlength="25" name="street" id="street">
      </td>

<%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("street").value='<%=session.getAttribute("street")%>';
    </script>
    <%
}
%>
       <td>      <font color="maroon">City</font>

     </td> 
    <td>
              <input type="text" maxlength="25" name="city" id="city">  

    </td>
    <%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("city").value='<%=session.getAttribute("city")%>';
    </script>
    <%
}
%>
</tr>

<tr>
    <td >      <font color="maroon">District</font>

   </td>
   <td>
           <input type="text" maxlength="25" name="district" id="district">  

  </td>
  <%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("district").value='<%=session.getAttribute("district")%>';
    </script>
    <%
}
%>

    <td>      <font color="maroon">State</font>

     </td> 
    <td>
              <input type="text" maxlength="25" name="state" id="state">  

    </td>
    <%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("state").value='<%=session.getAttribute("state")%>';
    </script>
    <%
}
%>
    <td >      <font color="maroon">PinCode</font>

   </td>
   <td>
           <input type="text" maxlength="7" name="pin_code" id="pin_code">  

  </td>
  <%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("pin_code").value='<%=session.getAttribute("pin_code")%>';
    </script>
    <%
}
%>
</tr>



<tr>
  <td >      <font color="maroon">Mobile</font>

   </td>
   <td>
           <input type="text" maxlength="15" name="mobile" id="mobile">  

  </td>  
  <%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("mobile").value='<%=session.getAttribute("mobile")%>';
    </script>
    <%
}
%>

   <td>  
             <font color="maroon">Contact person</font> 
  </td>

   <td  colspan="4">

                   <input type="text" maxlength="30" size="60" name="contact_person" id="contact_person">

  </td>
  <%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("contact_person").value='<%=session.getAttribute("contact")%>';
    </script>
    <%
}
%>
</tr>

<tr>
    <td>
              <font color="maroon">Phone no</font>
   </td>
   <td >
              <input type="text" maxlength="15" name="phone_no" id="phone_no">
   </td>
   <%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("phone_no").value='<%=session.getAttribute("phone")%>';
    </script>
    <%
}
%>
   <td >
              <font color="blue">Email Id</font>
   </td>
  <td colspan="4">
             <input type="text" maxlength="25" size="60"  name="email_id" id="email_id">
  </td>
<%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("email_id").value='<%=session.getAttribute("email")%>';
    </script>
    <%
}
%>
</tr>

<tr>
     <td >
            <font color="blue">Fax</font>
     </td>
     <td>
            <input type="text" maxlength="20" name="fax" id="fax">
     </td>
     <%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("fax").value='<%=session.getAttribute("fax")%>';
    </script>
    <%
}
%>

     <td>
           <font color="blue">Credit offered</font>
     </td>
     <td colspan="2">
           <input type="text" maxlength="10" name="credit_offered_i" id="credit_offered_i">
           <% 
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("credit_offered_i").value='<%=session.getAttribute("credOffI")%>';
    </script>
    <%
}
%>
       <select name="credit_offered" id="credit_offered">
<option value="years">Years</option>
<option value="months">Months</option>
<option value="days">Days</option>
</select>
</td>
<% 
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("credit_offered").value='<%=session.getAttribute("crePeriod")%>';
    </script>
    <%
}
%>
</tr>
<tr>
    <td>
           <font color="blue">Vendor Rating</font>
   </td>
   <td >
          <input type="text" maxlength="10" name="vendor_rating" id="vendor_rating">
   </td>
   <%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("vendor_rating").value='<%=session.getAttribute("venRat")%>';
    </script>
    <%
}
%>

    <td>
            <font color="blue">Gst No</font>
   </td>
   <td colspan="4">
            <input type="text" maxlength="40" size="60" name="gst_no" id="gst_no">
   </td>
   <%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("gst_no").value='<%=session.getAttribute("gstNo")%>';
    </script>
    <%
}
%>
</tr>
<tr>
 <td>
           <font color="blue">Cst No</font>
   </td>
   <td colspan="3">
          <input type="text" maxlength="40" size="65" name="cst_no" id="cst_no">
   </td>  
<%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("cst_no").value='<%=session.getAttribute("cstNo")%>';
    </script>
    <%
}
%>
</tr>
<tr>
 <td>
           <font color="blue">Remarks</font>
   </td>
   <td colspan="3">
          <input type="text" maxlength="50" size="65" name="remarks" id="remarks">
   </td>  
   <%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("remarks").value='<%=session.getAttribute("ramarks")%>';
    </script>
    <%
}
%>

</tr>
</table>


<br>
<table align="center" cellpadding="1">
<tr>
<td>
<input type="submit" value="save" id="save" onclick="return check()"/>
</td>
<td>
<input type="submit" value="edit" id="edit" onclick="return editValues()"/>
</td>
<td>
<input type="submit" value="delete" id="delete" onclick="return deleteValues()"/>
</td>
<td>
<input type="reset" value="cancel" id="cancel">
</td>
<td>
<input type="button" value="exit" id="exit">
</td>
<td>
<input type="hidden" value="save" id="hiddenValue" name="hiddenValue"/>
</td>
</tr>
</table>
<%
if(editStatus =="success")
{
    %>
    <script type="text/javascript">
    document.getElementById("hiddenValue").value='update';
    </script>
    <%
}
if(session !=null)
    session.invalidate();
%>
<script>
    $("#dealer_id").autocomplete("dealerList1.jsp");
</script>

<br><br>
<%  
Class.forName("com.mysql.jdbc.Driver");

System.out.println("driver loaded");
System.out.println("Driver is loaded");
Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/charms?user=root&password=root");
System.out.println("Connection created");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from dealer_masters");%>

<table align="center" border="1">

  <tr>
    <td>Dealer Id</td>
    <td>Dealer Name</td>
    <td>Door no</td>
    <td>Street</td>
    <td>City</td>
    <td>District</td>
    <td>State</td>
    <td>Mobile</td>
    <td>Contact Person</td>
    <td>Phone No</td>

  </tr>
   <% while (rs.next()) {%>
  <tr>
   <td><%=rs.getString("deal_id_v")%></td>
    <td ><%=rs.getString("deal_name_v") %></td>
    <td><%=rs.getString("deal_door_no_v") %></td>
    <td><%=rs.getString("deal_street_v") %></td>
    <td><%= rs.getString("deal_city_v") %></td>
    <td><%= rs.getString("deal_district_v")%></td>
    <td><%= rs.getString("deal_state_v")%></td>
    <td ><%=rs.getString("deal_mobile_no_v")%></td>
    <td><%=rs.getString("deal_contactperson_v") %></td>
    <td><%=rs.getString("deal_phone_no_v") %></td>

  </tr>
 <%}%>
</table>
</form>
</body>
</html>

1 个解决方案

#1


0  

i modified u r code try this

我修改了你的代码试试这个

    <div>
    <table border="1" width="400" >
    <tr>
    <thead> 
        <th>Dealer Id</th>
        <th>Dealer Name</th>
        <th>Door no</th>
        <th>Street</th>
        <th>City</th>
        <th>District</th>
        <th>State</th>
        <th>Mobile</th>
        <th>Contact Person</th>
        <th>Phone No</th>
    </thead>
    </tr>
    <%
    try
    {

    Class.forName("com.mysql.jdbc.Driver");

    System.out.println("driver loaded");
    System.out.println("Driver is loaded");
    Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/charms?user=root&password=root");
    System.out.println("Connection created");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select * from dealer_masters");
    while(rs.next())
    {
        <tr>
   <td><%=rs.getString("deal_id_v")%></td>
    <td ><%=rs.getString("deal_name_v") %></td>
    <td><%=rs.getString("deal_door_no_v") %></td>
    <td><%=rs.getString("deal_street_v") %></td>
    <td><%= rs.getString("deal_city_v") %></td>
    <td><%= rs.getString("deal_district_v")%></td>
    <td><%= rs.getString("deal_state_v")%></td>
    <td ><%=rs.getString("deal_mobile_no_v")%></td>
    <td><%=rs.getString("deal_contactperson_v") %></td>
    <td><%=rs.getString("deal_phone_no_v") %></td>

  </tr>
    %>

    <%}%> 
        </table>
       <%
        rs.close();
        stmt1.close();
        connection.close();
        }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    %>

    </div>

#1


0  

i modified u r code try this

我修改了你的代码试试这个

    <div>
    <table border="1" width="400" >
    <tr>
    <thead> 
        <th>Dealer Id</th>
        <th>Dealer Name</th>
        <th>Door no</th>
        <th>Street</th>
        <th>City</th>
        <th>District</th>
        <th>State</th>
        <th>Mobile</th>
        <th>Contact Person</th>
        <th>Phone No</th>
    </thead>
    </tr>
    <%
    try
    {

    Class.forName("com.mysql.jdbc.Driver");

    System.out.println("driver loaded");
    System.out.println("Driver is loaded");
    Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/charms?user=root&password=root");
    System.out.println("Connection created");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select * from dealer_masters");
    while(rs.next())
    {
        <tr>
   <td><%=rs.getString("deal_id_v")%></td>
    <td ><%=rs.getString("deal_name_v") %></td>
    <td><%=rs.getString("deal_door_no_v") %></td>
    <td><%=rs.getString("deal_street_v") %></td>
    <td><%= rs.getString("deal_city_v") %></td>
    <td><%= rs.getString("deal_district_v")%></td>
    <td><%= rs.getString("deal_state_v")%></td>
    <td ><%=rs.getString("deal_mobile_no_v")%></td>
    <td><%=rs.getString("deal_contactperson_v") %></td>
    <td><%=rs.getString("deal_phone_no_v") %></td>

  </tr>
    %>

    <%}%> 
        </table>
       <%
        rs.close();
        stmt1.close();
        connection.close();
        }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    %>

    </div>