我试图让某人将匹配结果输入到 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!");
}
}
}