Inserting into Access database - java

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.

Related

Insert map and other complex types in hive using jdbc

I have a java Map (Map) and a JDBC connection to hive server.
The schema of the table at the server contains a column of type Map.
Is it possible to insert the java Map to the hive table column with similar datatype using JDBC?
I tried:
"create table test(key string, value Map<String, String>)"
"insert into table test values ('keywer', map('subkey', 'subvalue')) from dummy limit 1;"
ref: Hive inserting values to an array complex type column
but the insert failed with:
"Error: Error while compiling statement: FAILED: ParseException line 1:69 missing EOF at 'from' near ')' (state=42000,code=40000)"
[EDIT]
hive version is : 0.14.0
Thanks
The manual clearly says you cannot insert into a Map data type using SQL:
"Hive does not support literals for complex types (array, map, struct, union), so it is not possible to use them in INSERT INTO...VALUES clauses. This means that the user cannot insert data into a complex datatype column using the INSERT INTO...VALUES clause.”
I think the correct DDL and query would be:
CREATE TABLE test(key STRING, value MAP<STRING, STRING>);
INSERT INTO TABLE test VALUES('keywer', map('subkey', 'subvalue')) from dummy limit 1;
A working method to put a complex type from jdbc client is:
insert into table test select "key",map("key1","value1","key2","value2") from dummy limit 1;
where dummy is another table which has at least one row.

Syntax error on Java PreparedStatement.setBytes with mysql

I'm trying to set a BLOB mysql field with the PreparedStatement.setBytes method.
The problem is that the query ends up with a string containing the bytes to be inserted.. more or less.
So, here is the code where it's about:
PreparedStatement stmt = this.getStatement( "UPDATE TABLE users SET logintoken=? WHERE qrid=?" );
stmt.setBytes( 1, token );
stmt.setString( 2, bar.getId() );
stmt.execute();
For the record, token is a byte[] always holding 20 bytes generated by SecureRandom
And here is the error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TABLE users SET logintoken=x'6DA9D5137B059AE6BCFE7F170693A76CA6484FFB24963BF88AA' at line 1
As you can see, it places an x' at the beginning of the data.
Logically this breaks the query since part of the data is not contained within the string.
I assumed that the PreparedStatement.setBytes would solve the problem but it does not.
I've also tried it with a java.sql Blob object but it more or less gave the same issues.
Is there something i'm missing?
Look at your UPDATE query closely, the TABLE is not necessary. it should just be like
UPDATE users SET logintoken=? WHERE qrid=?
Refer MySQL Documentation for more information.

"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.

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