Java存储过程

发布于 2024-09-04 16:22:42 字数 3117 浏览 8 评论 0原文

我有以下 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 循环中没有

一个当我运行这个程序时,如何查看一些错误?

I have the following Java stored procedure:

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());
        }
    }
}
/

And I create a procedure that will run the above like;

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

Now, when I run this class normally from java it works fine, it retrieves the list of LDAP user and saves them into the DB, (of course without the create or replace...)

But when I run it as a procedure in Oracle the users form LDAP do not get added to the table. I have added the addUser("tmp", "tmp"); just to see if my codes executes well, and with this line a user does get inserted, but none in the while loop

Is there a way to see some errors, when I run this procedure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江湖彼岸 2024-09-11 16:22:43

我在 SQLPLUS 中添加了以下内容

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

,现在我可以看到错误,它与我的数据库用户访问套接字的权限有关。

I have added in SQLPLUS the following

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

And I am able now to see the errors, it was related to the permission of my db user to access sockets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文