我写了一个连接数据库的例子结果总是报错,请高手给看看 郁闷死了,
下面我把 程序代码和报错的内容贴出来,哥哥姐姐们千万给帮个忙,
init err:javax.naming.NoInitialContextException: Need to specify class name in
environment or system property, or as an applet parameter, or in an application
resource file: java.naming.factory.initial
Exception in thread "main" java.lang.NullPointerException
at jdbctest.Jdbc1.<init>(Jdbc1.java:26)
at jdbctest.Jdbc1.main(Jdbc1.java:63)源码:
package jdbctest;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import java.util.Properties;
public class Jdbc1 { public Jdbc1() {
DataSource ds=null;
Context ctx=null;
Connection mycon=null;
try {
ctx=getInitialContext();
ds=(javax.sql.DataSource)ctx.lookup("dsjdatastore");
} catch ( Exception e ){
System.out.println("init err:"+e);
}
Statement mysta=null;
ResultSet myres1=null;
ResultSet myres2=null;
DatabaseMetaData mybase=null;
try {
mycon=ds.getConnection();
mysta=mycon.createStatement();
myres1=mysta.executeQuery("select * from why");
while (myres1.next())
{
System.out.println(myres1.getObject(1));
System.out.println();
}
myres1.close();
}catch (SQLException e)
{
System.out.println("err code :"+e.getErrorCode());
System.out.println("err message :"+e.getMessage());
}
finally {
try {
if (mysta!=null){
mysta.close();
}
if(mycon!=null)
{
mycon.close();
}
} catch (SQLException e )
{
System.out.println("err code:"+e.getErrorCode());
System.out.println("err message:"+e.getErrorCode());
}
}
}public static void main( String [] args)
{
Jdbc1 test =new Jdbc1();
} /**
* getInitialContext
*
* @return Context
*/
public static Context getInitialContext() throws Exception {
String url="t3://localhost:7001";
String user="weblogic";
String password="weblogic";
Properties prop=null;
try {
prop=new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
prop.put(Context.PROVIDER_URL,url);
if(user!=null)
{
prop.put(Context.SECURITY_PRINCIPAL,user);
prop.put(Context.SECURITY_CREDENTIALS,password==null ?"":password);
}
return new InitialContext (prop);
}catch (Exception e ){
throw e ;
} }}
下面我把 程序代码和报错的内容贴出来,哥哥姐姐们千万给帮个忙,
init err:javax.naming.NoInitialContextException: Need to specify class name in
environment or system property, or as an applet parameter, or in an application
resource file: java.naming.factory.initial
Exception in thread "main" java.lang.NullPointerException
at jdbctest.Jdbc1.<init>(Jdbc1.java:26)
at jdbctest.Jdbc1.main(Jdbc1.java:63)源码:
package jdbctest;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import java.util.Properties;
public class Jdbc1 { public Jdbc1() {
DataSource ds=null;
Context ctx=null;
Connection mycon=null;
try {
ctx=getInitialContext();
ds=(javax.sql.DataSource)ctx.lookup("dsjdatastore");
} catch ( Exception e ){
System.out.println("init err:"+e);
}
Statement mysta=null;
ResultSet myres1=null;
ResultSet myres2=null;
DatabaseMetaData mybase=null;
try {
mycon=ds.getConnection();
mysta=mycon.createStatement();
myres1=mysta.executeQuery("select * from why");
while (myres1.next())
{
System.out.println(myres1.getObject(1));
System.out.println();
}
myres1.close();
}catch (SQLException e)
{
System.out.println("err code :"+e.getErrorCode());
System.out.println("err message :"+e.getMessage());
}
finally {
try {
if (mysta!=null){
mysta.close();
}
if(mycon!=null)
{
mycon.close();
}
} catch (SQLException e )
{
System.out.println("err code:"+e.getErrorCode());
System.out.println("err message:"+e.getErrorCode());
}
}
}public static void main( String [] args)
{
Jdbc1 test =new Jdbc1();
} /**
* getInitialContext
*
* @return Context
*/
public static Context getInitialContext() throws Exception {
String url="t3://localhost:7001";
String user="weblogic";
String password="weblogic";
Properties prop=null;
try {
prop=new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
prop.put(Context.PROVIDER_URL,url);
if(user!=null)
{
prop.put(Context.SECURITY_PRINCIPAL,user);
prop.put(Context.SECURITY_CREDENTIALS,password==null ?"":password);
}
return new InitialContext (prop);
}catch (Exception e ){
throw e ;
} }}
解决方案 »
- 问个问题,该选择什么方案?
- 如何得到一个JTree上的TreePath?在线等!
- 哪里有sql server2005的驱动jar包下载
- float型数据怎么转换成字符串
- 怎样才能知道弹出菜单是在哪个文本框弹出的?
- “高分求解” 如何在JBuilder中调整JButton上的文本随着JButton的大小改变而改变!!
- 关于多线程的问题?
- 急!关于sdk环境配置的很奇怪的问题,大家帮帮我这个初学者吧。
- 如何建立一个圆形按钮,当单击它时,在右边的PANEL中画出一个圆,并且这个圆可以移动和改变大小?
- JBUILDER如何执行ORACLE的储存过程
- 求与数据库有关的问题
- 一个初学者配置JAVA环境的疑惑!
init err:javax.naming.NoInitialContextException: Cannot instantiate class: weblo
gic.jndi.WLInitialContextFactory [Root exception is java.lang.ClassNotFoundExcep
tion: weblogic.jndi.WLInitialContextFactory]
Exception in thread "main" java.lang.NullPointerException
at jdbctest.Jdbc1.<init>(Jdbc1.java:25)
at jdbctest.Jdbc1.main(Jdbc1.java:62)
实在抱歉我都弄蒙了
http://www.hmilyld.cn/post/398.htm
看下这个...- -#
ctx=getInitialContext();
ds=(javax.sql.DataSource)ctx.lookup("dsjdatastore");
}改成:
try {
ctx=new InitialContext();
ds=(javax.sql.DataSource)ctx.lookup("dsjdatastore");
就ok了
init err:javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
Exception in thread "main" java.lang.NullPointerException
at jdbctest.Jdbc1.<init>(Jdbc1.java:26)
at jdbctest.Jdbc1.main(Jdbc1.java:63)
就是我开始贴的那个报错信息,还可能那里出问题呢,我用的是weblogic测试连接的时候显示成功啊,还有没有别的可能呢,谢谢!!!