Java 注册oracle.jdbc.OracleDriver

Java 注册oracle.jdbc.OracleDriver,java,oracle,jdbc,Java,Oracle,Jdbc,我不熟悉JavaWeb服务。我已经玩了几天,终于可以在另一台机器上连接到Oracle数据库。到目前为止我很高兴 到目前为止,我在web上看到的示例表明,您只需要注册oracle.jdbc.OracleDriver一次 如何在web服务上做到这一点?现在,每次调用函数(例如getUserFullName)时,我都会注册它 欢迎您的任何意见 编辑: 以下是其中一个功能: public static String getUserName(int id) throws SQLException {

我不熟悉JavaWeb服务。我已经玩了几天,终于可以在另一台机器上连接到Oracle数据库。到目前为止我很高兴

到目前为止,我在web上看到的示例表明,您只需要注册oracle.jdbc.OracleDriver一次

如何在web服务上做到这一点?现在,每次调用函数(例如getUserFullName)时,我都会注册它

欢迎您的任何意见

编辑: 以下是其中一个功能:

public static String getUserName(int id) throws SQLException {
    String returnValue = "";
    Statement stmt = null;
    ResultSet rset = null;
    Connection conn = null; 

    try {
        DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
        conn = DriverManager.getConnection("jdbc:oracle:thin:@//myOracleIP:1521/myOracleDB", "admin", "password");
        stmt = conn.createStatement();
        rset = stmt.executeQuery("select name from tbl_users where id = " + id);

        while(rset.next()) {
            returnValue = rset.getString("name");
        }
    }
    catch(Exception ex) {
        returnValue = "jdbc.getUserName -- Exception: " + ex.getMessage();
    }
    finally {
        // close resultset
        if(rset != null)
            if(!rset.isClosed())
                rset.close();
        // close statement
        if(stmt != null)
            if(!stmt.isClosed())
                stmt.close();
        // close connection
        if(conn != null)
            if(!conn.isClosed())
                conn.close();
    }

    stmt = null;
    rset = null;
    conn = null;

    return returnValue;
}
JNDI函数

public static String getNameWithJNDI(int id) throws SQLException {
    int statusCode = 0;
    String returnValue = "Open DB";
    DataSource dc = null;
    Statement stmt = null;
    ResultSet rset = null;
    Connection conn = null;
    Context context = null;

    try {
        context = new InitialContext();
        // my datasource from the GlassFish 
        dc = (DataSource)context.lookup("jdbc/myConnection");
        context.close();
    }
    catch(NamingException e) {
        statusCode = 1;
        returnValue = "jdbc.GetNameWithJNDI - InitialContext Error: " + e.getMessage();
    }

    if((statusCode == 0) && (dc != null)) {
        try {
            conn = dc.getConnection();
            stmt = conn.createStatement();

            rset = stmt.executeQuery("select name from tbl_users where id = " + id);

            if(rset != null) {
                while(rset.next()) {
                    returnValue = "JNDI: " + rset.getString("name");
                }
            }
        }
        catch(SQLException e) {
            statusCode = 1;
            returnValue = "jdbc.GetNameWithJNDI - Database Error: " + e.getMessage();  
        }
        finally {
            // close resultset
            if(rset != null)
                if(!rset.isClosed())
                    rset.close();
            // close statement
            if(stmt != null)
                if(!stmt.isClosed())
                    stmt.close();
            // close connection
            if(conn != null)
                if(!conn.isClosed())
                    conn.close();
        }
    }



    dc = null;
    stmt = null;
    rset = null;
    conn = null;
    context = null;

    return returnValue;     
}

JDBC4兼容的驱动程序不需要注册。如果您使用的是应用程序服务器,通常的方法是在应用程序服务器中创建
数据源
,并从应用程序中使用该数据源(通过使用JNDI引用它),而不是使用
DriverManager
创建连接。应用服务器
DataSource
通常还负责连接池之类的事情。

每次需要使用JDBC资源时,我都会使用JNDI查找,特别是如果您使用的是Java应用服务器,看起来是这样(因为您正在执行Web服务)。这意味着您不必每次都继续加载JDBC驱动程序。例如,您可能希望查看以下内容:


连接池也可以在下面的文章中找到:

您到底是如何注册它的??什么类型的功能?类代码,还有你问题的web服务部分,我将.war web服务发布到我的开发PC上的GlashFish4开源虚拟机上;Oracle数据库位于另一台PC中。在这个java世界中,一切都是新的。谢谢。我如何知道我是否在运行应用服务器,是否在使用GlassFish4开源虚拟机?blackpanther:谢谢你的URL。关于如何使用连接池的好例子。我肯定会用的。谢谢你的反馈。我也会考虑你的建议。马克,谢谢你的建议。我在Glassfish vm中创建了数据源,并在webservice中创建了一个新函数来利用该数据源,它可以正常工作。我在上面添加了新函数。