Java 将数据输入数据库
我正在尝试制作一个程序,首先在数据库中显示所有结果,然后让某人输入匹配结果,但当我尝试插入数据时,我不断收到以下错误:Java 将数据输入数据库,java,sql,ms-access,sql-insert,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
* 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();
}
}
包结果;
导入java.sql.*;
导入java.util.*;
公共类UserEntryInsertion
{
私有静态语句;
专用静态连接;
公共静态void main(字符串[]args)
{
扫描仪键盘=新扫描仪(System.in);
弦乐队;
字符串newAwayTeam;
国际新家园中心;
int newAwayScore;
尝试
{
forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
connection=DriverManager.getConnection(
“jdbc:odbc:FootballData”,“”,“”);
}
捕获(ClassNotFoundException cnfEx)
{
System.out.println(“*无法加载驱动程序!*”);
系统出口(1);
}
捕获(SQLException sqlEx)
{
System.out.println(
“*无法连接到数据库!*”;
系统出口(1);
}
System.out.println(“\n初始表内容:\n”);
displayTableContents();
System.out.print(“\n按继续…”);
keyboard.nextLine();
系统输出打印(“\n新主队”);
newHomeTeam=keyboard.next();
系统输出打印(“\n新客场团队”);
newAwayTeam=keyboard.next();
系统输出打印(“\n新主分数”);
newHomeScore=keyboard.nextInt();
系统输出打印(“\n新分数”);
newAwayScore=keyboard.nextInt();
尝试
{
String insertion=“插入结果值(”
+新HomeTeam+“,”
+newAwayTeam+“,”
+newHomeScore+“,”
+newAwayScore+”;
语句。执行更新(插入);
System.out.println(“\n插入后的内容:\n”);
displayTableContents();
}
捕获(SQLException sqlEx)
{
System.out.println(“*无法执行插入!*”);
sqlEx.printStackTrace();
系统出口(1);
}
关闭();
}
静态void displayTableContents()
{
ResultSet results=null;
尝试
{
statement=connection.createStatement();
结果=statement.executeQuery(
“从结果中选择*”;
while(results.next())
{
System.out.println(“主队”+results.getString(1));
System.out.println(“客场团队:+results.getString(2));
System.out.println(“主分数:+results.getInt(3));
System.out.println(“客场得分:+results.getInt(4));
System.out.println();
}
}
捕获(SQLException sqlEx)
{
System.out.println(“*检索数据时出错!*”);
sqlEx.printStackTrace();
系统出口(1);
}
}
静态无效关闭()
{
尝试
{
connection.close();
}
捕获(SQLException sqlEx)
{
System.out.println(“*无法断开!*”);
sqlEx.printStackTrace();
}
}
}String insertion=“插入结果值(”
+新HomeTeam+“,”
+newAwayTeam+“,”
+newHomeScore+“,”
+newAwayScore+”;
有些字符串被引用(newAwayTeam/newHomeScore),而有些则没有。如果未加引号的字符串包含问号?
,您将得到显示的确切错误
但是,您应该真正使用的参数功能,它可以避免字符串中的引号之类的东西,这些东西可能会弄乱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);
(假设它们都应该是字符串,其他数据类型还有其他的set方法)newHomeTeam和newAwayScore没有“”。 试试这个:
String insertion = "INSERT INTO Results VALUES('"
+ newHomeTeam + "','"
+ newAwayTeam + "','"
+ newHomeScore + "','"
+ newAwayScore + "')";
参数太少
并不意味着必须插入更多字段?或者它是由“”
?@AzadOmer”引起的,这意味着语句需要一个参数(问号使它需要一个参数),并且由于SQL字符串本身中没有参数,它很可能来自键盘输入。您应该使用SQL INSERT语句,在其中提供列名和列值。您可以确保将值放在正确的列中。