Caused By Org.postgresql.util.psqlexception Error Syntax Error At Or Near $1
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business syntax error at or near $1 postgres Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation
Org.postgresql.util.psqlexception: Error: Syntax Error At Or Near ")" Hibernate
Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like sql error: 0, sqlstate: 42601 you, helping each other. Join them; it only takes a minute: Sign up org.postgresql.util.PSQLException: ERROR: syntax error at or near “$1” up vote 4 down vote favorite I am getting this PSQLException: org.postgresql.util.PSQLException: ERROR: syntax error at pq syntax error at or near $1 or near "$1" Position: 37 When I run the following code: ps = connection.prepareStatement("SELECT current_timestamp + INTERVAL ?;"); ps.setString(1, "30 minutes"); System.out.println(ps); rs = ps.executeQuery(); However, the println function displays this in the console: SELECT current_timestamp + INTERVAL '30 minutes' Anyone know what is wrong? The query in the console runs fine in pgAdmin so I know it isn't a syntax error. java postgresql jdbc share|improve this question edited Oct 10 '15 at 20:33 BalusC 682k20324712691 asked Oct 10 '15 at 20:32 Kevin Orriss 411616 Makes no difference, the semicolon is perfectly valid –Kevin Orriss Oct 10 '15 at 20:34 the query works for me, is this is the actual code or do you have any variable in place of "30 minutes" –silentprogrammer Oct 10 '15 at 20:41 This is the actual code... Are you running postgres? –Kevin Orriss Oct 10 '15 at 20:42 There is a bug with setting strings as an interval, I am sure of it... setString works for other strings and setInt works too... –Kevin Orriss Oct 10 '15 at 20:51 It seems to be postgresql bug –herry Oct 10 '15 at 20:58 | show 3 more comments 2 Answers 2 active oldest votes up vote 5 down vote accepted Although the syntax INTERVAL '30 minutes' is valid when you write SQL directly in a console, it is actually considered to be an interval literal and won't work where the string that follows the word INTERVAL is not a literal string. Prepared statements in PostgreSQL are implemented on the server side using PREPARE and each ? is seen as an actual variable on the server. This is also why it complains about $1 although you never wrote a $ in your statement. Therefore, literal syntax d
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million http://stackoverflow.com/questions/33058684/org-postgresql-util-psqlexception-error-syntax-error-at-or-near-1 programmers, just like you, helping each other. Join them; it only takes a minute: Sign up org.postgresql.util.PSQLException: ERROR: syntax error at or near “(” up vote 0 down vote favorite I have craeted a Web Application that can be deployed on Heroku by maven eclipse. Group Id: org.glassfish.jersey.archetypes Artifact Id: jersey-heroku-webapp version: 2.17 I tested http://stackoverflow.com/questions/30216175/org-postgresql-util-psqlexception-error-syntax-error-at-or-near the appication on the localhost and POSTMAN and it works fine. I pushed it to heroku to test it on servlet container but I am getting 520 OK 520 it is just a number that I return in the SQLEXCEPTION. IN the Heroku log I found this error: 2015-05-13T13:10:37.364388+00:00 app[web.1]: at java.lang.Thread.run(Thread.j ava:745) 2015-05-13T13:10:37.389547+00:00 app[web.1]: org.postgresql.util.PSQLException: ERROR: syntax error at or near "(" 2015-05-13T13:10:37.389560+00:00 app[web.1]: Position: 45 2015-05-13T13:10:37.389740+00:00 app[web.1]: at org.postgresql.core.v3.QueryE xecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102) Database class: public class Database { public Database() { } public void drivercConnection() { try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("jar works :) "); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static Connection getConnection() throws URISyntaxException, SQLException { URI dbUri = new URI(System.getenv("DATABASE_URL")); String username = dbUri.getUserInfo().split(":")[0]; String password = dbUri.getUserInfo().split(":")[1]; String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath(); Connection con = DriverManager.getConnection(dbUrl, username, password); return con; } public int insertData(String mac, int route, double latD, double longD) { int status = 201
log in tour help Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings http://dba.stackexchange.com/questions/103980/syntax-error-using-preparedstatements-with-postgresql and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Database Administrators Questions Tags Users Badges Unanswered Ask Question _ Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the syntax error community. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Syntax error using preparedstatements with postgresql up vote 1 down vote favorite I have a Java application connecting to a PostgreSQL database. Using straightforward code like: ps = con.prepareStatement(sql); syntax error at if (dataTypes != null && dataTypes.size() > 0) { for (int i = 0; i < dataTypes.size(); ++i) { if (dataTypes.get(i) == DataTypes.NUMERIC) ps.setLong((i + 1), Long.parseLong(values.get(i))); else ps.setString((i + 1), values.get(i)); } } rs = ps.executeQuery(); and with a query like select ? from dummy it just works fine. In the postgres logs what shows is: 2015-06-13 12:48:30 EEST [28294-3] xx LOG: execute