WDS*_*WDS 2 java sql postgresql jdbc
这是我在这里的第一篇文章,如果我的格式不正确/难以阅读,我将对其进行更改。请告诉我。
我一直在尝试使用用户输入数据将JDBC添加到数据库的JDBC。用户提供名字和姓氏,电子邮件,并使用随机函数生成用户ID。
该数据库是使用postgreSQL创建的。我正在尝试添加到名为accounts的表中,该表包含以下列-user_id(整数),first_name(varchar(100)),last_name(varchar(100)),电子邮件(varchar(500))。
我的程序能够成功连接到数据库,但是无法将数据添加到表中。
在以下代码中,firstName,lastName和eMail均为字符串,而sID为int。
state = conx.prepareStatement("INSERT INTO accounts VALUES ("+ sID +","+ firstName + "," + lastName + "," + eMail) + ")");
s.executeUpdate();
Run Code Online (Sandbox Code Playgroud)
通常,我希望将数据添加到表中以便我们可以每天调用它,但是我遇到了错误。
org.postgresql.util.PSQLException: ERROR: column "v" does not exist
Position: 36
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2183)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:308)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:143)
at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:120)
at Main.main(Main.java:49)
org.postgresql.util.PSQLException: ERROR: column "v" does not exist
Position: 36
Run Code Online (Sandbox Code Playgroud)
使用?的参数而不是将他们的价值观。另外,您应该在INSERT语句中命名列。例如:
s = conx.prepareStatement(
"INSERT INTO accounts (id, first_name, last_name, email) " +
"VALUES (?, ?, ?, ?)"
);
s.setInt(1, sID);
s.setString(2, firstName);
s.setString(3, lastName);
s.setString(4, email);
int affectedRows = s.executeUpdate();
Run Code Online (Sandbox Code Playgroud)