Invalid character value for cast specification on column number - java

I have an access database with address attribute as "Text" data type. In my java/jdbc code, I'm using resultset to retrieve the address value using select query.
Generally, for sql database, It works with String address= resultset.getString();
but, since it's access db how do I query it?
As, I'm using the String address= resultset.getString(); statement, I'm getting following exception:
Invalid character value for cast specification on column number

Specify the column name or the index of the column while getting the resultset values.
like,
String value= resultset.getString(1); // if the text is in the first column

Related

"Invalid Column id" error when counting unique value in Java

I am using the following sql query in my java program:
SELECT COUNT(*) FROM event WHERE externaleventid ='1256294';
But I have an error as:
Invalid Column id.
Same query works fine in SQL Developer.
For some reason you are attempting to get a column named "externaleventid" from the query, but only "count(*)" is available. You shouldn't try to get back the where clause bind variables from the result set, you should get the actual data back from the result set, by column index or by column name.
Try rs.getInt(1) to get the data from the first column. Or, you can alias the column in the query, e.g. SELECT count(*) cnt FROM..., and you can refer to it by the aliased column name: rs.getInt("cnt").

How to do size validation of values for columns before inserting data into table in sql for DB2?

I need to check whether the value is with proper size to be inserted into a table for particular column before actual insertion of data into table.
e.g.,
Suppose, I have a table 'MyTable' with column 'MYSMALLINT' of SMALLINT type in DB2,
If I try to insert the value as,
insert into MyTable(MYSMALLINT) values(12222222222222222222222222222225552);
I will get below error, after executing the above query:
Message: The numeric literal "12222222222222222222222222222225552" is not valid because its value is out of range.
Here, I need to check the size of value before inserting the data into table in Java/JDBC for a column with any data type(CHAR,VARCHAR,INT,BIGINT,TINYINT,DOUBLE,FLOAT,DECIMAL,REAL,...).
You can do it with PreparedStatement:
PreparedStatement statement = connection.prepareStatement("insert into MyTable(MYSMALLINT) values(?);"
for(Integer value:values){
try{
statement.setInt(1,value);
statement.addBatch();
}catch(SQLException e){
System.err.println("Invalid value: " + e.getMessage());
throw e;
}
}
statement.executeBatch();
You can validate the before value in a trigger if it is valid for your requirements. The way you validate the value can a UDF, in that way you can test it outside the trigger.
Why not just map the column to the correct Java primitive.
Smallint is equivalent to a "short" in Java so if you defined your host variable as a short the compiler would detect any errors at compile time.
insert into MyTable(MYSMALLINT) values(12222222222222222222222222222225552S);
would be spat out by your java compiler.

Resultset.getDate() throwing exception java.lang.NumberFormatException: For input string: "Name of Month"

When extracting date from database using Resultset.getDate(), I am getting the following exception:
java.lang.NumberFormatException: For input string: "Name of Month".
I can confirm that the exception is thrown when trying to get data from a column of Oracle date datatype.
I can't paste the actual code here as it is very long. But the sample code is as given below
pstmnt = connection.prepareStatement(selectQuery);
rs = pstmnt.executeQuery();
while (rs.next()) {
rs.getDate(column.getColName());
}
Please help?
As requested by Sanjay, please find the stack trace below
java.lang.NumberFormatException: For input string: "MAY"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:63)
at java.lang.Integer.parseInt(Integer.java:481)
at java.lang.Integer.parseInt(Integer.java:531)
at java.sql.Date.valueOf(Date.java:200)
at oracle.jdbc.driver.OracleStatement.getDateValue(OracleStatement.java:4610)
at oracle.jdbc.driver.OracleResultSetImpl.getDate(OracleResultSetImpl.java:625)
at oracle.jdbc.driver.OracleResultSet.getDate(OracleResultSet.java:1601)
at quotecopy.DbConnection.getTableRows(DbConnection.java:126)
at quotecopy.QuoteCopier.main(QuoteCopier.java:66)
java.lang.NumberFormatException: For input string: "MAY"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:63)
at java.lang.Integer.parseInt(Integer.java:481)
at java.lang.Integer.parseInt(Integer.java:531)
at java.sql.Date.valueOf(Date.java:200)
at oracle.jdbc.driver.OracleStatement.getDateValue(OracleStatement.java:4610)
at oracle.jdbc.driver.OracleResultSetImpl.getDate(OracleResultSetImpl.java:625)
at oracle.jdbc.driver.OracleResultSet.getDate(OracleResultSet.java:1601)
at quotecopy.DbConnection.getTableRows(DbConnection.java:127)
at quotecopy.QuoteCopier.main(QuoteCopier.java:66)
Can you check your query, i believe you are using to_char on the Date column which is causing the getDate unable to recognize it as a valid Date
please refer http://forum.springsource.org/showthread.php?19783-Oracle-JDBC-Date-Problem.
May be it would help you
rs.getString() or rs.getDate() retrieves based on your database table column name. Can you verify that the column containing the date is named "Name of Month" ?
A few things you might want to check out:
Is the column type of this column in database really date/datetime?
What happens when you print out getObject() instead of getDate()? Can you show us the output?
What happens when you use column indices to retrieve the date instead of using column name?
Based on the information provided, it would be worth checking for the following scenarios in the codebase:
Reading a column that is not of type DATE in the Oracle database. The Oracle JDBC driver maps the Oracle database type DATE to the java.sql.Date type; actually it is the oracle.sql.Date type, which is a subtype of the java.sql.Date type. If you are not reading a column of type Oracle DATE, but instead a TIMESTAMP field, then you might encounter the exception. You would therefore have to use a suitable method - likely to be getTimeStamp, to read the data in the column.
The NLS_DATE_FORMAT used in the database is different from the date format expected by the JDBC driver. This would either require you to change the NLS_DATE_FORMAT on creating a session, using a logon trigger, or it would require you to use TO_CHAR function calls with appropriate date formats in your SQL queries.
Accessing the columns in the ResultSet in a random manner. Most JDBC drivers will expect you to read the contents of the result set, column by column starting at the first column, without allowing you to ignore any columns. If you attempted reading a column while ignoring the previous ones, then you might encounter this exception.

Inserting into Access database

How do I resolve the error in inserting memo in Access from a Java program?
4159 the size of the string
the error
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]COUNT field incorrect
The source code that executes the insert statement:
statement.executeUpdate("INSERT INTO webData VALUES ("+"'" + list.get(y)+"','"+data+ "')");
4159 the size of data
my schma is :
table name webData with 2 coulmun the
first ID of type text
the second Field1 of type memo
i have update the statment but i have get the same error:
statement.executeUpdate("INSERT INTO webData (ID,Field1) VALUES ("+"'" + list.get(y)+"','"+data+ "')");
Thank you
Please post your schema.
Rather than doing:
INSERT INTO webData VALUES (...)
You should be doing:
INSERT INTO webData (MyColumn1, MyColumn2) VALUES (...)
Do not rely on the physical column order in the table, you should state it explicitly to avoid errors.
Does the comma have to be in speech marks and inverted commas? You can simplify this, just a tip :). But yes, post your DB scheme.

Why am I getting the error SQLException: [Microsoft][ODBC Excel Driver] Too few parameters. Expected 1

I am using the following code for uploading keywords & count to an Excel file. I am having keyword_id as a primary key for that one. I am having the two columns in the Excel file. 1.keyword and 2.count
My code is:
while (rs.next()) {
System.out.println("inside ");
String keyword = rs.getString(1);
int count = rs.getInt(2);
System.out.println("insert into SEARCHABLE_KEYWORDS values ('"+
keyword+"','"+count+"')");
stmtdb.execute("insert into SEARCHABLE_KEYWORDS (keyword_id,keyword,count) values ('"+
"select Searchable_Keywords_sequence.nextval from dual"+
"','"+keyword+"','"+count+"')");
System.out.println(keyword + " " + keyword+" count "+count);
}
but I am getting the following error:
java.sql.SQLException: [Microsoft][ODBC Excel Driver] Too few parameters. Expected 1.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6998)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7155)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3151)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:378)
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(JdbcOdbcStatement.java:284)
at keywordsreader.main(keywordsreader.java:42)
count is a reserved keyword so if you have a column in your SEARCHABLE_KEYWORDS table called count then you need to put the column name in double quotes (")
Doublecheck the column names in your query. Does the table SEARCHABLE_KEYWORDS has the columns KEYWORD_ID, KEYWORD and COUNT? As far as I know this error message often is cause by typos or wrong spelling of your column names.
As mentioned by mikej you should also make sure not to use reserved keywords inside your statements without putting them in quotes. This tip applies to your column with the name COUNT.
You are defining in your SQL an insert for two values, but you are selecting three.
insert into SEARCHABLE_KEYWORDS (keyword_id,keyword,count) values ('"+
"select Searchable_Keywords_sequence.nextval from dual"+
"','"+keyword+
"','"+count+"')") `

Categories

Resources