本文介绍了org.postgresql.util.PSQLException:错误:语法错误处于或接近“(”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



组ID:org.glassfish.jersey.archetypes

$ b我已经制作了一个可通过maven eclipse在Heroku上部署的Web应用程序。
$ b

神器Id:jersey-heroku-webapp

版本:2.17

我测试了本地主机和 POSTMAN 的应用程序,它工作正常。我把它推到了heroku上,在servlet容器上测试它,但是我得到了 520 OK 520这只是一个我在SQLEXCEPTION中返回的数字。在Heroku日志中,我发现了这个错误:

  2015-05-13T13:10:3​​7.364388 + 00:00 app [web。 1]:在java.lang.Thread.run(Thread.j 
ava:745)
2015-05-13T13:10:3​​7.389547 + 00:00 app [web.1]:org.postgresql .util.PSQLException:
语法错误:(
2015-05-13T13:10:3​​7.389560 + 00:00 app [web.1]:Position:45
2015-05-13T13:10:3​​7.389740 + 00:00 app [web.1]:at org.postgresql.core.v3.QueryE
xecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)

数据库类:

  public class Database {


public Database(){

}

public void drivercConnection(){
try {
Class.forName(com.mysql.jdbc.Driver);
System.out.println(jar works :));
$ b $ catch(ClassNotFoundException e){
// TODO自动生成的catch块
e.printStackTrace();


$ b $ 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();

连接con = DriverManager.getConnection(dbUrl,username,password);
return con;
}

public int insertData(String mac,int route,double latD,double longD){
int status = 201;

drivercConnection();

尝试{
Connection con = null;
尝试{
con = getConnection();
} catch(URISyntaxException e){
// TODO自动生成的catch块
e.printStackTrace();
}


//创建语句
语句stt = con.createStatement();

DatabaseMetaData dbm = con.getMetaData();

ResultSet tables = dbm.getTables(null,null,bus,null);

if(tables.next()){
// stt.execute(ALTER TABLE bus AUTO_INCREMENT = 1);

返回insertRecord(mac,route,latD,longD,status,con);
$ b} else {
//创建总线表
stt.execute(CREATE TABLE IF NOT EXISTS bus
+(id INT(11)NOT NULL AUTO_INCREMENT PRIMARY KEY,
+mac VARCHAR(30)NOT NULL UNIQUE,
+route int(11)NOT NULL,
+纬度FLOAT(10,6)NOT NULL,
+经度FLOAT(10,6)NOT NULL,
+created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP));

stt.execute(CREATE EVENT IF NOT EXISTS AutoDelete
+ON SCHEDULE EVERY 3 MINUTE
+DO
+DELETE FROM bus WHERE created_at<(NOW() - INTERVAL 3 MINUTE));

stt.execute(SET GLOBAL event_scheduler = ON);

first_data_insert(mac,route,latD,longD,con);

}
返回状态;
} catch(SQLException e){
// TODO自动生成的catch块
e.printStackTrace();
返回状态= 520;


$ b


解决方案

表示sql查询不正确。你可能想把它改成这样的东西。

  DROP TABLE IF EXISTS bus; 

CREATE TABLE总线(
id序列主键,
mac VARCHAR(30)NOT NULL UNIQUE,
route int NOT NULL,
latitude numeric( 10,6)NOT NULL,
经度数值(10,6)NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);

请注意,create语句(afaik)不是一个sql标准命令。所以,因为您使用的是postgresql,所以您需要将其更改为postgresql create语句。

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 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;

        drivercConnection();

        try {
            Connection con = null;
            try {
                con = getConnection();
            } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            // Create a statement
            Statement stt = con.createStatement();

            DatabaseMetaData dbm = con.getMetaData();

            ResultSet tables = dbm.getTables(null, null, "bus", null);

            if (tables.next()) {
                // stt.execute("ALTER TABLE bus AUTO_INCREMENT = 1");

                return insertRecord(mac, route, latD, longD, status, con);

            } else {
                // Create bus table
                stt.execute("CREATE TABLE IF NOT EXISTS bus"
                        + "(id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,"
                        + "mac VARCHAR(30) NOT NULL UNIQUE,"
                        + "route int(11) NOT NULL,"
                        + "latitude FLOAT(10,6) NOT NULL,"
                        + "longitude FLOAT(10,6) NOT NULL,"
                        + "created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");

                stt.execute("CREATE EVENT IF NOT EXISTS  AutoDelete "
                        + "ON SCHEDULE EVERY 3 MINUTE "
                        + "DO "
                        + "DELETE FROM bus WHERE created_at < (NOW() - INTERVAL 3 MINUTE)");

                stt.execute("SET GLOBAL event_scheduler = ON");

                first_data_insert(mac, route, latD, longD, con);

            }
            return status;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return status = 520;

        }
    }
解决方案

that indicate the sql query is not right. you might want to change it into something like this.

DROP TABLE IF EXISTS bus;

CREATE TABLE bus( 
id SERIAL PRIMARY KEY,
mac VARCHAR(30) NOT NULL UNIQUE,
route int NOT NULL,
latitude numeric(10,6) NOT NULL,
longitude numeric(10,6) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);

Note that create statement (afaik) is not a sql standard command. So, because you're using postgresql, you need to change it into postgresql create statement.

这篇关于org.postgresql.util.PSQLException:错误:语法错误处于或接近“(”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 08:38