如题,该程序在windows下运行正常,但是在linux下能正常编译,却无法运行,则报错。
该javabean代码如下:
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.*;
import java.util.*;public class sqlGetName{
     /*数据库连接对象*/   
    Connection   conn=null; 
    ResultSet rs=null;    //在构造函数中连接数据库
    public   sqlGetName()
    {     
        try
        {   
               Class.forName("com.mysql.jdbc.Driver"); //加载驱动程序
               conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bird?useUnicode=true&characterEncoding=gb2312","root","recall");
               if(!conn.isClosed())
                   System.out.println("Succeeded connecting to the Database!");
        }catch(Exception   e)
        {  
            System.err.println("sqlGetName error: "+e.getMessage());
        }   
    }   
    
    /*关闭连接*/   
    public    void CloseConnection()
    {   
        try
        {   
            conn.close(); 
        }catch(Exception   e)
        {
            System.err.println("CloseConnection error: "+e.getMessage());
        }   
    }       public ResultSet executeQuery(String sql)
    {
        try
        {
            Statement stmt=conn.createStatement();
            rs=stmt.executeQuery(sql);    //结果集
        }catch(SQLException ex)
        {
            System.err.println("sql= "+sql);
            System.err.println("sqlGetName.executeQuery: "+ex.getMessage());
        }
        return rs;        
    }
}
在网页中的提示信息为:java.lang.NullPointerException
    sqlGetName.executeQuery(sqlGetName.java:49)
其中第49行实际上是executeQuery函数的Statement stmt=conn.createStatement();
根据我自己的判断,应该是由于conn为null导致出错的,但是不知道怎么解决。
另外,我通过java程序直接连接数据库并查询,完全没有问题。
以上问题还请大侠指导,多谢了。
使用的是sunjava 的1.6.0.13版本的jdk

解决方案 »

  1.   

      try
            { 
                  Class.forName("com.mysql.jdbc.Driver"); //加载驱动程序
                  conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bird?useUnicode=true&characterEncoding=gb2312","root","recall");
                  if(!conn.isClosed())
                      System.out.println("Succeeded connecting to the Database!");
            }catch(Exception  e)
            { 
                System.err.println("sqlGetName error: "+e.getMessage());
            }  如果你创建conn有问题的话是会有"sqlGetName error: ......"信息的,你看到了吗?   把它放上来吧。
      

  2.   

    谢谢楼上的回复。但是因为上述代码是作为javabean使用的,通过jsp页面调用javabean来使用。所以System.out.println的信息没法看到。在tomcat的log信息中也没看到
    谢谢楼上的朋友了
      

  3.   

    你好。我将信息输出到文件当中,发现报错信息如下:
    sqlGetName.sqlGetName: Communications link failure due to underlying exception:** BEGIN NESTED EXCEPTION **java.net.SocketException
    MESSAGE: java.security.AccessControlException: access denied (java.net.SocketPermission 192.168.233.130:3306 connect,resolve)STACKTRACE:java.net.SocketException: java.security.AccessControlException: access denied (java.net.SocketPermission 192.168.233.130:3306 connect,resolve)
            at com.mysql.jdbc.StandardSocketFactory.unwrapExceptionToProperClassAndThrowIt(StandardSocketFactory.java:407)
            at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:268)
            at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
            at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
            at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
            at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
            at java.sql.DriverManager.getConnection(DriverManager.java:582)
            at java.sql.DriverManager.getConnection(DriverManager.java:185)
            at birdidentify.sqlGetName.<init>(sqlGetName.java:24)
            at org.apache.jsp.insertgene_jsp._jspService(insertgene_jsp.java:64)
            at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
            at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
            at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
            at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:597)
            at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
            at java.security.AccessController.doPrivileged(Native Method)
            at javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
            at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
            at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:283)
            at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:56)
            at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:189)
            at java.security.AccessController.doPrivileged(Native Method)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:185)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
            at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
            at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
            at java.lang.Thread.run(Thread.java:619)
    ** END NESTED EXCEPTION **Last packet sent to the server was 2 ms ago.