0

我试图让某人将匹配结果输入到 html 页面中,然后该数据将输入到数据库中,但我无法插入任何数据,我错过了什么?

这是我的代码:

输入结果.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>

匹配结果.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 代码中的 action 属性。其次,您忘记读取从 html 页面发送到您的 servlet/jsp 的值。在创建查询之前尝试添加这些行:

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

您是否尝试在表单方法中添加双引号?

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

除此之外,我没有看到任何检索 GET 传递的参数的代码。您没有将变量分配给 GET 中的值。:)

于 2013-05-11T19:11:05.523 回答