首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java - SQLException中的SQLException连接

Java - SQLException中的SQLException连接
EN

Stack Overflow用户
提问于 2013-08-21 07:44:27
回答 2查看 1.2K关注 0票数 0

我用postgresql创建一个数据库,我想连接并显示数据库。

代码语言:javascript
复制
    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(){

    }
}

我得到以下例外:

代码语言:javascript
复制
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)

我该如何解决这个问题?有人能帮我吗?谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-21 07:59:31

例如,当您有一个保留名称或表已经存在的名称时,就会发生这种情况。

假设您正在尝试添加一个新表,错误是: org.postgresql.util.PSQLException: ERROR:语法错误在"mytable“或”mytable“附近

您得到了此错误,因为当前架构中的postgresql已经没有mytable表或表,并且保留了这个名称。

将表名更改为不同的名称,然后尝试并确保创建了表。

票数 0
EN

Stack Overflow用户

发布于 2013-08-21 07:57:19

尝试使用schemaname.table_name前缀访问表。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18351615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档