Java SQL“ERROR:Relation”Table_Name"不存在“ [英] Java SQL "ERROR: Relation "Table_Name" does not exist"
问题描述
我正在尝试将netbeans连接到我的postgresql数据库。连接似乎有效,因为我在连接时没有出现任何错误或异常,getCatalog()等方法也会返回正确答案。
I'm trying to connect netbeans to my postgresql database. The connection seems to have worked as I don't get any errors or exceptions when just connecting, methods such as getCatalog() also return the correct answers.
但是当我尝试运行一个简单的SQL语句我收到错误错误:关系TABLE_NAME不存在,其中TABLE_NAME是我的任何一个表,它存在于数据库中。这是我的代码:
But when I try to run a simple SQL statement I get the error "ERROR: relation "TABLE_NAME" does not exist", where TABLE_NAME is any one of my tables which DO exist in the database. Here's my code:
Statement stmt = con.createStatement();
ResultSet rs;
String query = "SELECT * FROM clients";
rs = stmt.executeQuery(query);
我以为netbeans可能找不到表,因为它没有查看默认模式(public ),有没有办法在java中设置架构?
I was thinking that netbeans might not be finding the tables because it's not looking in the default schema (public), is there a way of setting the schema in java?
编辑:我的连接代码。数据库名称是Cinemax,当我省略语句代码时,我没有错误。
My connection code. The database name is Cinemax, when I leave out the statement code, I get no errors.
String url = "jdbc:postgresql://localhost:5432/Cinemax";
try{
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe) {
System.err.println("Couldn't find driver class:");
cnfe.printStackTrace();
}
Connection con = DriverManager.getConnection( url,"postgres","desertrose147");
推荐答案
我怀疑你使用双引号创建表格,例如Clients
或其他一些大写/小写字符的组合,因此表名现在区分大小写。
I suspect you created the table using double quotes using e.g. "Clients"
or some other combination of upper/lowercase characters and therefor the table name is case sensitive now.
声明是什么
SELECT table_schema, table_name
FROM information_schema.tables
WHERE lower(table_name) = 'clients'
返回?
如果返回的表名不是小写,在引用它时必须使用双引号,如下所示:
If the table name that is returned is not lowercase you have to use double quotes when referring to it, something like this:
String query = "SELECT * FROM \"Clients\"";
这篇关于Java SQL“ERROR:Relation”Table_Name"不存在“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!