What I should do is to create a Java stored procedure and later call it from Java program.
I am having problems executing the JAVA program - JDBCPiemers (below) wit
CallableStatement stmt = null;
String sql = "{call movecartrecord(?)}";
try
{
stmt = conn.prepareCall(sql);
stmt.setInt(1, order_id); //you can pass argument if procedure accepts
stmt.execute();
stmt.close();
String name = stmt.getString(1);
System.out.println("Name : " + name);
}
catch (SQLException e)
{
e.printStackTrace();
}
Here movecartrecord() is a stored procedure name and conn is a connection object.
Here order_id passes as argument. If your stored procedure don't have any argument then just leave that line code.
Hope it will work and help you.
Here is a wonderful code that show you how to perform it.
To return a value you need it to be a function, not a procedure:
public static String getBreweryInfo (int Raz_ID)
throws SQLException
{
String sql =
"SELECT Nosaukums FROM Alus_razotaji WHERE ID = ?";//vaicajums
try {
Connection conn = DriverManager.getConnection("jdbc:default:connection:");
PreparedStatement apstmt = conn.prepareStatement(sql);
apstmt.setInt(1, Raz_ID);
ResultSet rset = apstmt.executeQuery();// SQL vaicājuma izpildīšana
if (rset.next()) {
return rset.getString(1);
}
}
catch (SQLException e) {
System.err.println(e.getMessage()); //Kļūdu izvadīsana
}
finally {
rset.close();
apstmt.close(); //Savienojuma aizvēršana
}
}
Then your package becomes:
CREATE OR REPLACE PACKAGE BODY BeerBeer AS
FUNCTION getBreweryInfo(Raz_ID number) RETURN varchar2 AS Language Java
NAME 'BeerBeer.getBreweryInfo(int) return java.lang.String';
END BeerBeer;
And you call it something like:
String SQL = "{CALL ? = BeerBeer.getBreweryInfo (?)}";
stmt = conn.prepareCall(SQL);
int Raz_ID = 4;
stmt.registerOutParameter(1, java.sql.Types.VARCHAR);
stmt.setInt(2, Raz_ID);
System.out.println("Izpildam JAVA glabajamo proceduru ...");
// Vaicājuma izpilde
stmt.execute();
//Izgustam Alus_razotaja nosaukumu ar getXXX metodi.
String Razotajs = stmt.getString(1);
Note that the out parameter is now index 1 as it's the return from the function, so the value you pass in is now index 2. All untested of course...