我正在尝试创建一个程序,用户可以在其中将匹配结果输入到 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!");
}
}
}