Hughes
发贴: 1
积分: 0
|
于 2004-12-01 16:09
本人是个菜鸟,刚学不久,日前遇到一个问题怎么也搞不懂,还请大虾帮忙。 按书上写了一个使用Jdbc的例子,但怎么也调不通。faint! 数据库:Jbuilder9自带JDataStore. weblogic版本:8.1 连接池已经设定好 Name:JDataStorePool server:myserver DataSouce: Name:JDataStoreDataSource JNDI Name:JDataStoreDataSource PoolName:JDataStorePool target:myserver Weblogic启动后,JDataStore显示已经连接成功。
但一下程序(按照书上打的,怎么也跳不通,晕) package jdbctest1; import java.sql.*; import javax.naming.*; import javax.sql.*; import java.util.Properties; import javax.rmi.PortableRemoteObject; import java.sql.Connection;
public class test { public test() { } private static Context getInitialContext() throws Exception { String url="t3://localhost:7001"; String user="system"; String password="security"; Properties properties=null; try{ properties=new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLIntialContextFactory"); properties.put(Context.PROVIDER_URL,url); if (user!=null) { properties.put(Context.SECURITY_PRINCIPAL,user); properties.put(Context.SECURITY_CREDENTIALS,password==null?"":password); }
return new InitialContext(properties);/**调式后,断定错误应该在这里 }catch(Exception e){ System.out.println("InitialContext Error:"+e); throw e; } }
public static void main(String argv[]){ DataSource ds=null; Context ctx=null; Connection myconn=null; try{ ctx=getInitialContext(); //ctx=new InitialContext(); ds=(javax.sql.DataSource)ctx.lookup("JDataStoreDataSource"); } catch(Exception E) { System.out.println("Init Error:"+E); } Statement myStatement=null; ResultSet myResultSet=null; Connection myConn=null; try{ myConn=ds.getConnection(); myStatement=myConn.createStatement(); myResultSet=myStatement.executeQuery("select full_name from employee"); while(myResultSet.next()) { System.out.println("the employee full name is"+myResultSet.getString("full_name")); } myResultSet.close(); }catch(SQLException e){ System.out.println("Error code="+e.getErrorCode()); System.out.println("Error message="+e.getMessage()); } finally { try{ if(myStatement!=null) { myStatement.close(); } if(myConn!=null) { myConn.close(); } }catch(SQLException e){ System.out.println("Error code="+e.getErrorCode()); System.out.println("Error message="+e.getMessage()); } } } }
报错: javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLIntialContextFactory [Root exception is java.lang.ClassNotFoundException: weblogic.jndi.WLIntialContextFactory]
|