Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "{"

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "{"

xi nong
Can someone help me?I tried other way to fix this error.but failed.
1.1Envirment:
Hardware:Windows10+JDK 1.8;
Software:Java+Eclipse+Mybatis+maven3.6+tomcat8.0+Postgresql 10.6;
1.2 Project structure

jiegou.png



1.3 jar package
asm-3.3.1.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.1.jar
mysql-connector-java-5.1.20.jar
postgresql-42.2.5.jre7.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
2.problem:
when i run the test,the error come,I really need some advices.
hrer is my code:
3.User.java
package com.ironxi.entity;

public class User {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassward(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
}
}
4.UserMapping.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ironxi.entity.UserMapper">
  <select id="selectUser" resultType="com.ironxi.entity.User">
    SELECT * FROM public.use where id=#{id}
  </select>
  
  <insert id="addUser" parameterType="com.ironxi.entity.User" useGeneratedKeys="true">
    INSERT INTO public.use{name,password} VALUES(#{name},#{password})
   
  </insert>
</mapper> 
5.UserDao.java
package com.ironxi.dao;

import java.io.IOException;

import org.apache.ibatis.session.SqlSession;

import com.ironxi.entity.User;
import com.ironxi.util.MybatisUtil;

public class UserDao {
/**search*/
public User getById(int id) throws IOException{
SqlSession session = MybatisUtil.getSession();
User user = (User)session.selectOne("com.ironxi.entity.UserMapper.selectUser",1);
session.close();
return user;
}
/**add*/
public int add(User user) throws IOException{
SqlSession session = MybatisUtil.getSession();
int result=session.insert("com.ironxi.entity.UserMapper.addUser",user);
session.commit();
session.close();
return result;
}
}
6.Test.java
package com.ironxi.test;

import java.io.IOException;

import org.apache.ibatis.session.SqlSession;

import com.ironxi.dao.UserDao;
import com.ironxi.entity.User;
import com.ironxi.util.MybatisUtil;

public class Test {
public static void main(String[] args) throws IOException {
UserDao userDao= new UserDao();
//System.out.println(userDao.getById(1));
User user=new User();
user.setName("SaSon");
user.setPassward("12349");
System.out.println(userDao.add(user));
}
}
7.mybatis.conf.xml
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="UNPOOLED">
<property name="driver" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/postgres" />
<property name="username" value="postgres" />
<property name="password" value="Iron.336" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/ironxi/entity/UserMapper.xml" />
</mappers> 
</configuration>

8.Console:
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "{"
  Position: 23
### The error may involve com.ironxi.entity.UserMapper.addUser-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO public.use{name,password} VALUES(?,?)
### Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "{"
  Position: 23
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:154)
at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:141)
at com.ironxi.dao.UserDao.add(UserDao.java:21)
at com.ironxi.test.Test.main(Test.java:19)
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "{"
  Position: 23
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.execute(PgPreparedStatement.java:132)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:62)
at com.sun.proxy.$Proxy1.execute(Unknown Source)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:44)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:69)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:48)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:105)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:71)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:152)
... 3 more

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "{"

Guy Rouillier-2
Syntax error:

INSERT INTO public.use{name,password} VALUES(#{name},#{password})
                                                 ^                            ^
should be 

INSERT INTO public.user(name,password) VALUES(#{name},#{password})

--
Guy Rouillier

------ Original Message ------
From: "xi nong" <[hidden email]>
To: "mybatis-user" <[hidden email]>
Sent: 1/23/2019 8:42:47 AM
Subject: Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "{"

Can someone help me?I tried other way to fix this error.but failed.
1.1Envirment:
Hardware:Windows10+JDK 1.8;
Software:Java+Eclipse+Mybatis+maven3.6+tomcat8.0+Postgresql 10.6;
1.2 Project structure

jiegou.png



1.3 jar package
asm-3.3.1.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.1.jar
mysql-connector-java-5.1.20.jar
postgresql-42.2.5.jre7.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
2.problem:
when i run the test,the error come,I really need some advices.
hrer is my code:
3.User.java
package com.ironxi.entity;

public class User {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassward(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
}
}
4.UserMapping.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<mapper namespace="com.ironxi.entity.UserMapper">
  <select id="selectUser" resultType="com.ironxi.entity.User">
    SELECT * FROM public.use where id=#{id}
  </select>
  
  <insert id="addUser" parameterType="com.ironxi.entity.User" useGeneratedKeys="true">
    INSERT INTO public.use{name,password} VALUES(#{name},#{password})
   
  </insert>
</mapper> 
5.UserDao.java
package com.ironxi.dao;

import java.io.IOException;

import org.apache.ibatis.session.SqlSession;

import com.ironxi.entity.User;
import com.ironxi.util.MybatisUtil;

public class UserDao {
/**search*/
public User getById(int id) throws IOException{
SqlSession session = MybatisUtil.getSession();
User user = (User)session.selectOne("com.ironxi.entity.UserMapper.selectUser",1);
session.close();
return user;
}
/**add*/
public int add(User user) throws IOException{
SqlSession session = MybatisUtil.getSession();
int result=session.insert("com.ironxi.entity.UserMapper.addUser",user);
session.commit();
session.close();
return result;
}
}
6.Test.java
package com.ironxi.test;

import java.io.IOException;

import org.apache.ibatis.session.SqlSession;

import com.ironxi.dao.UserDao;
import com.ironxi.entity.User;
import com.ironxi.util.MybatisUtil;

public class Test {
public static void main(String[] args) throws IOException {
UserDao userDao= new UserDao();
//System.out.println(userDao.getById(1));
User user=new User();
user.setName("SaSon");
user.setPassward("12349");
System.out.println(userDao.add(user));
}
}
7.mybatis.conf.xml
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="UNPOOLED">
<property name="driver" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/postgres" />
<property name="username" value="postgres" />
<property name="password" value="Iron.336" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/ironxi/entity/UserMapper.xml" />
</mappers> 
</configuration>

8.Console:
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "{"
  Position: 23
### The error may involve com.ironxi.entity.UserMapper.addUser-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO public.use{name,password} VALUES(?,?)
### Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "{"
  Position: 23
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:154)
at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:141)
at com.ironxi.dao.UserDao.add(UserDao.java:21)
at com.ironxi.test.Test.main(Test.java:19)
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "{"
  Position: 23
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.execute(PgPreparedStatement.java:132)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:62)
at com.sun.proxy.$Proxy1.execute(Unknown Source)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:44)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:69)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:48)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:105)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:71)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:152)
... 3 more

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.