各位十一好,问一个有关weblogic 8的连接池的问题

05-10-04 yxb335
我的问题是这样的:我在weblogic server console中已经建好了一个,Connection Pool,并且利用它建立一个DataSourses,在weblogic

server console中Testing是的结果是:Connection successful on: myserver。

现在我想写一个Java程序来测试一下,我的程序是:

可是怎么就是运行不出结果,请问各位高手,怎么写测试的程序,就是如何通过DataSources来读出数据库中的数据呢?我搞了很长时间,可是就是出不来,请问各位怎么写呢??谢谢!!

import java.sql.*;
import javax.naming.*;
import javax.naming.InitialContext;
import javax.sql.DataSource;



public class TestConnection {
	public static void main(String[] args) 
	throws Exception
	{
		// TODO: Add your code here
	    Connection myConn=null;
	    DataSource  ds=null; 
             Context ctx=new InitialContext();
             ds=(javax.sql.DataSource)ctx.lookup("MyJDBC Data Source");
	    myConn=ds.getConnection();
	    Statement stmt=myConn.createStatement();
	    ResultSet rs=stmt.getResultSet();
	    rs=stmt.executeQuery("select * from MB_Picture");
	    while(rs.next())
	    {
	   System.out.print(rs.getString("MB_Picture_Name"));
	    }
       	
      }
    }
  

kevinzhou
2005-10-04 11:03
报错了没有?

hashtable env = new Hashtable()

env.put(Context.INITIAL_CONTEXT_FACTORY,proviederFactory);

env.put(Context.PROVIDER_URL, proviederUrl);

proviederFactory=weblogic.jndi.WLInitialContextFactory

proviederUrl=t3://localhost:7001

yxb335
2005-10-04 18:27
首先谢谢您,

这样我方法,我也尝试过:我的程序如下:(在JBuilder中):

package testone;

import java.sql.*;
import javax.naming.*;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.util.*;


public class TestConnection {
        public static void main(String[] args)
        throws Exception
        {
            Connection myConn=null;
            DataSource  ds=null;
           Context ctx=getInitialContext();
           ds=(javax.sql.DataSource)ctx.lookup("MyJDBC Data Source");
            myConn=ds.getConnection();
            Statement stmt=myConn.createStatement();
            ResultSet rs=stmt.getResultSet();
            rs=stmt.executeQuery("select * from MB_Picture");
            System.out.print(123);
            while(rs.next())
            {

                System.out.print(rs.getString("MB_Picture_Name"));
            }
   }

public static Context getInitialContext() throws Exception
       {
        String url="t3://localhost:7001";
        String user="335202335202";
        String password="335202335202";
        Properties properties=null;
        try
        {
                properties=new Properties();
                properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
                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)
        {
                throw e;
        }


      }
    }



<p>

可是出现的错误是:

javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFactory [Root exception is java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory]

	at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:652)

	at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)

	at javax.naming.InitialContext.init(InitialContext.java:219)

	at javax.naming.InitialContext.<init>(InitialContext.java:195)

	at testone.TestConnection.getInitialContext(TestConnection.java:61)

	at testone.TestConnection.main(TestConnection.java:20)

Caused by: java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory

	at java.net.URLClassLoader$1.run(URLClassLoader.java:199)

	at java.security.AccessController.doPrivileged(Native Method)

	at java.net.URLClassLoader.findClass(URLClassLoader.java:187)

	at java.lang.ClassLoader.loadClass(ClassLoader.java:289)

	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)

	at java.lang.ClassLoader.loadClass(ClassLoader.java:235)

	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)

	at java.lang.Class.forName0(Native Method)

	at java.lang.Class.forName(Class.java:219)

	at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:42)

	at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:649)

	... 5 more

Exception in thread "main" 
<p>

我不知道是为什么会出现这种错误,是不是有weblogic.jndi.WLInitialContextFactory类啊,不然为什么会出现:javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFactory 的异常,所以还请高手多多指点指点,我在这里先谢谢了!!

kevinzhou
2005-10-05 08:04
weblogic.jar有没有包括在classpath里面呢?

另外connection别忘了关掉。

yxb335
2005-10-05 09:13
十又分感谢您在十一期间还回答我的问题,的确是classpath没有设置的问题,我尝试了,可以成功了,谢谢您,十一快乐!!Thank you very much!!

猜你喜欢
2Go 1 2 下一页