i am a beginner in java Desktop application, and i was trying to insert some data into my table and i get the following reply "colown count does not match value count at row 1" here is my query
String sql = "insert into cataloguetb(title_statement,aurthurs_name,edition_statement,book_title,publisher_name"
+ "place_of_publication,year_of_publication,isbn_no,index_no,pagenRomannuem,pagneArabi,illuss,size_of_book"
+ "otherAurthurs,addEntries,length_in_cm,accessionNO,call_No1,call_No2,call_No,call_No4)values ('"+(titleStatement)+"','"+(aurthursName)+"'"
+ "'"+(editionStatement)+"','"+(bookTitle)+"','"+(publisherName)+"','"+(placeOfPublication)+"','"+(yearOfPublication)+"'"
+ "'"+(isbnNo)+"','"+(indexNo)+"','"+(pageRoman)+"','"+(pageArabic)+"','"+(illustration)+"','"+(size)+"','"+(otherAuthurs)+"'"
+ "'"+(addedEntries)+"','"+(lengthOfBook)+"','"+(accessionNo)+"','"+(calNo1)+"','"+(calNo2)+"','"+(calNo3)+"','"+(calNo4)+"')";
i have tried so many solutions even from stacflowoverflow there seem to no solution thanks for your help.
You need trailing commas on the fields at the end of the rows. Try this:
String sql = "insert into cataloguetb(title_statement,aurthurs_name,edition_statement,book_title,publisher_name,"
+ "place_of_publication,year_of_publication,isbn_no,index_no,pagenRomannuem,pagneArabi,illuss,size_of_book,"
+ "otherAurthurs,addEntries,length_in_cm,accessionNO,call_No1,call_No2,call_No,call_No4) values ('"+(titleStatement)+ "','"+(aurthursName)+"',"
+ "'"+(editionStatement)+"','"+(bookTitle)+"','"+(publisherName)+"','"+(placeOfPublication)+"','"+(yearOfPublication)+"',"
+ "'"+(isbnNo)+"','"+(indexNo)+"','"+(pageRoman)+"','"+(pageArabic)+"','"+(illustration)+"','"+(size)+"','"+(otherAuthurs)+"',"
+ "'"+(addedEntries)+"','"+(lengthOfBook)+"','"+(accessionNo)+"','"+(calNo1)+"','"+(calNo2)+"','"+(calNo3)+"','"+(calNo4)+"')";
This is because number of columns that you want to enter and datas(values for columns) you are entering are not same
the output of your code will be something like this
insert into cataloguetb(title_statement,aurthurs_name,edition_statement,book_title,publisher_nameplace_of_publication,year_of_publication,isbn_no,index_no,pagenRomannuem,pagneArabi,illuss,size_of_bookotherAurthurs,addEntries,length_in_cm,accessionNO,call_No1,call_No2,call_No,call_No4)values (.......)
That means you trying to insert in 19 columns but giving 21 values.So the error.
Well it is not the proper way of insertion.
Better would be to use PreparedStatement like this way
PreparedStatement pt=con.prepareStatement("insert into table (x,y) values(?,?");
pt.setString(1,value_for_x);
pt.setString(2,value_for_y);
pt.executeUpdate();
Related
I'm trying to insert a large amount of records in my inverted index which has built as table in MS access database. This is table design (ID,term,doc,sent is compound primary key):
and this is the code:
Connection conn = DriverManager.getConnection("jdbc:ucanaccess://myDB.accdb");
Statement s = conn.createStatement();
s.execute("DELETE FROM invertedIndex");
for(String o : POSoutputs) //while (Tokenizer.hasMoreTokens())
{
String word = o;
s.execute("insert into invertedIndex (term,doc,sent) values ('"+ o + "','" + listOfFiles[i].getAbsolutePath() + "','" + fileText + "')");
conn.commit();// i do commit to empty the stack because i will insert thousands of records, by scanning hundreds of documents.
}
This is the Error:
java.lang.StackOverflowError
at java.nio.DirectByteBuffer.put(DirectByteBuffer.java:297)
at java.nio.ByteBuffer.put(ByteBuffer.java:832)
at java.nio.DirectByteBuffer.put(DirectByteBuffer.java:379)
at java.nio.DirectByteBuffer.put(DirectByteBuffer.java:342)
at sun.nio.ch.IOUtil.write(IOUtil.java:60)
at sun.nio.ch.FileChannelImpl.writeInternal(FileChannelImpl.java:778)
at sun.nio.ch.FileChannelImpl.write(FileChannelImpl.java:761)
at com.healthmarketscience.jackcess.impl.PageChannel.allocateNewPage(PageChannel.java:350)
at com.healthmarketscience.jackcess.impl.TempPageHolder.setNewPage(TempPageHolder.java:115)
at com.healthmarketscience.jackcess.impl.UsageMap$ReferenceHandler.createNewUsageMapPage(UsageMap.java:763)
at com.healthmarketscience.jackcess.impl.UsageMap$ReferenceHandler.addOrRemovePageNumber(UsageMap.java:747)
at com.healthmarketscience.jackcess.impl.UsageMap.removePageNumber(UsageMap.java:337)
at com.healthmarketscience.jackcess.impl.PageChannel.allocateNewPage(PageChannel.java:354)
at com.healthmarketscience.jackcess.impl.TempPageHolder.setNewPage(TempPageHolder.java:115)
at com.healthmarketscience.jackcess.impl.UsageMap$ReferenceHandler.createNewUsageMapPage(UsageMap.java:763)
at com.healthmarketscience.jackcess.impl.UsageMap$ReferenceHandler.addOrRemovePageNumber(UsageMap.java:747)
at com.healthmarketscience.jackcess.impl.UsageMap.removePageNumber(UsageMap.java:337)
at com.healthmarketscience.jackcess.impl.PageChannel.allocateNewPage(PageChannel.java:354)
at com.healthmarketscience.jackcess.impl.TempPageHolder.setNewPage(TempPageHolder.java:115)
........ ERROR RECORDS ARE DUPLICATED .. etc
what's the problem?
The error is clear: ERROR RECORDS ARE DUPLICATED
So you have a unique index on one or more fields. Either remove this or remove the records with duplicate field values.
The database was corrupted, i have created another one with the same tables ..
I think the reason is because the answered query in this question: How to restart counting from 1 after erasing table in MS Access?, which have damage the structural indices of the database.
A similar issue is in: deleting row with BigIndex UsageMap exception
I am querying my SQL database and have discovered that when I ask for 160 rows, and the database only has 44 rows, ResultSet returns 160 rows. When this happens, the first 44 rows have actual useful numeric values and the remaining 116 rows have values that are all 0. How do I get ResultSet to only contain or provide me with the 44 rows that are returned from the database query? Here is the code fragment:
statement = connection.createStatement();
query = "SELECT " + "Close" + ", Volume" + " FROM source_data_manager.TradingData_daily WHERE Exchange = '" + exchange +
"' and Symbol = '" + symbol + "' order by EntryID DESC limit " + 160 + ";";
//Execute the query
resultSet = statement.executeQuery(query);
//Loop through the results
int count = 0;
while (resultSet.next())
{
valuesRetrieved[count++] = resultSet.getDouble(1);
volume += resultSet.getLong(2);
}
Coming out of this loop, count is equal to 160 but should equal to 44. The array valuesRetrieved[] has 44 elements of actual usable values, and the remaining 114 values all equal to zero (0). The array should only have 44 values in it.
Try using Statement.setMaxRows
void setMaxRows(int max) throws SQLException;
The advantage of setMaxRows is that you can create general statements, valid for Postgres, Oracle, Mysql etc.
In fact, Oracle is using ROWNUM syntax, postgres - LIMIT, msqsql - TOP.
It's strange, might it be driver specific reaction to limit without fetch size?
What would resultSet.isAfterLast()return for empty rows?
Thank you all for your responses. Nicholas Filotto had the correct answer. In the top of the method is the declaration of valuesRetrieved as follows:
double [] valuesReceived = new double [160];
The problem is fixed by not using the statement:
valuesReceived[count++] = resultSet.getDouble(1);
There is one additional peculiarity. I must not understand what getFetchSize() does because the statement:
int xxx = resultSet.getFetchSize();
always results in 0 being assigned to xxx.... Don't really understand why.
I'm trying to solve the problem of doing an insert into a Postgresql table
I looked at this similar question but it did not solve my problem
ERROR : The column index is out of range: 1, number of columns: 0
here is the part of code getting the error:
String query = "INSERT INTO reviews (nbstar, body, author, product_id) VALUES($1,$2,$3,$4)";
PreparedStatement prepareStatement = connection.prepareStatement(query);
prepareStatement.setInt(1, nbStar);
prepareStatement.setString(2, body);
prepareStatement.setString(3, author);
prepareStatement.setInt(4, productId);
boolean executed = prepareStatement.execute();
i tried several times to change the index number but still the same error
and here is the schema of the table:
table schema
can anyone give me an advice ?
thanks.
In the sql query, you want to insert the values for 5 fields (id, nbstar, body, author, product_id) but there are only 4 values VALUES($1,$2,$3,$4).
Update following your edited question, just modify your query as follows:
VALUES($1,$2,$3,$4)
to
VALUES(?,?,?,?)
My problem was that the question mark had single quotes around it and I copy pasted the query from straight sql so I just replaced the string with a ?. For example
Select * from datatable where id = '?'
and I had to change it to
Select * from datatable where id = ?
For me, I had added a comment which included a question mark.
Silly me!
I got that same error because I had the last \n missing in following query, I hope this helps somebody.
" order by mp.id desc\n" +
" limit 1\n" +
" ) as mge_mp\n" +
" )\n" +
"\n" +
"-- #pageable\n",
My last line was
"-- #pageable",
Incase you get this error, for my own issue, I was passing a field ending with a character next to a variable e.g.:
select * from my_schema."my_table" mt
where
mt.field_2 = variable_2
and mt.field_1 = 'variable_1'
[[and cast(mt.date as DATE) = {{date_picked}}]]
-> This failed with an error of:
The column index is out of range: 1, number of columns: 0
Changed to:
select * from my_schema."my_table" mt
where
mt.field_1 = 'variable_1'
and mt.field_2 = variable_2
[[and cast(mt.date as DATE) = {{date_picked}}]]
Please note the variable ending with a quote character has been moved.
-> This worked for me.
For me the issue was having a semicolon at the end of the query string. Something like:
String query = "INSERT INTO reviews (nbstar, body, author, product_id) VALUES(?,?,?,?);";
Notice the ; appended to the end of the query string. The fix is, well, to remove it:
String query = "INSERT INTO reviews (nbstar, body, author, product_id) VALUES(?,?,?,?)";
I came across a problem with going through a ResultSet I'm generating from my MySQL db. My query should return at most one row per table (I'm looping through several tables searching by employee number). I've entered data in some of the tables; but my test o/p says that the resultset contains 0 rows and doesn't go through the ResultSet at all. The o/p line it's supposed to print never appears. It was in a while loop before I realised that it'd be returning at most one row, at which point I just swapped the while(rs.next()) for an if(rs.first()). Still no luck. Any suggestions?
My code looks like this:
try
{
rsTablesList = stmt.executeQuery("show tables;");
while(rsTablesList.next())
{
String tableName = rsTablesList.getString(1);
//checking if that table is a non-event table; loop is skipped in such a case
if(tableName.equalsIgnoreCase("emp"))
{
System.out.println("NOT IN EMP");
continue;
}
System.out.println("i'm in " + tableName); //tells us which table we're in
int checkEmpno = Integer.parseInt(empNoLbl.getText()); //search key
Statement s = con.createStatement();
query = "select 'eventname','lastrenewaldate', 'expdate' from " + tableName + " where 'empno'=" + checkEmpno + ";"; // eventname,
System.out.println("query is \n\t" + query + "");
rsEventDetails = s.executeQuery(query) ;
System.out.println("query executed\n");
//next two lines for the number of rows
rsEventDetails.last();
System.out.println("no. of rows is " + rsEventDetails.getRow()+ "\n\n");
if(rsEventDetails.first())
{
System.out.println("inside the if");
// i will add the row now
System.out.println("i will add the row now");
// cdTableModel.addRow(new Object[] {evtname,lastRenewalDate,expiryDate});
}
}
}
My output looks like this:
I'm in crm
query is
select 'eventname','lastrenewaldate', 'expdate' from crm where 'empno'=17;
query executed
no. of rows is 0
I'm in dgr
query is
select 'eventname','lastrenewaldate', 'expdate' from dgr where 'empno'=17;
query executed
no. of rows is 0
NOT IN EMP
I'm in eng_prof
query is
select 'eventname','lastrenewaldate', 'expdate' from eng_prof where 'empno'=17;
query executed
no. of rows is 0
I'm in frtol
query is
select 'eventname','lastrenewaldate', 'expdate' from frtol where 'empno'=17;
query executed
no. of rows is 0
(and so on, upto 17 tables.)
The '17' in the query is the empno that I've pulled from the user.
The thing is that I've already entered data in the first two tables, crm and dgr. The same query in the command line interface works; this morning, I tried the program out and it returned data for the one table that had data in it (crm). The next time onwards, nothing.
Context: I'm working on a school project to create some software for my dad's office, it'll help them organise the training etc schedules for the employees. (a little like Google Calendar I guess.) I'm using Netbeans and Mysql on Linux Mint. There are about 17 tables in the database. The user selects an employee name and the program searches for all entries in the database that correspond to an 'event' (my generic name for a test/training/other required event) and puts them into a JTable.
The single quotes around the column names and table name in the creation of the query seem to have caused the problem. On changing them to backticks, retrieval works fine and the data comes in as expected.
Thank you, #juergend (especially for the nice explanation) and #nailgun!
I have the following problem:
I have two tables in one data base which consist of the same columns besides the name of the last column. I want to write data into them using Java.
I want to use the same preparedStatement for both tables, where I check with an if-command whether it is table1 or table2. table2 has amount10 as the name for the last column, table1 has amount20 for it. This number is stored in a variable within my code.
Below you can see a (simplified) example and how I tried to let the column name variable but it doesn't work. Is there any way to fix this without copying the whole statement and manually changing the number variable?
String insertData = "INSERT INTO `database`.`"+table+"`
(`person_id`,`Date`,`amount`+"number") VALUES "+
"(?,?,?) ON DUPLICATE KEY UPDATE " +
"`person_id` = ? , " +
"`Date` = ? , " +
"`amount`+"number" = ? ; ";
PreparedStatement insertDataStmt;
This will not work since variables number and table are not going to be magically injected into your insertData string while you are changing them.
I'd to a method prepareInsertstatement(String table, String number) that would return correct PreparedStatement:
public void prepareInsertStatement(Connection conn, Strint table, String number) {
String insertData = "INSERT INTO `database`.`"+table+"`
(`person_id`,`Date`,`amount+"number"') VALUES "+
"(?,?,?) ON DUPLICATE KEY UPDATE " +
"`person_id` = ? , " +
"`Date` = ? , " +
"`amount+"number"' = ? ; ";
PreparedStatement insertDataStmt = conn.prepareStatement(insertData);
return insertDataStmt;
}
Just remember to close the PreparesStatement when you don't need it any more.
I suppose that reason for that is invalid syntax. When you concatenate string for last column name you use code 'amount' + number. If your number value is 20, than concat result will be
'amount'20 that cause invalid syntax exception. Just move one extra ' after number.
"'amount" + number + "'"
Note: log, or just error that appears during this statement execution would be very useful to find right answer for your question.