将数据从html页面插入数据库

时间:2013-05-12 01:06:56

标签: java sql database jsp javabeans

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

EnterResult.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>

MatchResult.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!");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您永远不会在MatchResult.java的{​​{1}}中设置值。

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

我无法对此进行测试,但它应该有效:

EnterResult.jsp

EnterResult.jsp

MatchResult.java

...
<%
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);
%>
...

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