Вопрос

I create a database with postgresql and i would like to connect and display the database.

    public Connect(){

        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            System.err.println("ClassNotFoundException: Postgres Server JDBC");
        }

        try {
            conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres","postgres","postgrespassword");
            System.out.println("Connection successful.");
            sql = "select * from mytable";
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            md = rs.getMetaData();
            columns = md.getColumnCount();
            for (int i = 1; i <= columns; ++i){
                columnNames.addElement(md.getCatalogName(i));
            }
            while (rs.next()){
                Vector row = new Vector(columns);
                for(int i = 0; i <= columns; ++i){
                    row.addElement(rs.getObject(i));
                }
                data.addElement(row);
            }

            rs.close();
            stmt.close();
        } catch (SQLException e){
            System.err.println("SQLException");
        }
        JTable table = new JTable(data, columnNames)
        {
            public Class getColumnClass(int column)
            {
                for (int row = 0; row < getRowCount(); row++)
                {
                    Object o = getValueAt(row, column);

                    if (o != null)
                    {
                        return o.getClass();
                    }
                }

                return Object.class;
            }
        };
        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );

        JPanel buttonPanel = new JPanel();
        getContentPane().add( buttonPanel, BorderLayout.SOUTH );
    }

    public void closeConnection() throws SQLException {
        try {
            conn.close();
            System.out.println("Connection close.");
        } catch (SQLException e) {
            System.err.println("SQLException");
        }
    }

    public void displayDatabase(){

    }
}

I get the following exception:

org.postgresql.util.PSQLException: ERROR: syntax error at or near "mytable"
  Position: 15
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:403)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:283)
    at hu.aycm.Connect.<init>(Connect.java:37)
    at hu.aycm.Main.main(Main.java:8)

How can I solve this problem? Can someone help me? Thanks

Это было полезно?

Решение

This happen when you have a name which is a reserved name or already exists by a table for example.

Lets say your are trying to add a new table and the error was: org.postgresql.util.PSQLException: ERROR: syntax error at or near "mytable"

You got this error since the postgresql in the current schema has already a mytable table or table is not there and this name is reserved.

Change your table name to a different and then try out and make sure you have the table created.

Другие советы

try accessing the table by prefixing it with schemaname.table_name.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top