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)
Related
I'm trying to convert a MySQL stored procedure to java code. It's pretty simple SQL. It creates three temporary tables and then does a select based on them. However, I'm having trouble with SQLWarnings related to Data Truncation being thrown while creating the last temp table.
I'm not looking for an explanation of the warnings themselves. I was getting the same warnings during execution of the stored procedure (due to improperly formatted datetime fields), only there it didn't cause me any problems. In the java version, after Statement.executeUpdate() throws an SQLWarning, the temp table I was attempting to create doesn't exist.
How can I suppress the SQLWarnings? I'd prefer to be able to log a message noting the warning text, but I want my temp table to successfully be created as well.
My code looks something like this:
sqlStatement = "create temporary table mytemptable as (select ...) ;";
try {
stmt.executeUpdate(sqlStatement);
// Throws SQLWarning about Data Truncation
} catch (SQLWarning sqlW) {
logInfo("SQLWarning Caught: " + sqlW.getMessage());
}
stmt.executeQuery("select * from mytemptable ");
// Throws SQLException because mytemptable doesn't exist
To me it's a little strange but the equivalent of this worked:
create temporary table mytemptable ignore select ...;
This way, no exception is thrown by jdbc, but you can still call getWarnings() on the Statement object to get a list of warnings that were ignored.
Thanks Norbert van Nobelen for helping me figure this out in the comments of the OP.
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.
I made a program to parse an XML file with, and now I want to put the data in a database,
a PostgreSQL database. However, I cannot use
executeUpdate(INSERT INTO Titles(name) VALUES (parseTitles())),
since it wants a boolean. The string that comes out of the function looks like this:
'a','b','c','d'
Is there a way to solve this, or am I bound to put all the data in manually?
java runs first and then the SQL statement is sent to the db to be executed.
You probably need something like this to produce the right sql statement:
executeUpdate( "INSERT INTO Titles(name) VALUES (" + parseTitles() + ")" );
I have table called mpi which contains 23 columns. I have introduced the search field with button for every column where user can enter the query to fetch the records using query
query="select * from mpi where Genus ='"+genus+"'
Now I want to fetch records by giving keywords using LIKE %% but it is not working and not giving any records but if type type the full name it is working perfectly. Here is the code
String uname=request.getParameter("uname");
String full="%"+uname+"%";
dbconn=new DatabaseConnection();
conn=dbconn.setConnection();
pstmt=conn.prepareStatement("select * from mpi where Genus LIKE ?");
pstmt.setString(1, full);
res=pstmt.executeQuery
Could any one tell me where is the mistake and why I am not getting the records when I use half keyword like %keyword%.
It works (apart from the missing parentheses) and the approach with a prepared statement is entirely correct.
However I have seen a couple of code pieces like that, and always the problem lay with variables mix-up or not closing, or simple oversight. Better declare as close as possible.
try (ResultSet res = pstmt.executeQuery()) {
while (res.next()) {
..
}
} // Automatically closes res.
Also handle the life-cycle of pstmt correctly, with closing.
I'm using postgresql to host my database. In my database, I have a table say xyz whose structure is as shown below
id content folder
1 hello Inbox
2 hi Sent
I want to export this table to CSV using my java program. The code snippet is below
Connection connection2 = new ServerDBConnect().getConnection();
PreparedStatement statement = connection2.prepareStatement("copy (SELECT * FROM xyz WHERE folder=? ) to 'C:/export.csv' delimiter ','");
statement.setString(1, FOLDER_SELECTED); //Here, FOLDER_SELECTED=Inbox
statement.execute();
When I execute this code, I'm getting SQLException saying
ERROR: there is no parameter $1
If I execute the code without folder specified ("copy (SELECT * FROM xyz) to 'C:/export.csv' delimiter ','")), the code works fine.
What am I doing wrong here? How to go about this problem?
Note: If I execute the query (copy (SELECT * FROM xyz WHERE folder='Inbox' ORDER BY time) to 'G:/export.csv' delimiter ',') directly in the Postgresql SQL console, I'm getting the desired output.
Please help
Ah
I finally found the answer myself.
Small change in the query gave me the desired result
The query is suppose to be like this
Connection connection2 = new ServerDBConnect().getConnection();
PreparedStatement statement = connection2.prepareStatement("copy (SELECT * FROM xyz WHERE folder='" + FOLDER_SELECTED + "' ) to 'C:/export.csv' delimiter ','");
This was driving me crazy, but finally done :-)