将数据输入数据库

时间:2013-05-11 11:58:41

标签: java sql ms-access sql-insert

我正在尝试制作一个程序,首先在数据库中显示所有结果然后让某人输入匹配结果,但是当我尝试插入数据时,我不断收到此错误:

* Cannot execute insertion! *
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3109)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:337)
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:287)
at results.UserEntryInsertion.main(UserEntryInsertion.java:57)

这是我的插入代码:

package results;
import java.sql.*;
import java.util.*;

public class UserEntryInsertion
{
    private static Statement statement;
    private static Connection connection;

    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        String newHomeTeam;
        String newAwayTeam;
        int newHomeScore;
        int newAwayScore;

        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connection = DriverManager.getConnection(
                                "jdbc:odbc:FootballData","","");
        }
        catch(ClassNotFoundException cnfEx)
        {
            System.out.println("* Unable to load driver! *");
            System.exit(1);
        }
        catch(SQLException sqlEx)
        {
        System.out.println(
                    "* Cannot connect to database! *");
        System.exit(1);
        }

    System.out.println("\nInitial table contents:\n");
    displayTableContents();
    System.out.print("\nPress <Enter> to continue...");
    keyboard.nextLine();

    System.out.print("\nNew Home Team ");
    newHomeTeam = keyboard.next();
    System.out.print("\nNew Away Team ");
    newAwayTeam = keyboard.next();
    System.out.print("\nNew Home Score ");
    newHomeScore = keyboard.nextInt();
    System.out.print("\nNew Away Score ");
    newAwayScore = keyboard.nextInt();

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

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

static void displayTableContents ()
{
    ResultSet results = null;

    try
    {
        statement = connection.createStatement();
        results = statement.executeQuery(
                        "SELECT * FROM Results");
        while (results.next())
        {
            System.out.println("Home Team"+ results.getString(1));
            System.out.println("Away Team:"+ results.getString(2)); 
            System.out.println("Home Score:"+ results.getInt(3));
            System.out.println("Away Score:" + results.getInt(4));
            System.out.println();
        }
    }
    catch(SQLException sqlEx)
    {
        System.out.println("* Error retrieving data! *");
        sqlEx.printStackTrace();
        System.exit(1);
    }
}

static void closeDown()
{
    try
    {
        connection.close();
    }
    catch(SQLException sqlEx)
    {
        System.out.println("* Unable to disconnect! *");
        sqlEx.printStackTrace();
    }
}

}

2 个答案:

答案 0 :(得分:1)

String insertion = "INSERT INTO Results VALUES("
                        + newHomeTeam + ",'"
                        + newAwayTeam + "','"
                        + newHomeScore + "',"
                        + newAwayScore + ")";

引用了一些字符串(newAwayTeam / newHomeScore),而有些不是。如果未加引号的字符串包含问号?,则会显示您正在显示的确切错误。

你应该真正使用PreparedStatement的参数功能,它可以帮助你避免使用字符串中的引号等可能搞乱SQL的东西,更多的东西;

String insertion = "INSERT INTO Results VALUES(?,?,?,?)";
statement.setString(1, newHomeTeam);
statement.setString(2, newAwayTeam);
statement.setString(3, newHomeScore);
statement.setString(4, newAwayScore);
statement.executeUpdate(insertion);

(假设它们都应该是字符串,还有其他数据类型的其他设置方法)

答案 1 :(得分:0)

newHomeTeam和newAwayScore没有''。 试试这个:

String insertion = "INSERT INTO Results VALUES('"
                    + newHomeTeam + "','"
                    + newAwayTeam + "','"
                    + newHomeScore + "','"
                    + newAwayScore + "')";