1

我有一个 jsp 页面,它获取两个参数 1 用于添加另一个用于查看。这个 jsp 页面被命名为 sucessAdmin.jsp。我有另一个调用 sucessadmin.jsp 的 jsp 页面 admin1.jsp。admin1.jsp 有一个表单视图和 add.so,如果我按下视图按钮,它会调用 sucessAdmin.jsp 并显示数据。当我按下添加按钮时,它会显示一个输入数据的表单,但它不会向 mysql DB 添加值。当我再次调用同一页面时,它不会添加数据。请告诉我哪里出错了

这是豆子

package sucessAdmin;
import java.sql.*;
import java.io.*;
import java.util.*;
public class sucessAdmin {
private String question;
private String answer;

private String questionNo;
private String opt1;
private String opt2;
private String opt3;
private String opt4;
public sucessAdmin()
{

}

public void setQuestion(String question)
{
    this.question=question;
}
public String getQuestion()
{
    return question;
}
public void setAnswer(String answer)
{
    this.answer=answer;
}
public String getAnswer()
{
    return answer;
}
public void setQuestionNo(String questionNo)
{
    this.questionNo=questionNo;
}
public String getQuestionNo()
{
    return questionNo;
}
public void setOpt1(String opt1)
{
this.opt1=opt1;
}
public String getOpt1()
{
    return opt1;
}
public void setOpt2(String opt2)
{
this.opt2=opt2;
}
public String getOpt2()
{
    return opt2;
}
public void setOpt3(String opt3)
{
this.opt3=opt3;
}
public String getOpt3()
{
    return opt3;
}
public void setOpt4(String opt4)
{
this.opt4=opt4;
}
public String getOpt4()
{
    return opt4;
}

    public Vector getDetails()
    {
        int x=0;
        Vector v= new Vector();
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/Ontest";
            Connection con=DriverManager.getConnection(url,"root","spanwave");
            ResultSet rs=null;
            Statement st=con.createStatement();
            rs=st.executeQuery("select * from questions");
            String quer="SELECT * FROM questions";

                     rs=st.executeQuery(quer);
                    ResultSetMetaData rsmd = rs.getMetaData();
                    int NumOfCol=0;
                    NumOfCol=rsmd.getColumnCount();
                    System.out.println("Query Executed!! No of Colm="+NumOfCol);
            while(rs.next())
            {

                for(int i=1;i<NumOfCol+1;i++)
                {
                    if(i==NumOfCol)
                        {System.out.print(rs.getString(i));System.out.println();}else
                System.out.print(rs.getString(i)+" ");
                    v.addElement(rs.getString(i));
                }


            }

        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        return v;
    }
    public void addDetails(String question,String opt1,String opt2,String opt3,String opt4,String questionNo,String answer)
    //public void addDetails()
    {
        int qnNo=0;

        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/Ontest";
            Connection con=DriverManager.getConnection(url,"root","spanwave");
            ResultSet rs=null;
            qnNo=Integer.parseInt(questionNo);
            PreparedStatement ptst=con.prepareStatement("insert into questions values(?,?,?,?,?,?,?)");
ptst.setString(1,question );
ptst.setString(2,opt1 );
ptst.setString(3,opt2 );
ptst.setString(4,opt3 );
ptst.setString(5,opt4 );
ptst.setInt(6, qnNo);
ptst.setString(7,answer);
ptst.executeUpdate();

                    int NumOfCol=0;
                    System.out.println("Query Executed!! No of Colm="+NumOfCol);

    }
        catch(Exception e)
        {
            System.out.println(e);
        }


}
}

这是调用页面

<%@ 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">
<html>
<head>
<jsp:useBean id="admin" scope="request" class="admin.adminBean">
<jsp:getProperty property="userId" name ="admin"/>
</jsp:useBean>
<jsp:useBean id="sucessAdmin" scope ="request" class="sucessAdmin.sucessAdmin"></jsp:useBean>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><jsp:getProperty property="userId" name ="admin"/></title>
</head>
<body bgcolor="pink">
<form action="details.jsp" method="get">
view questions<input type="radio" name="details" value="view"><br>
edit questions
<input type="radio" name="details" value="edit"><br>
add questions

<input type="radio" name="details" value="add"><br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

这是被调用的页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><jsp:useBean id="sucessAdmin" scope ="request" class="sucessAdmin.sucessAdmin">

</jsp:useBean>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Details</title>
</head>
<body>

<%String input; %>
<%=input=request.getParameter("details") %>
<%if (input.equals("view")) {
    %><%Vector v; %>
    <%v=(Vector)sucessAdmin.getDetails();
out.println(sucessAdmin.getDetails()); }
%><%
if(input.equals("add"))
    {out.println("addds");
    %>
    <form method="POST" action="details.jsp">
<table>
<tr>
<td>question<input type="text" name="question" value="" /></td>
</tr>
<tr>
<td>opt1<input type="text" name="opt1" value="" /></td>
</tr>
<tr>
<td>opt2<input type="text" name="opt2" value="" /></td>
</tr>
<tr>
<td>opt3<input type="text" name="opt3" value="" /></td>
</tr>


<tr>
<td>opt4<input type="text" name="opt4" value="" /></td>
</tr>
<tr>
<td>questionNumber<input type="text" name="questionNo" value="" /></td>
</tr>
<tr>
<td>answer<input type="text" name="answer" value="" /></td>
</tr>




</table><br><input type="hidden" name="details"/>
<br><input type="submit" name="submit" value="submit"/>
<%sucessAdmin.addDetails("question","opt1","opt2","opt3","opt4","questionNo","answer");%>
</form>

    <%out.println("adding");}
%>
</body>
</html>
4

1 回答 1

0
replace <%sucessAdmin.addDetails("question","opt1","opt2","opt3","opt4","questionNo","answer");%>

<% 
      String question = request.getParameter("question");
      String opt1= request.getParameter("opt1");
      String opt2 = request.getParameter("opt2");
      String opt3 = request.getParameter("opt3");
      String opt4 = request.getParameter("opt4");
      String questionNo = request.getParameter("questionNo");
      String answer = request.getParameter("answer");
      if(question!=null && opt1!=null && opt2 != null
         opt3!=null && opt4 !=null && questionNo!=null && answer!=null) 
      {
           sucessAdmin.addDetails(question,opt1,opt2,opt3,opt4,questionNo,answer);
       }
   %>

试试这个,让我们希望它有效..

于 2013-03-02T17:13:40.473 回答