I have a project named 'Online Recruitment System'. There is problem in database connectivity.
First i made table 'registration' and 'clogindetails' without using quotes in sqlplus.
Then all data used to save properly, but on login I was getting following error:
java.sql.SQLException: ORA-00904: PASSWORD: invalid identifier
After this I read stackoverflow multiple examples. And I added "double quotes" to the table items in database and kept them in lowercase.
Now the data is not even getting saved. I tried to look in 'object browser' in 'data' tab and there was following error:
failed to parse SQL query:
ORA-00904: "pass": invalid identifier
As far I know the project is made all right. Only there is problem in making tables.
Here is code from one of the page which use table 'clogindetails':
String usrname=getClogid();
String pass=getCpassword();
if(usrname!=null && pass!=null && usrname.length()>0 && pass.length()>0)
{
ps = con.prepareStatement("select * from clogindetails where logid=? and password=?");
ps.setString(1,usrname);
ps.setString(2,pass);
rs=ps.executeQuery();
HttpSession session=request.getSession(true);
if(!rs.next())
{
errors.add("invalid", new ActionMessage("errors.invalidusername"));
}
}
rs.close();
ps.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
if(getClogid()==null || getClogid().length()<1)
{
errors.add("logid", new ActionMessage("errors.logid.required"));
}
if(getCpassword()==null || getCpassword().length()<1)
{
errors.add("password", new ActionMessage("errors.password.required"));
}
return errors;
the schema of clogindetails is
CREATE TABLE "CLOGINDETAILS"(
"ADMITID" NUMBER(15,0),
"NAME" VARCHAR2(25),
"LOGID" VARCHAR2(10),
"PASS" VARCHAR2(20)
)
ORA-00904 is common when invalid alias in a select statement is referred.
To overcome this error, you need enter a valid column name.
To avoid ORA-00904, column names cannot be a reserved word, and must contain these four criteria to be valid:
begin with a letter
be less than or equal to thirty characters
consist only of alphanumeric and the special characters ($_#); other characters need double quotation marks around them
For more info you can refer this link.
http://www.dba-oracle.com/t_ora_00904_string_invalid_identifier.htm
By looking at user exception error i could say that such type of exceptions are occurred when we are using different column names.
for ex:
i have created table in database i.e
create table tb_login(username varchar(10), password varchar(10));
and my java code is
ps=con.prepareStatement("select * from tb_login where username=? and pass=?");
//error bcz pass
This code will produce same exception bcz in my database second column name is password not pass.
so check your database schema and java code. or else share your database schema.
k now try this
ps=con.prepareStatement("select * from clogindetails where logid=? and pass=?");
rest of the code keep as it is..
Try to do this one:
Change your query
from: select * from clogindetails where logid=? and password=?
to: select * from clogindetails where logid='?' and password='?'
Point is that when you make queries, you should spot your identifiers with ' ', otherwise it seems like you are comparing some unknown table column's data (variables) with data from existing columns.
ex: select e.full_name from e.employees where e.full_name like '&Medet&'
otherwise you'll get an error.
Hope this will help you!
This error occurs when your column name not as same as you are writing in your query . and avoid to give column names which are Keyword of oracle , Group this cant be your column name or table name.
Related
This question already has answers here:
How To Handle Table Column Named With Reserved Sql Keyword?
(4 answers)
Closed 19 days ago.
I'm trying to add a column to my users table but I get an error every time I try to create a new user I end up getting the error "java.sql.SQLSyntaxErrorException: ORA-00904: "IMPORTED": invalid identifier".
In the database my column is created but I still get an error when i try to create a user.
I'm trying to create a column that receives true or false in my user table.
When I try to create a new user by jpa I end up getting an error
My Model:
#Column(name = "IMPORTED")
#Enumerated(EnumType.STRING)
private Eboolean imported;
My query:
alter table USER add IMPORTED VARCHAR2(1);
What you are saying is not (entirely) true. Table name certainly isn't user:
SQL> create table user (id number);
create table user (id number)
*
ERROR at line 1:
ORA-00903: invalid table name
Why? Because it is reserved for function that returns currently logged user:
SQL> select user from dual;
USER
--------------------------------------------------------------------------------
SCOTT
SQL>
Therefore, table name is something else; or, if it is user, then you must have created it by enclosing its name into double quotes, but then you have to use double quotes every time, matching letter case every time:
SQL> create table "user" (id number);
Table created.
Presuming that this is what you have, then:
SQL> alter table "user" add imported varchar2(1);
Table altered.
SQL>
Furthermore, error you specified is
ORA-00904: invalid identifier
If you got it when inserting a row into a table, it means that there's no such column in that table. Possible causes:
you misspelled its name
there's really no such column there
double quotes and letter case matching issue
Presuming that column exists (see above alter table), insert also works:
SQL> insert into "user" (id, imported) values (1, 0);
1 row created.
SQL> select * from "user";
ID IMPORTED
---------- ----------
1 0
SQL>
I am retrieving data from database using jdbc. In my code I am using 3-4 tables to get data. But sometimes if table is not present in database my code gives exception. How to handle this situation. I want my code to continue working for other tables even if one table is not present. Please help.
I have wrote a code like this
sql="select * from table"
now Result set and all.
If table is not present in database it give exception that no such table. I want to handle it. In this code I cannot take tables which are already present in advance . I want to check here itself if table is there or not.
Please do not mark it as a duplicate question. The link you shared doesnot give me required answer as in that question they are executing queries in database not through JDBC code
For Sybase ASE the easiest/quickest method would consist of querying the sysobjects table in the database where you expect the (user-defined) table to reside:
select 1 from sysobjects where name = 'table-name' and type = 'U'
if a record is returned => table exists
if no record is returned => table does not exist
How you use the (above) query is up to you ...
return a 0/1-row result set to your client
assign a value to a #variable
place in a if [not] exists(...) construct
use in a case statement
If you know for a fact that there won't be any other object types (eg, proc, trigger, view, UDF) in the database with the name in question then you could also use the object_id() function, eg:
select object_id('table-name')
if you receive a number => the object exists
if you receive a NULL => the object does not exist
While object_id() will obtain an object's id from the sysobjects table, it does not check for the object type, eg, the (above) query will return a number if there's a stored proc named 'table-name'.
As with the select/sysobjects query, how you use the function call in your code is up to you (eg, result set, populate #variable, if [not] exists() construct, case statement).
So, addressing the additional details provided in the comments ...
Assuming you're submitting a single batch that needs to determine table existence prior to running the desired query(s):
-- if table exists, run query(s); obviously if table does not exist then query(s) is not run
if exists(select 1 from sysobjects where name = 'table-name' and type = 'U')
begin
execute("select * from table-name")
end
execute() is required to keep the optimizer from generating an error that the table does not exist, ie, the query is not parsed/compiled unless the execute() is actually invoked
If your application can be written to use multiple batches, something like the following should also work:
# application specific code; I don't work with java but the gist of the operation would be ...
run-query-in-db("select 1 from sysobjects where name = 'table-name' and type = 'U'")
if-query-returns-a-row
then
run-query-in-db("select * from table-name")
fi
This is the way of checking if the table exists and drop it:
IF EXISTS (
SELECT 1
FROM sysobjects
WHERE name = 'a_table'
AND type = 'U'
)
DROP TABLE a_table
GO
And this is how to check if a table exists and create it.
IF NOT EXISTS (
SELECT 1
FROM sysobjects
WHERE name = 'a_table'
AND type = 'U'
)
EXECUTE("CREATE TABLE a_table (
col1 int not null,
col2 int null
)")
GO
(They are different because in table-drop a temporary table gets created, so if you try to create a new one you will get an exception that it already exists)
Before running the query which has some risk in table not existing, run the following sql query and check if the number of results is >= 1. if it is >= 1 then you are safe to execute the normal query. otherwise, do something to handle this situation.
SELECT count(*)
FROM information_schema.TABLES
WHERE (TABLE_SCHEMA = 'your_db_name') AND (TABLE_NAME = 'name_of_table')
I am no expert in Sybase but take a look at this,
exec sp_tables '%', '%', 'master', "'TABLE'"
Sybase Admin
I am trying to get rows from table using SQLDeveloper by writing simple query:
SELECT * FROM agreements WHERE agreementkey = 1;
SELECT * FROM agreements WHERE agreementkey = 4;
but getting invalid character encountered error. It's not a problem with query(working using other keys, i.e. agreementkey = 3) but with XMLType column in this table - there is something wrong with data in some rows. Is there a way to select this affected row(I know keys of this affected rows) using queries? Maybe export to file or something? Solution of copying value manually is not acceptable.
Create an empty copy of the table and then run an INSERT into it based on a select from the original table but do it using the DML error logging clause.
This should show you any rows that fail to load and the reason for the failure.
CREATE TABLE test_agreements
AS SELECT * FROM agreements
WHERE ROWNUM <1;
INSERT INTO test_agreements
SELECT *
FROM agreements
LOG ERRORS REJECT LIMIT UNLIMITED
This will create you an error logging table called ERR$TEST_AGREEMENTS which you can query to find the problem rows.
Problem is in WHERE key = 1 cause key is a Reserve Word in Oracle. You should escape it like
SELECT * FROM table WHERE "key" = 1;
KEY is a reserved word so to overcome that you need to use double quotes "".
SELECT * FROM table WHERE "key" = 1;
I think the problem can be solved by putting the argument in quotes:
SELECT * FROM agreements WHERE agreementkey = "1";
I wish I were familiar with XML, but I have run into this with VARCHAR2 columns that are supposed to have all numeric values. Oracle looks at the request
where agreementkey = 1
and tries to convert agreementkey to a number rather than 1 to a varchar2.
If the database contains invalid characters I would try one the following:
Maybe the solution of BriteSponge will work, using an insert statemant with error logging clause.
Use datapump to export the data to a file. I think the log will contain information to identify the invalid columns.
There was a tool called "character set scanner" that checked the characters of the data of a table, here is some documentation: CSSCAN. Or maybe you can use the Database Migration Assistent for Unicode (DMU) mentioned in the same manual.
4- You can write a small PL/SQL program that retrieves the rows row by row and in case of an error catches the exception and notifies you about the row.
DECLARE
invalid_character_detected EXCEPTION;
PRAGMA EXCEPTION_INIT(invalid_character_detected, ???); begin
for SELECT rowid into rec FROM agreements do begin
for
SELECT * into dummy
FROM agreements
where rowid=rec.rowid
do
null;
end loop;
except
WHEN invalid_character_detected THEN
dbms_ouput.put_line(rec.rowid)
end;
end loop;
end;
I did not compile and test the program. ??? is the (negative) error code, e.g. -XXXXX if the error is ORA-XXXXX
I am getting this error while I am fetching value from resultset.
Error : com.microsoft.sqlserver.jdbc.SQLServerException: The column name company.short_name is not valid
CASE 1 :
select company.short_Name,location_name from company,location;
this query is executing fine on SQL Server but in my java code when I trying to retrieve value like resultset.getString("company.short_name"); that time this give the above error.
CASE 2 :
select company.short_Name short_name,location_name from company,location;
and retrieve value like resultset.getString("short_name"); than it work fine with both database MySQL and MSSQL.
I am migrating my database from MySQL to MSSQL.above case 1 is work fine in MySQL, but why it is not work in MSSQL?
resultset.getString("company.short_name"); is wrong here. No need to specifying fully qualified name while trying to fetch the data in your application. Just specify the column name like resultset.getString("short_name");.
Cause even though you say select company.short_Name ... query out the column name as short_Name since that's what defined in table schema.
In case both tables has same column which may result in ambiguity, give a alias name to the columns like
select company.short_Name as company_shortname,
location.short_Name as location_shortname,
location.location_name from company,location;
add the following to your application.properties file
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
When you do
select company.short_Name,location_name from company,location;
This query outs the column name short_Name and resultSet would also have short_Name
since the company.short_name doesnt exist you get an error.
the function resultset.getString(String columnLabel)
Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.
Parameters:
columnLabel the label for the column specified with the SQL AS clause. If the SQL AS clause was not specified, then the label is the name of the column
Returns:
the column value; if the value is SQL NULL, the value returned is null
Throws:
SQLException - if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set
in the function resultset.getString(String columnLabel), the arg is a column name for executing sql, the statement select company.short_Name,location_name from company,location; will get a result set, which has table headers short_Name,location_name
I don't know whats wrong with this query :
SQL = "UPDATE "+ choosenClass +" SET COUNT = "+count+" WHERE NAME = "+names;
stmt2.executeUpdate( SQL );
Error :
java.sql.SQLSyntaxErrorException: Column 'RAMAYAH' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'RAMAYAH' is not a column in the target table.
It says Column RAMAYAH is not there but the string "RAMAYAH" itself is taken from the same table. Help Please.
Try to enclose the string into single quotes:
SQL = "UPDATE "+ choosenClass +" SET COUNT = "+count+" WHERE NAME = '"+names+"'";
stmt2.executeUpdate( SQL );
Apart from SQL-Injection vulnerability as mentioned by others in comment; your query has few more mistakes/issues.
You need to quote the filter value in WHERE condition WHERE NAME = "+names should be
WHERE NAME = '"+names+"'". Otherwise, your query will be formed like below and query engine will presume that there exist a column named RAMAYAH in your table in question.
UPDATE tab1 SET COUNT = 10 WHERE NAME = RAMAYAH;
Also, in your UPDATE statement, you have a column names COUNT which is a reserve word. You need to escape it to be in safe side.
If you are using mysql then use backtique
for sql-server use [] square bracket
for postgresql and oracle use "" double-quote