I am working on test environment.
Here is the query which I used in java
String sql = "INSERT INTO PO_PART_XML_IF (INTERFACE_ID, INTERFACE_TRANSACTION_ID,"
+ " O_SEQ_NO, PART_NO, O_QTY, O_TYPE_CODE, R_R_NO"
+ " ,O_LINE_COMMENT, C_CODE, INPUT_USER_ID, INPUT_TIMESTAMP, "
+ "LAST_UPDATE_USER_ID, LAST_UPDATE_TIMESTAMP ) VALUES (
G_REPAIR_PARTS_IF_SEQ.nextval,"+transactionnumber+",'"+orderSecNo+"','"+
job.getPartNo()+"',"+ Long.parseLong(job.getOrderQty())+",'"+job.getDeliveryType().getTitle()+"','"+job.getServiceReceiptNo()+"','"+replaceSingleQuote(job.getOrderComment())+"','"+currencyCode+"','"+inputUserID+"',SYSDATE,'"+inputUserID+"',SYSDATE)";
When I did some test then I got this error in log files :
org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
I checked directly on pl/sql, It is working fine.I mean insert data. but when you test with java program, it show me this error.
Any Idea!
If the query is ok (you'll have to check that), the code which should work is
Statement statement = dbConnection.createStatement();
statement.executeUpdate(sql);
You are probably using
statement.executeQuery(sql);
which is wrong for insert query.
You haven't shown the code that actually executes the query, but judging by the message it seems you are executing as if it returns data, ie a SELECT, but it's an UPDATE statement.
Try executing it using the appropriate API for an update.
Related
ResultSet rs;
rs = this.orderedProduct.select(" sum(unitstoproduce) "," soNo = "+ soNo);
int sum = Integer.parseInt(rs.getString(1));
When i try to execute the above query inside the java class i'm getting an exception as below.
Here the orderedProduct(orderedZnAlProduct) is the table
SELECT sum(unitstoproduce) FROM orderedZnAlProduct WHERE soNo = '15005'
java.sql.SQLException: Before start of result set
I don't see where your query is executed (like statement.executeQuery()) but, this kind of errors generally happens when your cursor is not well positioned. You need to call something likers.next() or rs.first() to move your cursor and then to get the result.
Even I encountered the same problem when I tried to execute
select count(*) TABLE_NAME
But I never encountered the error in any other scenarios when retrieving the values, So I assumed for the queries which have only one row we need to explicitly mention rs.first() and it 'll work like a charm.
This question already has an answer here:
Using parameter values for schema and table in Postgresql
(1 answer)
Closed 2 years ago.
I am creating a simple java application that uses a postgresql database which I have already created (the database has been created, not the application).
One of the TABLEs is called 'Employee'.
It has an attribute called 'eid', short for employee id.
I am trying to use a PreparedStatement to generalize a database SELECT query. I want there to be two parameters which are generalized. I would also like to get the length of the result, so I created a second PreparedStatement to find that.
The code seems quite simple, but I keep getting the same syntax error.
Here is the code I am trying to run:
public static String[] select(Connection conn, String arguments) {
try {
PreparedStatement preparedSelect = conn.prepareStatement("SELECT ? FROM ?");
PreparedStatement preparedCount = conn.prepareStatement("SELECT COUNT(*) FROM ?");
String[] arrOfStr = arguments.split(" ", 0);
System.out.println(arrOfStr[0].equals("eid"));
System.out.println(arrOfStr[1].equals("Employee"));
preparedSelect.setString(1, arrOfStr[0]);
preparedSelect.setString(2, arrOfStr[1]);
preparedCount.setString(1, arrOfStr[1]);
ResultSet rsSelect = preparedSelect.executeQuery();
ResultSet rsCount = preparedCount.executeQuery();
...
In the first line's argument, I would like the SELECT query to be: "SELECT eid FROM Employee".
In the second line's argument, I would like the SELECT query to be: "SELECT COUNT(*) FROM Employee".
The user who uses this application enters (earlier in the program) the "arguments" argument found in the arguments of the "select" function. The "arrOfStr" string array will contain these two strings, "eid", and "Employee".
To check that "arrOfStr" has the correct values, I printed the booleans resulting from the .equals() function comparing the values to the strings I want which, again, are "eid", and "Employee".
In the console the results are, not to my surprise:
true
true
Thus, in the next 3 lines, I set the String values "eid" and "Employee" into the PreparedStatements.
Finally, I execute the queries in the following 2 lines. Both of these query executions (I have tried switching them around) give me a very similar error. For the given code I get the error:
SQL State: 42601
ERROR: syntax error at or near "$2"
Position: 16
This is stating that there is an issue with the syntax of the string "Employee".
When I go directly to my postgresql database, and input:
SELECT eid FROM Employee;
I get the output:
1663
1983
1357
...
Could someone explain this syntax error?
As mentioned by Elliott, you cannot bind the table name that way.
To achieve your goal, try this:
String[] arrOfStr = arguments.split(" ", 0);
PreparedStatement preparedSelect = conn.prepareStatement("SELECT ? FROM " + arrOfStr[1]);
PreparedStatement preparedCount = conn.prepareStatement("SELECT COUNT(*) FROM " + arrOfStr[1]);
I am dealing with a legacy code that is 'not changeable' (no way to move to criteria api) and I have a little trouble with proper parameters binding. The query looks like this (MS SQL):
SELECT BRAND AS b FROM CAR WHERE (NAME LIKE :phrase OR MODEL LIKE :phrase ) AND AGE NOT IN(1997, 1998) AND (:mileage IS NULL OR MILEAGE LIKE :mileage) ORDER BY BRAND
(...)
query.setParameter("phrase", "%" + phrase + "%");
query.setParameter("mileage", mileage);
phrase is actually required, but because the mileageparameter is optional, it's done in wierd way presented above.
The problem is that with both phrase and mileage provided, It keeps giving me following error : java.sql.SQLException: ResultSet may only be accessed in a forward direction.. It works wihtout mileage parameter provided. Why I am getting this error ?
EDITED:
Running this query on db gives me no results.
I am using query.setResultTransformer(Transformers.aliasToBean(type)) as well as setting first and max result on my SQLQuery query object.
An error is when calling query.list() (used intellij evaluate expression)
shouldn't query.list() return an empty result ?
Probable answer (in that case):
It looks like setting FirstResult on the query cause that problem because - by mistake - it's a negative number.
How are you executing the SQL and then accessing the results?
It sounds like there are no results when executing the SQL with the mileage parameter and you are somehow trying to read the results anyway.
Try running the generated SQL immediately on the database and see if it returns any rows.
I am trying to use a Java application (which I do not have the source code for) to output the results of a call to a stored procedure into a text file.
This file works for other similar stored procedures in the system, but I can't seem to get it to produce anything for my new text file other than this exception:
ResultSet is from UPDATE: No Data
I've simplified the body of the stored procedure to a simple select 'Hello World!' and even that doesn't seem to be able to be written out.
Is there anything I can do within the stored procedure to produce results in a fashion that Java will accept?
I encountered this java.sql.SQLException. In my case I was running a query in this way:
String query =
"-- a classical comment " +
"select * " +
"from MYTABLE ";
ResultSet rs = conMain.createStatement().executeQuery(query);
while(rs.next()) {
//do something...
}
rs.next() throws the exception. The reason is that, due to the comments, query results to be:
"-- a classical comment select * from MYTABLE "
hence it's all commented... query is invalid! Many examples could be shown with this mistake (with the comment in the middle of the query etc.).
Solutions: add a \n at the end of each line of the query or use comments in the /*...*/ form.
I selected an older version of the driver an it worked for me.
http://dev.mysql.com/downloads/mirror.php?id=13598 (mysql-connector-java-5.0.8.zip)
i written this query (in java class) to select some information from the MySQL database
and view it on jsp page...
SELECT instructor.name FROM
instructor,section,teach WHERE
teach.student_id='3'AND teach.section
= section.number AND section.instructor_id= instructor.ID
but there is exception was occur!
javax.servlet.ServletException:
com.mysql.jdbc.exceptions.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
'.student_id='3'AND teach.section =
section.number AND
section.instructor_id= ins' at line 1
,,
by the way, i was write it in PhpMyAdmin, and it's work..
App server: Sun GlassFish Enterprise Server v2.1
please help me...
Regards
You need a space after the '3'.
It seems that there is a space missing. '.student_id='3'AND -> '.student_id='3' AND
If your query is exactly as you show it:
SELECT instructor.name FROM instructor,section,teach WHERE teach.student_id='3'AND
teach.section = section.number AND section.instructor_id= instructor.ID
then you need a space after the '3' and before AND on the first line shown above.
What is the datatype of "teach.student_id"? Is it numeric or varchar. If it is numeric, no need to put the '3' inside single quotes.
e.g.
teach.student_id = 3
Thanks for all
i was write query in java class like this
ResultSet rs
=stmt.executeQuery("SELECT instructor.name " +
"FROM instructor,section,teach" +
"WHERE teach.student_id=" + "'" + idd + "'" +
" AND teach.section = section.number
AND section.instructor_id=
instructor.ID");
then i eliminate all lines, and put query in one line like this, then it's solved...
Regards