I'm trying to execute a stored procedure without input variables like :
String sql2 = "{call vivek}" ;
System.out.println(sql2);
System.out.println("Executing procedure without parameters");
stmt = conn.createStatement();
stmt.executeQuery(sql2);
But its throwing an error saying :
syntax error at or near "{"
Position: 1
I'm trying to google it but not able to find anything. How do I do it ? By the way it didn't work with callablestatement also
Not tested:
CallableStatement cs = null;
cs = conn.prepareCall("{call vivek}");
cs.executeQuery();
http://docs.oracle.com/javase/tutorial/jdbc/basics/storedprocedures.html
It's similar to calling a function without arguments.
CallableStatement cat = null;
con = YourConnectionClass.getConnection();
cst = con.prepareCall("{call YOUR_PROCEDURE_NAME()}");
cst.executeQuery();
Could not find stored procedure? I will explain what it means when you get this error. Assuming our code is like this:
String sp="{call GetUnitReferenceMap}";
stmt=conn.prepareCall(sp);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
currencyMap.put(rs.getString(1).trim(), rs.getString(2).trim());
I have 4 DBs (sample1, sample2, sample3), but stmt will search location master (Default DB) then we will get an Exception.
We should provide DB name, then problem will be resolved::
String sp="{call sample1..GetUnitReferenceMap}";
Thanks for the help everyone but the following code worked for me :
sql2 += "{exec "+ proc_name2 +"}" ;
cstmt = conn.prepareCall(sql2);
cstmt.executeUpdate();
Here cstmt is the CallableStatement object by the way. And I'm using Postgresql Database hence the above code worked for me
Related
i want to pass another database to the stored procedure in java
like
call Sel_RptDaywiseProduction(?)
how to pass DIERP database in this SP in java in callable statement
please help
thank you.
Edit: code from the comment
CallableStatement cs = con.prepareCall("{call Sel_RptDaywiseProduction(?)}");
cs.setString(1, "DIERP");
ResultSet rs1 = cs.executeQuery();
while (rs1.next()) {
System.out.println("mat_code = ");
}
I am trying to execute results from my stored procedure and it is not displaying anything. I have played around with sql syntax query and nothing worked...when I do a normal select statement I get results from my tables, but so far nothing worked with my stored procedure...what am I not getting results ?
I am using sql-server by the way
<%
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://....";
String username = "something";
String password = "something";
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
ResultSet rs;
CallableStatement cs;
cs = conn.prepareCall("{ exec rptGetAssetbyLocation(?, ?,?,?,?,?) }");
cs.setString(1, "name");
cs.setString(2, "model");
cs.setString(3, "serialNumber");
cs.setString(4, "type");
cs.setString(5, "condition");
cs.setString(6, "note");
try {
cs.execute();
out.println("Stored procedure completed successfully.<br>");
} catch (Exception e) {
out.println("The following error occurred: " + e + "<br>");
}
%>
edit:
ran a stacktrace
I am getting an error com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '{'.
To display the output from the procedure to need to do something like this
char val= ((OracleCallableStatement)cs).getCHAR(3)
Remember OracleCallableStatement is an interface which implements CallableStatement, OraclePreparedStatement
You can override its method to get the results from the callable statments
Also these examples would help you
1.how to display tabledata with java stored procedure
2.Stored procedure with Input/Output parms and a ResultSet
3.Java Stored procedures
Hope this helps !!
My oracle procedure returns an array of string and I call the procedure from my java method.
When I execute the query I get the following exception.
java.sql.SQLException: ORA-03115: unsupported network datatype or representation
Note:
I have used OracleTypes.ARRAY here but I also tried using Types.ARRAY and it still gave me the same error.
My Java method:
public void searchRecords() throws FinderException{
Session session = getCurrentSession();
Connection con = session.connection();
CallableStatement stmt = null;
String procCall = "call pkg_inquiry.test2(?,?)";
try{
stmt = con.prepareCall(procCall);
stmt.setInt(1, 1);
stmt.registerOutParameter(2, OracleTypes.ARRAY);
//stmt.registerOutParameter(2, Types.ARRAY);
stmt.execute(); //-----------This statement causes exception
}catch(SQLException e){
System.out.println(e.getMessage());
}
}
MY Procedure:
PROCEDURE test2(
p_in in INTEGER,
p_out OUT header_row_vt
)
is
BEGIN
p_out := header_row_vt('NAME', 'AGE', 'CITY', 'STREET');
END test2;
If you want to know I am using Oracle 11.2 and jdbc 11.2
I experienced a similar problem. My first guess was that the driver's sub-version (11.2.0.3) did not match the database's version (11.2.0.2), but this was not the case.
I got it to work eventually by declaring a return type in the database and calling stmt.registerOutParameter(2, OracleTypes.ARRAY, "TYPE_NAME"); instead.
An example for this is provided here:
CallableStatement + registerOutParameter + multiple row result
I cant test your code right now but seems like the issue is following:
If you are using OracleTypes then you should use OracleCallableStatement
OracleCallableStatement cs = con.prepareCall(procCall);
Also if you want to use CallableStatement try java.sql.Types.ARRAY
Cheers !!
I am trying to call a stored function in Java. Function has no package, it's placed under user (schema) USER and returns a cursor. I tried twoways to call it and none of these works.
First one
Query query = coreDao.getEntityManager().createNativeQuery("{call USER.gen_rephead_sm_task_report(?, ?) }");
query.setParameter(1, dateFrom);
query.setParameter(2, dateTo);
List<?> queryResult = query.getResultList();
I got a not a procedure or not defined
I also tried this approach with
select gen_rephead_sm_task_report(?, ?) from dual
as a createNativeQuery parameter but with the same result.
Second one
Connection connection = dataSource.getConnection();//javax.sql.DataSource
CallableStatement statement = connection.prepareCall("{? = call USER.gen_rephead_sm_task_report(?, ?) }");
statement.registerOutParameter(1, OracleTypes.CURSOR);//oracle.jdbc.OracleTypes
statement.setDate(2, new java.sql.Date(dateFrom.getTime()));
statement.setDate(3, new java.sql.Date(dateTo.getTime()));
statement.executeQuery();
ResultSet set = ((OracleCallableStatement) statement).getCursor(1);
I got the ClassCastException on the last line (obviously OracleCallableStatement doesn't implement the CallableStatement). So which types shoud I use here?
Ok, so I found a link which solves my problem (iDevelopment). Here is what I used
connection = dataSource.getConnection();
CallableStatement statement = connection.prepareCall(TIMESHEET_QUERY);
statement.registerOutParameter(1, OracleTypes.CURSOR);
statement.setDate(2, new java.sql.Date(dateFrom.getTime()));
statement.setDate(3, new java.sql.Date(dateTo.getTime()));
statement.execute();
ResultSet set = (ResultSet) statement.getObject(1);
Everything is straight from javax.sql.* so there is no need for Oracle Specific API.
Im trying to write sample stored functions in postgresql and call them using the CallableStatement offered by JDBC.
Here's some my test code
Consumer bean =new Consumer();
CallableStatement pstmt = null;
try {
con.setAutoCommit(false);
String query = "{ ? = call getData( ? ) }";
pstmt = con.prepareCall(query);
pstmt.registerOutParameter(1, Types.OTHER);
pstmt.setInt(2,5);
pstmt.execute(); // execute update statement
bean=(Consumer)pstmt.getObject(1);
System.out.println("bean"+bean.getConsumer_name());
And my Stored function is of the form .
CREATE FUNCTION getData(int) RETURNS SETOF db_consumer AS $$
SELECT * FROM db_consumer WHERE consumer_id = $1;
$$ LANGUAGE SQL;
However, I'm getting the following error when I try to run the code .
org.postgresql.util.PSQLException: A CallableStatement was executed with an invalid number of parameters .
Any idea why this could be happening?
I don't think you need a CallableStatement as you should be able to run select * from getData(5) directly:
PreparedStatement pstmt = con.prepareStatement("select * from getData(?)")
pstmt.setInt(1,5);
ResultSet rs = pstmt.execute();
while (rs.next()) {
System.out.println(rs.getString(1));
}
You are trying to call a SETOFF function via a Callable Statement. That's not going to happen! You'll always get an error.
PostgreSQL's stored functions can return results in two different ways. The function may return either a refcursor value or a SETOF some datatype. Depending on which of these return methods are used determines how the function should be called.
Functions that return data as a set should not be called via the CallableStatement interface, but instead should use the normal Statement or PreparedStatement interfaces.