Java stored procedure calling in java program

前端 未结 2 1635
予麋鹿
予麋鹿 2020-12-22 06:40

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

相关标签:
2条回答
  • 2020-12-22 06:47
                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.

    0 讨论(0)
  • 2020-12-22 07:08

    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...

    0 讨论(0)
提交回复
热议问题