-1

我正在尝试创建一个程序,用户可以在其中将匹配结果输入到 html 页面中,然后将该数据添加到数据库中,但我可以获取要插入的任何数据。我错了什么,这是我的代码:

<html>
<head>
<title>Result</title>
</head>
<body>
<FORM   METHOD=GET ACTION="EnterResult.jsp">
Enter your home team:
<INPUT  TYPE="text"  NAME="newhomet" VALUE = "" >
Enter your away team:
<INPUT  TYPE="text"  NAME="newawayt" VALUE = "" >
Enter your home score:
<INPUT  TYPE="text"  NAME="newhomes" VALUE = "" >
Enter your away score:
<INPUT  TYPE="text"  NAME="newaways" VALUE = "" >
<INPUT  TYPE="submit"  VALUE = "Submit">
</FORM>
</body>
</html>

输入结果.jsp

<HTML>
<%@ page import="java.util.*"  import="java.awt.*" import= "javax.swing.*" import= "java.sql.*" import= "java.util.* " import= "matchBean.*"  errorPage= "NewError.jsp" %>
<jsp:useBean id="newMatchBean" class="matchBeans.MatchResults" />

<%
String newHomeTeam;
String newAwayTeam;
String newHomeScore;
String newAwayScore;
newHomeTeam = request.getParameter("newhomet");
newAwayTeam = request.getParameter("newawayt");
newHomeScore = request.getParameter("newhomes");
newHomeScore = request.getParameter("newaways");    
%>  
</HTML>

匹配结果.java

package matchBeans;
import java.sql.*;
import java.util.*;
public class MatchResults
{
    private static Connection connection = null;
    private static Statement statement;
    private ResultSet results;
    private String query;
    private String newHomeTeam;
    private String newAwayScore;
    private String newHomeScore;
    private String newAwayTeam;

    public MatchResults() throws ClassNotFoundException
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch (ClassNotFoundException cnfEx)
        {
            throw new ClassNotFoundException(
                        "Unable to locate JDBC driver!");
        }
    }

    public void newMatchBean() throws SQLException, ClassNotFoundException
    {

        connectAndCreateStatement();
        String query = "INSERT INTO Results VALUES('" + newHomeTeam + "','"
                + newAwayTeam + "','" + newHomeScore + "','" + newAwayScore
                + "')";
        statement.executeUpdate(query);

        System.out.println("\nContents after insertion:\n");
        disconnectFromDb();

    }

    private static void connectAndCreateStatement() throws SQLException,ClassNotFoundException
    {
        try
        {
            connection = DriverManager.getConnection(
                                    "jdbc:odbc:FootballData","","");
        }
        catch (SQLException sqlEx)
        {
            throw new SQLException("Unable to connect to database!");
        }

        try
        {
            statement = connection.createStatement();
        }
        catch (SQLException sqlEx)
        {
            throw new SQLException("Unable to create SQL statement!");
        }
    }


    private static void disconnectFromDb() throws SQLException
    {
        try
        {
            connection.close();
        }
        catch (SQLException sqlEx)
        {
            throw new SQLException(
                    "Unable to disconnect from database!");
        }
    }
}
4

1 回答 1

1

您永远不会在MatchResult.javafrom中设置值EnterResult.jsp

您需要newMatchBean()将四个变量作为参数,然后从EnterResult.jsp.

我无法对此进行测试,但它应该可以工作:

输入结果.jsp

...
<%
String newHomeTeam;
String newAwayTeam;
String newHomeScore;
String newAwayScore;
newHomeTeam = request.getParameter("newhomet");
newAwayTeam = request.getParameter("newawayt");
newHomeScore = request.getParameter("newhomes");
newHomeScore = request.getParameter("newaways");

newMatchBean.newMatchBean(newHomeTeam, newAwayTeam, newHomeScore, newAwayScore);
%>
...

匹配结果.java

...
public void newMatchBean(String newHomeTeam, String newAwayTeam, String newHomeScore, String newAwayScore) throws SQLException, ClassNotFoundException
{
    ...

参考: http: //leejeok.wordpress.com/2007/08/11/jsp-and-javabeans-passing-parameter/

于 2013-05-12T01:54:10.773 回答