Executing a Stored Procedure using Java

You can execute a stored procedure using Java using a CallableStatement
 Connection connection = null;
        try {
            // Instantiate  the appropriate JDBC driver
            final String driverName = "oracle.jdbc.driver.OracleDriver";
            Class.forName(driverName);
 
            // Connection parameters
            final String databaseHost = "databaseHost";
            final String portNumber = "1527"; //Port Number
            final String sid = "dbname";
            final String url = "jdbc:oracle:thin:@" + databaseHost + ":" + portNumber + ":" + sid;
            final String username = "username";
            final String password = "password";
            connection = DriverManager.getConnection(url, username, password);
 
            CallableStatement proc = connection.prepareCall("{call myproc(?, ?, ?)}");
            proc.setString(1, "value1");
            proc.setString(2, "value2");
            proc.setString(3, "value3");
            proc.execute();
 
        } catch (ClassNotFoundException e) {
            System.err.println("Unable to load the driver class, check your classpath.");
            e.printStackTrace();
        } catch (SQLException e) {
            //Database problem.
            System.err.println("Database problem.");
            e.printStackTrace();
        }

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.