2010-06-08 104 views
2

我有以下的Java存儲過程:Java存儲過程

CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "RetreiveLdap" AS 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.SQLException; 
import java.util.Hashtable; 

import javax.naming.Context; 
import javax.naming.NamingEnumeration; 
import javax.naming.NamingException; 
import javax.naming.directory.DirContext; 
import javax.naming.directory.InitialDirContext; 
import javax.naming.directory.SearchControls; 
import javax.naming.directory.SearchResult; 

public class RetreiveLdap { 
    // LDAP CONFIG 
    public static String CONTEXT = "com.sun.jndi.ldap.LdapCtxFactory"; 
    public static String HOST = "ldap://192.168.30.12:389"; 
    public static String USER = "cn=root,dc=company"; 
    public static String PASSWORD = "pa55w0rd"; 
    public static String BASE = "dc=company"; 

    // Reads from LDAP 
    public static void readConfig() throws NamingException, SQLException { 
     try { 
      addUser("tmp", "tmp"); 
      Hashtable env = new Hashtable(); 
      env.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT); 
      env.put(Context.PROVIDER_URL, HOST); 
      env.put(Context.SECURITY_PRINCIPAL, USER); 
      env.put(Context.SECURITY_CREDENTIALS, PASSWORD); 

      DirContext ctx = new InitialDirContext(env); 

      SearchControls sc = new SearchControls(); 
      sc.setSearchScope(SearchControls.SUBTREE_SCOPE); 
      String filter = "(objectClass=person)"; 
      NamingEnumeration items = ctx.search(BASE, filter, sc); 
      System.out.println("STARTING..."); 
      while (items != null && items.hasMore()) { 
       SearchResult sr = (SearchResult) items.next(); 

       String cn = (String) sr.getAttributes().get("cn").get(); 
       String sn = (String) sr.getAttributes().get("sn").get(); 
       System.out.println("WORKING..."); 
       // save to table 
       addUser(cn, sn); 

      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    // Add LDAP user to DB. 
    public static void addUser(String cn, String sn) { 

     try { 
      Connection conn = DriverManager 
        .getConnection("jdbc:default:connection:"); 

      String sql = "INSERT INTO ldapuser " + "(cn,sn) " + "VALUES(?,?)"; 
      PreparedStatement pstmt = conn.prepareStatement(sql); 
      pstmt.setString(1, cn); 
      pstmt.setString(2, sn); 
      pstmt.executeUpdate(); 
      pstmt.close(); 
     } catch (SQLException e) { 
      System.err.println("ERROR: " + e.getMessage()); 
     } 
    } 
} 
/

我創建將運行上面像一個過程;

CREATE OR REPLACE PROCEDURE read_ldap 
    AS LANGUAGE JAVA 
    NAME 'RetreiveLdap.readConfig()'; 
/

現在,當我從Java它工作正常,正常運行這個類,它檢索LDAP用戶的列表,並將它們保存到數據庫,(沒有創建或替換,當然...)

但是,當我將它作爲Oracle中的一個過程運行時,用戶表單LDAP不會被添加到表中。我已經添加了addUser("tmp", "tmp");只是爲了查看我的代碼是否執行正常,並且此線路用戶確實已插入,但沒有在while循環中

有沒有辦法在運行此過程時看到一些錯誤?

回答

4

我在SQLPLUS添加了以下

SQL> SET SERVEROUTPUT ON 
SQL> CALL dbms_java.set_output(2000); 

,我能夠看到現在的錯誤,這是關係到我的數據庫用戶的訪問插座的權限。