基于 Weblogic 7.0 的应用开发 - 中国WEB开发者网络 (http://www.webasp.net) -- 技术教程 (http://www.webasp.net/article/) --- 基于 Weblogic 7.0 的应用开发 (http://www.webasp.net/article/8/7031.htm) |
| -- 作者:未知 -- 发布日期: 2003-07-26 |
| 1. 数据库连接 本文将以SQL Server数据库为例,讲述通过ConnectionPool And DataSource访问数据库的方法。 1. 安装微软提供的驱动程序 安装JDBC For SQL Server的驱动程序,从微软的网站上http://www.microsoft.com/downloads/details.aspx?FamilyID=4f8f2f01-1ed7-4c4d-8f7b-3d47969e66ae&DisplayLang=en下载JDBC驱动程序,点击setup.exe,安装驱动程序。修改WebLogic 7的Classpath,加入 %SQLServer_JDBC%\lib\msbase.jar; %SQLServer_JDBC%\lib\mssqlserver.jar; %SQLServer_JDBC%\lib\msutil.jar ,重新启动WebLogic 2. 配置WebLogic。 具体步骤: ConnectionPool与DataSource的联合使用,步骤: 1) 建立一个连接池(ConnectionPool). a.在Console的菜单中选择,Services/JDBC/Connection Pools b.点击Configure a new JDBC Connection Pool... c.在Configuration/General页签中填写(这里根据不同的JDBC驱动填写的参数不同) 方法一:使用微软的JDBC For SQL Server驱动程序: Name:SQLServerPool URL:jdbc: jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master Driver Classname: com.microsoft.jdbc.sqlserver.SQLServerDriver Properties(key=value): user=sa password=sa 方法一:使用BEA的JDBC For SQL Server驱动程序(存在中文问题) Name:SQLServerPool URL:jdbc: jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master Driver Classname: com.microsoft.jdbc.sqlserver.SQLServerDriver Properties(key=value): user=sa password=sa d.点击Create e.Configuration/Connections Tab: Initial Capacity:1 Maximum Capacity:5 其余默认 f.点击Apply g.Targets/Servers Tab: 选择myserver,点击Apply!如果,没有报错,即证明创建连接池成功! 2) 将连接池映射成数据源(DataSource). a.在Console的菜单中选择,Services/jdbc/Data Sources b.点击Configure a new JDBC Data Source... c.Configuration Tab: Name:SQLServerDataSource JNDI Name: SQLServer Pool Name: SQLServerPool d.点击Create e.Targets/Services Tab: 选择myserver,点击Apply!如果,没有报错,即证明创建数据源成功! 3. 编写测试程序 (其中涉及到JNDI的问题,其实很简单,就那么几步,记住就OK!): import java.sql.*; import java.util.*; import javax.naming.*; import javax.sql.*; public class DataSourceTest { public static void main(String[] args) { Statement stmt = null; Connection conn = null; ResultSet res = null; try{ Hashtable env = new Hashtable(); env.put(InitialContext.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory"); env.put(InitialContext.PROVIDER_URL,"t3://localhost:7001"); //webLogic端口IP env.put(InitialContext.SECURITY_PRINCIPAL,"system"); //webLogic连接用户 env.put(InitialContext.SECURITY_CREDENTIALS,"sysmanager");//webLogic密码 InitialContext ctx = new InitialContext(env); DataSource ds = (DataSource)ctx.lookup("SQLServer"); //JNDI名字 conn = ds.getConnection(); stmt = conn.createStatement(); res = stmt.executeQuery("select * from testtable"); System.out.println("id------name------address"); while(res.next()){ int id = res.getInt(1); String name = res.getString(2).trim(); String address = res.getString(3).trim(); System.out.println(id+"------"+name+"------"+address); } } catch(SQLException sse){ System.out.println("sql error!"); } catch(NamingException e){ System.out.println("namingexception"); } try{ stmt.close(); conn.close(); } catch(SQLException se){} } } 附: 连接Oracle 数据库的设置 拷贝Oracle安装目录下面的classes12.zip,在WebLogic的Classpath添加该包作为驱动程序。 在Weblogic中的ConnectionPool的Configuration/General页签中填写如下参数 Name:OraclerPool URL: jdbc:oracle:thin:@[Oracle服务器IP]:1521:[服务名] Driver Classname: oracle.jdbc.driver.OracleDriver Properties(key=value): user=[oracle用户] password=[oracle用户密码] dll=ocijdbc8 protocol=thin 其它可设置可参考上文中SQL Server的设置。 连接MySQL数据库的设置 略 备注: 使用MS JDBC For SQL Server和BEA JDBC For SQL Server的缺陷(未写) |
| webasp.net |