0

誰かがマッチ結果を HTML ページに入力できるようにしようとしています。そのデータはデータベースに入力されますが、挿入するデータを取得できません。何が欠けていますか?

ここに私のコードがあります:

EnterResults.html

<html>
<head>
<title>Result</title>
</head>
<body>

<FORM   METHOD=GET ACTION="MatchResult.java">
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>

MatchResult.jsp

package results;
import java.sql.*;
import java.util.*;
public class MatchResult 
{

private static final String newHomeTeam = null;
private static final String newAwayTeam = null;
private static final String newHomeScore = null;
private static final String newAwayScore = null;
private static Statement statement;
private Connection connection;
public MatchResult() throws ClassNotFoundException, SQLException 
{
    try 
    {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    } catch (ClassNotFoundException cnfEx)
    {
        throw new ClassNotFoundException("Unable to locate JDBC driver!");
    }

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

    System.out.println("\nContents after insertion:\n");
}
catch(SQLException sqlEx)
{
    System.out.println("* Cannot execute insertion! *");
    sqlEx.printStackTrace();
    System.exit(1);
}
disconnectFromDb();
}



private 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 void disconnectFromDb() throws SQLException
{
    try 
    {
        connection.close();
    } catch (SQLException sqlEx) 
    {
        throw new SQLException("Unable to disconnect from database!");
    }
}
}
4

2 に答える 2

0

まず、web.xml の URL マッピングと html コードのアクション属性を確認します。次に、html ページからサーブレット/jsp に送信された値を読み取るのを忘れていました。クエリを作成する前に、次の行を追加してみてください。

newHomeTeam = request.getParameter("newhomet");
newAwayTeam  = request.getParameter("newawayt");
newHomeScore = request.getParameter("newhomes");
newAwayScore = request.getParameter("newaways");
于 2013-05-11T18:52:02.910 に答える
0

Did you try putting double quotes in your form method?

<FORM METHOD="GET" ACTION="MatchResult.java">

Aside from that, I don't see any code which retrieves the parameters passed by GET. You did not assign your variables to the values in GET. :)

于 2013-05-11T19:11:05.523 に答える