Java 带有提交按钮的html表格,用于使用SQL数据库中的数据更新每个单独的表格行

Java 带有提交按钮的html表格,用于使用SQL数据库中的数据更新每个单独的表格行,java,html,sql-server,jsp,html-table,Java,Html,Sql Server,Jsp,Html Table,我有一个html表,它使用for循环从SQL数据库打印信息。html表格的每一行上都有一个提交按钮。当用户单击此按钮时,行更新。此命令有效,但如果数据库中有多行具有相同的主键,则只有最后一行更新,并且数据库中具有相同主键的所有行的值都设置为与表中最后一行相同的值 我需要表中每一行上的按钮只更新一行,而不更新具有相同标识符的多行。我是新手,因此非常感谢您的帮助。 以下是我一直使用的代码: <html> <% Connection objConn = null; Medicatio

我有一个html表,它使用for循环从SQL数据库打印信息。html表格的每一行上都有一个提交按钮。当用户单击此按钮时,行更新。此命令有效,但如果数据库中有多行具有相同的主键,则只有最后一行更新,并且数据库中具有相同主键的所有行的值都设置为与表中最后一行相同的值

我需要表中每一行上的按钮只更新一行,而不更新具有相同标识符的多行。我是新手,因此非常感谢您的帮助。
以下是我一直使用的代码:

<html>
<%
Connection objConn = null;
MedicationList objMedicationList=null;
Medication [] objMedication;

OutputLogs objLog=null;
int intDebug=0;
String strDebug;
String sessionUsername, sessionHCN; 
String pmedicationtype=null;
String pdose=null;
String pinstructions=null;
String pcomments=null;
int pvalid=0;

//try{
    objConn=(Connection)session.getAttribute(Patientportal.PortalConstants.SESSION_ATTR_DBCONNECTION);
    objLog=(OutputLogs)session.getAttribute(Patientportal.PortalConstants.SESSION_ATTR_DEBUG_OBJECT);
    intDebug=(int)session.getAttribute(Patientportal.PortalConstants.SESSION_ATTR_DEBUG_FLAG);

    //objPortalUserList=new PatientDetailsList(intDebug,objLog);
    objLog=new OutputLogs(intDebug);

    objMedicationList=new MedicationList(intDebug,objLog);

    sessionUsername=(String)session.getAttribute(Patientportal.PortalConstants.SESSION_ATTR_USERNAME);
    sessionHCN=(String)session.getAttribute(Patientportal.PortalConstants.SESSION_ATTR_HCN);
    if (intDebug==1){
        objLog.SendToDebug("Getting medication");
    }
    objMedication=objMedicationList.GetUserMedication(objConn, sessionHCN); 


            <table style="width:100%">
                <tr>
                    <th>Medication</th>
                    <th>Dose</th>
                    <th>Instructions</th>
                    <th>Comments</th>
                </tr>
                    <% for(int count = 0; count < objMedication.length; count++){%>
                        <form method="post" action="MedicationPage.jsp">
                <tr>
                    <td><%out.println(objMedication[count].GetPatientMedication());%></td>
                    <td><%out.println(objMedication[count].GetDose());%></td>
                    <td><%out.println(objMedication[count].GetHowOften());%></td>
                    <td><%out.println(objMedication[count].GetComments());%></td> 
                    <td><input type="button" value="Request"</td>

                    <% 

                        pvalid=Integer.valueOf(objMedication[count].GetValid()); 
                        pmedicationtype=String.valueOf(objMedication[count].GetPatientMedication());
                        if(pvalid!=0){
                            pvalid=pvalid-1;  
                            objMedicationList.editPrescription(objConn, sessionHCN, pmedicationtype, pvalid); 
                    }
                 }

                %>
                </tr>    
               </form>    
            </table> 

…数据库中具有相同主键的多行仅更新最后一行和的值
数据库中具有相同主键的所有行都设置为与数据库中最后一行相同的值
表


如果您的db表中确实没有唯一的主键,那么这将一直失败。请看这里:

我遗漏了一些代码我只包含了与问题相关的代码否,您的html不完整,例如元素按钮未关闭。根据定义,“主键”是用于唯一标识行的键。如果有两行具有相同的主键,则表示数据设计不正确。可以有一个完全没有主键的表,但是如果没有唯一的字段组合,您不能期望能够区分两行。我在数据库表中没有主键,但使用了“not null”来确保填充必要的列,并在sql命令中使用比较运算符来确保在html表中打印正确的数据