gpt4 book ai didi

java - 在 JSP 中从 MySQL 数据库中获取值

转载 作者:行者123 更新时间:2023-11-29 06:23:26 25 4
gpt4 key购买 nike

以下代码用于从 MySQL 数据库中获取 PONo 并在按钮中查看它们。

当用户点击(PONo viewing)按钮时,会出现另一个页面来查看该特定PONo的采购订单信息。(viewpo.jsp)

但如屏幕截图所示,当我单击 104 时显示 102 采购订单详细信息。当我点击 102 时会显示 102 采购订单的详细信息。 enter image description here如何修复此错误?

 <%
String CONN_STRING = "jdbc:mysql://localhost/pmsdb";
String USERNAME = "dbuser";
String PASSWORD = "dbpassword";

Connection conn=null;
PreparedStatement pst=null;
ResultSet rs =null;
ResultSet rs2 =null;
ResultSet rs3 =null;
Statement stmt=null;
Statement stmt2=null;

Object userr = session.getValue("Username");
String user = userr.toString();
Class.forName("com.mysql.jdbc.Driver");

conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);

String sql0="SELECT * FROM websupplierinfo WHERE username='"+user+"'";
pst=conn.prepareStatement(sql0);
rs3=pst.executeQuery();

if(rs3.next()){
String companyname = rs3.getString("Company");

String sql = "SELECT * FROM purchaseorderinfo WHERE Supplier='"+companyname+"' ORDER BY PONo DESC";
// String sql2 = "SELECT * FROM itemspoinfo WHERE Company='"+companyname+"' ORDER BY PONo DESC";
stmt = conn.createStatement();
rs=stmt.executeQuery(sql);

//stmt2=conn.createStatement();
// rs2=stmt2.executeQuery(sql2);

%>

<div class="row">
<div class="col-md-12">
<div class="citydetails">
<div class="panel panel-primary">
<div class="panel-heading">Available Purchase Orders</div>
<div class="panel-body">
<table class="table">
<tr>
<th>All Purchase Orders</th>


</tr>
<tr><td><form>
<div class="form-group">
<div class="row">
<div class="col-sm-6 center-block">
<input type="button" name="view" id="view" tabindex="1" class="form-control btn btn-login" value="All Purchase Orders" onclick="window.open('alladminpo.jsp','_blank','resizable=yes')">
</div>
</div>
</div>
</form></td>
</tr>

<th>Latest Purchase Order Numbers</th>
<%while(rs.next()){ %>
<tr>
<td>
<form>
<div class="form-group">
<div class="row">
<div class="col-sm-2 center-block">
<input type="button" name="view" id="view" tabindex="1" class="form-control btn btn-login" value="<%=rs.getString("PONo")%>" onclick="window.open('viewpo.jsp','_blank','resizable=yes')">
<%
String po = rs.getString("PONo");
session.putValue("PONo", po);
%>
</div>
</div>
</div>
</form>
<% }}%>
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>

viewpo.jsp(重要代码段);

    <%
String CONN_STRING = "jdbc:mysql://localhost/pmsdb";
String USERNAME = "dbuser";
String PASSWORD = "dbpassword";

Connection conn=null;
ResultSet rs =null;
ResultSet rs2 =null;

Statement stmt=null;
Statement stmt2=null;

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

conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);

Object po = session.getValue("PONo");
String pno=po.toString();

String sql = "SELECT * FROM purchaseorderinfo WHERE PONo='"+pno+"'";
String sql2 ="SELECT * FROM itemspoinfo WHERE PONo='"+pno+"'";

stmt = conn.createStatement();
rs=stmt.executeQuery(sql);

stmt2=conn.createStatement();
rs2=stmt2.executeQuery(sql2);
%>

<div class="row">
<div class="col-md-12">
<div class="citydetails">
<div class="panel panel-primary">
<div class="panel-heading">Purchase Order Details</div>
<div class="panel-body">
<table class="table">
<thead>
<tr>
<th>Purchase Order <%=session.getValue("PONo") %></th>
</tr>
</thead>
<tbody>

<tr>

<td>
<% if(rs.next()){ %>
Purchase Order Date : <%= rs.getString("PODate") %><br>
Expected Date : <%=rs.getString("ExpectedDate") %> <br>
<% } %>

<table class="table">
<tbody>
<tr>
<th>Items</th>
<th>Quantity</th>
</tr>
<% if (rs2.next()){ %>
<tr>

<td><%=rs2.getString("ItemName")%></td>
<td><%=rs2.getString("Qty")%></td>

</tr>
<% } %>

最佳答案

您正在替换 session 中的 PONo 值,它只包含最后一个值。因此,将 PONo 值作为 request 参数传递给 viewpo.jsp

   <input type="button" name="view" id="view" tabindex="1" class="form-control btn btn-login" value="<%=rs.getString("PONo")%>" onclick="window.open('viewpo.jsp?PONo=<%=rs.getString("PONo")%>','_blank','resizable=yes')">

并在您的 jsp 中删除以下代码,因为它替换了 PONo 的值。

 <% 
String po = rs.getString("PONo");
session.putValue("PONo", po);
%>

viewpo.jsp 中,我们可以像下面这样访问 PONo:

String pno=request.getParameter('PONo');

无论您在何处使用 session.getValue("PONo");,都必须 request.getParameter("PONo")

关于java - 在 JSP 中从 MySQL 数据库中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32307680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com