How to remove the database name as the prefix of a table within a query.
The following query works:
select USER_ID, NAME, STATUS from CSBK.dbo.T_USERS;
But I would like to know how to get this one to work without the database name:
select USER_ID, NAME, STATUS from dbo.T_USERS;
Otherwise I'm getting the following error
com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name
UPDATE
connection = DriverManager.getConnection("jdbc:sqlserver://SOMETHING.COM:1438", "CSBK", "aPassword");
ideally i would like to use only the table name without any prefix but it seems the scheme name is required by sql server.
msdn
Connect to a named database on a remote server:
jdbc:sqlserver://localhost;databaseName=AdventureWorks
which will be in your case:
jdbc:sqlserver://SOMETHING.COM:1438;databaseName=CSBK
I am trying to use hsqldb-2.3.4 to connect from Spring applicastion.
I created data base using the following details
Type : HSQL Database Engine Standalone
Driver: org.hsqldb.jdbcDriver
URL: jdbc:hsqldb:file:mydb
UserName: SA
Password: SA
I created a table named ALBUM under "MYDB" schema
In spring configuration file:
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dbcpDataSource" />
</bean>
<bean id="dbcpDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:file:mydb" />
<property name="username" value="SA" />
<property name="password" value="SA" />
</bean>
And in my spring controller I am doing jdbcTemplate.query("SELECT * FROM MYDB.ALBUM", new AlbumRowMapper());
And It gives me exception:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT * FROM MYDB.ALBUM]; nested exception is java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: ALBUM
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
If I execute same query through SQL editor of hsqldb it executes fine. Can you please help me with this.
As said by a previous response there is many possible causes. One of them is that the table isn't created because of syntax incompatibility. If specific DB vendor syntax or specific capability is used, HSQLDB will not recognize it. Then while the creation code is executed you could see that the table is not created for this syntax reason. For exemple if the entity is annoted with #Column(columnDefinition = "TEXT") the creation of the table will fail.
There is a work around which tell to HSQLDB to be in a compatible mode
for pgsl you should append your connection url with that
"spring.datasource.url=jdbc:hsqldb:mem:testdb;sql.syntax_pgs=true"
and for mysql with
"spring.datasource.url=jdbc:hsqldb:mem:testdb;sql.syntax_mys=true"
oracle
"spring.datasource.url=jdbc:hsqldb:mem:testdb;sql.syntax_ora=true"
note there is variant depending on your configuration
it could be hibernate.connection.url= or spring.datasource.url=
for those who don't use the hibernate schema creation but a SQL script you should use this kind of syntax in your script
SET DATABASE SQL SYNTAX ORA TRUE;
It will also fix issues due to vendor specific syntax in SQL request such as array_agg for posgresql
Nota bene : The the problem occurs very early when the code parse the model to create the schema and then is hidden in many lines of logs, then the unitTested code crash with a confusing and obscure exception "user lacks privilege or object not found error" which does not point the real problem at all. So make sure to read all the trace from the beginning and fix all possible issues
If you've tried all the other answers on this question, then it is most likely that you are facing a case-sensitivity issue.
HSQLDB is case-sensitive by default. If you don't specify the double quotes around the name of a schema or column or table, then it will by default convert that to uppercase. If your object has been created in uppercase, then you are in luck. If it is in lowercase, then you will have to surround your object name with double quotes.
For example:
CREATE MEMORY TABLE "t1"("product_id" INTEGER NOT NULL)
To select from this table you will have to use the following query
select "product_id" from "t1"
user lacks privilege or object not found can have multiple causes, the most obvious being you're accessing a table that does not exist. A less evident reason is that, when you run your code, the connection to a file database URL actually can create a DB. The scenario you're experiencing might be you've set up a DB using HSQL Database Manager, added tables and rows, but it's not this specific instance your Java code is using. You may want to check that you don't have multiple copies of these files: mydb.log, mydb.lck, mydb.properties, etc in your workspace. In the case your Java code did create those files, the location depends on how you run your program. In a Maven project run inside Netbeans for example, the files are stored alongside the pom.xml.
I had the error user lacks privilege or object not found while trying to create a table in an empty in-memory database.
I used spring.datasource.schema and the problem was that I missed a semicolon in my SQL file after the "CREATE TABLE"-Statement (which was followed by "CREATE INDEX").
I had similar issue with the error 'org.hsqldb.HsqlException: user lacks privilege or object not found: DAYS_BETWEEN' turned out DAYS_BETWEEN is not recognized by hsqldb as a function. use DATEDIFF instead.
DATEDIFF ( <datetime value expr 1>, <datetime value expr 2> )
When running a HSWLDB server. for example your java config file has:
hsql.jdbc.url = jdbc:hsqldb:hsql://localhost:9005/YOURDB;sql.enforce_strict_size=true;hsqldb.tx=mvcc
check to make sure that your set a server.dbname.#. for example my server.properties file:
server.database.0=eventsdb
server.dbname.0=eventsdb
server.port=9005
I was inserting the data in hsql db using following script
INSERT INTO Greeting (text) VALUES ("Hello World");
I was getting issue related to the Hello World object not found and HSQL database user lacks privilege or object not found error
which I changed into the below script
INSERT INTO Greeting (text) VALUES ('Hello World');
And now it is working fine.
Add these two extra properties:
spring.jpa.hibernate.naming.implicit-strategy=
org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=
org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
I bumped into kind of the same problem recently. We are running a grails application and someone inserted a SQL script into the BootStrap file (that's the entry point for grails). That script was supposed to be run only in the production environment, however because of bad logic it was running in test as well. So the error I got was:
User lacks privilege or object not found:
without any more clarification...
I just had to make sure the script was not run in the test environment and it fixed the problem for me, though it took me 3 hours to figure it out. I know it is very, very specific issue but still if I can save someone a couple of hours of code digging that would be great.
I was having the same mistake. In my case I was forgetting to put the apas in the strings.
I was doing String test = "inputTest";
The correct one is String test = "'inputTest'";
The error was occurring when I was trying to put something in the database
connection.createStatement.execute("INSERT INTO tableTest values(" + test +")";
In my case, just put the quotation marks ' to correct the error.
In my case the error occured because i did NOT put the TABLE_NAME into double quotes "TABLE_NAME" and had the oracle schema prefix.
Not working:
SELECT * FROM FOOSCHEMA.BAR_TABLE
Working:
SELECT * FROM "BAR_TABLE"
had this issue with concatenating variables in insert statement. this worked
// var1, var3, var4 are String variables
// var2 and var5 are Integer variables
result = statement.executeUpdate("INSERT INTO newTable VALUES ('"+var1+"',"+var2+",'"+var3+"','"+var4+"',"+var5 +")");
In my case the issue was caused by the absence (I'd commented it out by mistake) of the following line in persistence.xml:
<property name="hibernate.hbm2ddl.auto" value="update"/>
which prevented Hibernate from emitting the DDL to create the required schema elements...
(different Hibernate wrappers will have different mechanisms to specify properties; I'm using JPA here)
I had this error while trying to run my application without specifying the "Path" field in Intellij IDEA data source (database) properties. Everything else was configured correctly.
I was able to run scripts in IDEA database console and they executed correctly, but without giving a path to the IDEA, it was unable to identify where to connect, which caused errors.
You have to run the Database in server mode and connect.Otherwise it wont connect from external application and give error similar to user lacks privilege.
Also change the url of database in spring configuration file accordingly when running DB in server mode.
Sample command to run DB in server mode
$java -cp lib/hsqldb.jar org.hsqldb.server.Server --database.0 file:data/mydb --dbname.0 Test
Format of URL in configuration file
jdbc:hsqldb:hsql://localhost/Test
In my case, one of the columns had the name 'key' with the missing #Column(name = "key") annotation so that in the logs you could see the query that created the table but in fact it was not there. So be careful with column names
For what it's worth - I had this same problem. I had a column named 'TYPE', which I renamed to 'XTYPE' and a column named ORDER which I renamed to 'XORDER' and the problem went away.
Yet another reason could be a misspelt field name. If your actual table has an id column named albumid and you'd used album_id, then too this could occur.
As another anwer remarked, case differences in field names too need to be taken care of.
I faced the same issue and found there was more than one PersistenceUnit (ReadOnly and ReadWrite) , So the tables in HSQLDDB created using a schema from one persistence unit resulted in exception(HSQL database user lacks privilege or object not found error) being thrown when accessed from other persistence unit .It happens when tables are created from one session in JPA and accessed from another session
In my case the table MY_TABLE was in the schema SOME_SCHEMA. So calling select/insert etc. directly didn't work. To fix:
add file schema.sql to the resources folder
in this file add the line CREATE SCHEMA YOUR_SCHEMA_NAME;
We are working on extracting database DDL of Sybase ASE 15.5 version. We have one sample created in java that execute ddl commands. We got ddl using ddlgen utility provided by Sybase with commands :
java -cp "mypath/lib/jconn4.jar;mypath/lib/dsparser.jar;mypath/lib/DDLGen.jar" com.sybase.ddlgen.DDLGenerator -Usa -Pmypassword -S192.123.13.111:5000 -Dmaster
Above command generate DDL for all database objects exist in user sa, also we get list of objects shared to other user by user sa as :
Grant Select on dbo.mytable1 to anotheruserthansa
go
Grant Select on dbo.mytable2 to anotheruserthansa
go
Now we want DDL of shared objects by username, like in my case user is anotheruserthansa
We need some command that can generate DDL for user, we might not have its password. What we need to get DDL using my user sa, its password and with schema name : anotheruserthansa we need to generate ddl for all the database object those are shared by user sa to anotheruserthansa.
Like we do in Oracle with query :
select object_type from dba_objects where owner = ''anotheruserthansa' and object_name = 'anotheruserthansa'
How can we get DDL for shared object using its schema?
This would likely be done with a combination of a SQL Query, with parameters then passed to ddlgen
SELECT db_name()
, su.name
, so.type
, so.name
FROM sysusers su, sysobjects so
WHERE su.uid = so.uid
AND su.name = "[name of user you are looking for]"
You can then pass the dbname, object type, and object name into ddlgen to generate the DDL.
ddlgen -Usa -Sservername -Tobject_type -Nobject_name -Ddbname ...etc
FYI - Sybase/SAP best practices recommends against allowing users to own objects within the database. It's recommended that all database objects be owned by dbo This is not to be confused with the concept of schemas used by Oracle and SQL Server. ASE does not use those kinds of logical divisions.
I have analyzing an code of application implemented Java SE (Spring) and Oracle 11g. In XXXDaoImpl class there are many queries writte as
select * from PREFIX.TableName
I have created a schema in oracle 11g via Oracle SQL Developer and imported all tables successfully.
But when an application tries to call any procedure it call MYPREFIX extention lets say
select * from MYPREFIX.TableName
I would like to know how to change the MYPREFIX name in SQL Developer to get code and newly created schema similat each other in order to run the application properly.
I am currently having following error.
ERROR 30 May 2013 18:06:02,036 [ChangeDeleteMHandler] [ChangeDeleteMHandler]: Error during change/delete msisdn procedure call
org.springframework.jdbc.UncategorizedSQLException: CallableStatementCallback; uncategorized SQLException for SQL [{call TABLENAME.changeM()}]; SQL state [72000]; error code [4063]; ORA-04063: package body "PREFIX.TABLENAME" has errors
ORA-06508: PL/SQL: could not find program unit being called: "PREFIX.TABLENAME"
ORA-06512: at line 1
; nested exception is java.sql.SQLException: ORA-04063: package body "PREFIX.TABLENAME" has errors
ORA-06508: PL/SQL: could not find program unit being called: "PREFIX.TABLENAME"
ORA-06512: at line 1
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:83)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:969)
at org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:1003)
at org.springframework.jdbc.object.StoredProcedure.execute(StoredProcedure.java:125)
Chances are PREFIX and MYPREFIX as Oracle Schema users, so in your scenario your application is accessing tables under the PREFIX schema and you've created a new MYPREFIX schema. If that's the case, you could grant privileges to your schema by running under the PREFIX:
GRANT SELECT, INSERT, UPDATE, DELETE ON YOUR_TABLE
TO MYPREFIX;
I'm trying to connect to a sql server 2005 database via JDBC.
I get the error:
com.microsoft.sqlserver.jdbc.SQLServerException: The SELECT permission
was denied on the object 'MyTable', database 'MyDatabase', schema
'dbo'.
The schema I use to connect is "MyUser". How do I connect using MyUser as opposed to dbo?
Thanks!
To clear things up: You connect to SQL Server using a user, not a schema. You don't say what version of SQL Server you're connecting to, but it used to be the case that the two were equivalent. As of 2005+, that is no longer true.
dbo is the default schema (think of it as a namespace); what the error message is telling you is the user you are connecting with (If I understand correctly, that's MyUser) does not have permission to SELECT from the MyTable table, which is part of the dbo schema in the MyDatabase database.
The first thing to do is confirm whether or not the user you're connecting with does or does not have SELECT permissions on that table. The second thing to do is, if it doesn't, either give MyUser that permission or use a different user to perform the SELECT statement.
i found that you have to specify your schema in your POJOS definitions.
In my case I got the same trouble using JPA (Entities / Annotations) and I realized that specifing the schema property in the #Table annotation works.
for example:
#Table(name = "address", **schema="*dbo*"**, catalog = "petcatalog")
I hope this helps you.