postgres function does not exist when call with Callable in Java JDBC - java

I called a postgresql function with Callable in JDBC, but it show exception
"function does not exis"
con = dataSource.getConnection();
con.setAutoCommit(false);
CallableStatement stmt = con.prepareCall("{call myfunction(?)}");
stmt.registerOutParameter(1, Types.REF_CURSOR);
stmt.execute();
con.commit();
However, when i add
stmt.setObject(1, null);
before registerOutParameter , it works.
I can not understand what happen

Related

PreparedStatement not executing the query

I am trying to move all query executions from Statement to PreparedStatement due to SQL injection. My original issue was with update statement, but I wanted to try it with select statement as well. When I execute the below line of code, the statement returns nothing.
String selectQuery = "select is_enabled, syllabus_id from ic_syllabus where syllabus_id=?";
PreparedStatement pstmt = conn.prepareStatement(selectQuery);
pstmt.setString(1, "25AC1CFB7C1A2CF07F176BD3A296F229");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
String flag = rs.getString(1);
String sybsId = rs.getString(2);
}
I am using Oracle database and am not getting any exceptions either.

Exception in prepared statement

Following is the code.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:cse");
//Statement stmt;
ResultSet rset;
//stmt = conn.createStatement();
String sql = " Select * from registration where id=?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, "101");
rset = pst.executeQuery(sql);
while (rset.next()) {
arr.add(rset.getInt("id"));
arr.add(rset.getString("first"));
arr.add(rset.getString("last"));
arr.add(rset.getInt("age"));
}
System.out.println(arr);
pst.close();
conn.close();
For the above am getting "Error: java.sql.SQLException: Driver does not support this function". What might be the problem?
You are misusing the PreparedStatement interface. When using PreparedStatements, you should prepare the statement with your query, bind all necessary parameters and then execute it without any SQL - this will cause the statement to execute the previously prepared SQL statement:
String sql = "Select * from registration where id=?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, "101");
rset = pst.executeQuery(); // Note - No args in the executeQuery call

How Get a scrollable resultset in Java

I am using Java for my application and Oracle database in the back-end
ResultSet GetCar()
{
CallableStatement cs;
ResultSet rs;
try{
//conn = dbConnector.getConnection();
conn = dbConnection.getStaticConnection();
cs = conn.prepareCall("begin select_all_car(?); end;",
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
cs.registerOutParameter(1, OracleTypes.CURSOR);
cs.execute();
rs = ((OracleCallableStatement)cs).getCursor(1);
return rs;
}
}
void foo()
{
ResultSet rs = GetCar();
rs.beforeFirst();
}
In foo rs.beforeFirst is giving me this error: "Invalid operation for forward only resultset : beforeFirst"
And this is my query for ORACLE database:
CREATE OR REPLACE PROCEDURE
SELECT_ALL_CAR
(
pCar_Recordset OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN pCar_Recordset FOR
SELECT ID, MANUFACTURER, MAKE, YEAR, MODEL
FROM CAR
ORDER BY ID;
END SELECT_ALL_CAR;
what am I doing wrong? is it oracle cursor? how can I make my resultset scrollable?
As #Przemyslaw pointed out you could use a PreparedStatement to call your stored procedure (no need to move your implementation to the front end, and your front end should not be making SQL calls directly anyway) you can define an ad hoc procedure call to then call your existing (packaged right?) stored procedures. If that isn't acceptable, you can create a view (or a materialized view) and query from that instead.

How to update updateble ResultSet with SQL Function e.g. NOW()

Is there any way to call SQL Function during updateXXX method in ResultSet of type ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE.
For example look at this code fragment:
stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(query);
while (rs.next()) {
rs.updateTimestamp("status_time", "NOW()"); // call some SQL function during update
rs.updateRow();
}
Is there any way to update value such way? The above example ofcourse don't work.
You can't do that. These methods are for setting actual values, not for executing SQL functions.

Java JDBC exec procedure with sql statement

I need to execute this SQL code:
exec procedure(param, param, param, param)
select * from bla_bla_table;
commit;
And get a ResultList from this query.
I tried to do it like this:
CallableStatement stmt = connection.prepareCall("{call procedure(param,param,param,param)}");
stmt.execute();
How can I insert sql statement select * from bla_bla_table; here before commit to get the ResultSet. I tried a lot of ways to do that... but nothing helps.
Did you try this?
connection.setAutoCommit(false); // Disable Auto Commit
CallableStatement stmt = connection.prepareCall("{call procedure(param,param,param,param)}");
stmt.execute();
Statement stmt2 = connection.createStatement();
ResultSet rs = stmt2.executeQuery("select * from bla_bla_table"); // Result set for Query
connection.commit();
Add this code after the execution of your code.
PreparedStatement prep = connection.prepareStatement(string);
ResultSet rs = prep.executeQuery();
// now iterate the resultset.
Before everything you should make sure that you run a transaction by setting the autocommit option of the connection to false.
connection.setAutoCommit(false);

Categories