简体   繁体   English

空指针异常抛出getConnection()

[英]Null Pointer Exception throws in getConnection()

I have the followiing situation: I have a java code which is connected with a database (MySQL). 我有以下情况:我有一个与数据库(MySQL)连接的Java代码。 When i am running the java code from eclipse and taking data from database, everything is ok. 当我从Eclipse运行Java代码并从数据库中获取数据时,一切正常。 But when i am running java code from html code as Applet, it throws Null Pointer Exception and i detect that problem in this case occur to the following java code: 但是,当我从Applet的html代码运行Java代码时,它将引发Null Pointer Exception,并且在以下情况下,我检测到以下Java代码会发生该问题:

private  Connection getConnection() throws SQLException{
   try {
        Class.forName("com.mysql.jdbc.Driver");
        String name="jdbc:mysql://localhost:3306/sensors_data";
        String username="root";
        String psw="11111";

        conn = DriverManager.getConnection(name,username,psw);
   } catch(Exception e) {
        e.printStackTrace();
   }
   return conn;

} }

The object "conn" is null. 对象“ conn”为空。

Well, if DriverManager.getConnection() throws an exception, you're printing out the exception but then returning conn anyway. 好吧,如果DriverManager.getConnection()引发异常,则说明您正在打印异常,但无论如何都会返回conn You should probably be letting the exception propagate to the caller - after all, it's not like the caller now has a connection they can use. 您可能应该让异常传播给调用者-毕竟,这与调用者现在可以使用的连接不同。

(You're also catching any exception, instead of just SQLException , which is a bad idea.) (您还捕获了所有异常,而不仅仅是SQLException ,这是一个坏主意。)

I wouldn't expect to be able to connect directly to a database from an applet though - I can't remember the exact restrictions on applet network connections, but that may well be the problem. 我不希望能够从applet直接连接到数据库-我不记得对applet网络连接的确切限制,但这很可能就是问题所在。 Where is the database running compared with the web server? 与Web服务器相比,数据库在哪里运行? If it's not on the same host that the applet is published from, I wouldn't expect it to be able to make a connection at all without special permissions. 如果它不在发布该applet的同一主机上,那么我将不希望它在没有特殊权限的情况下完全能够建立连接。 (Trying to connect to localhost is particularly unlikely to work, I suspect.) (我怀疑尝试连接到localhost特别不可能。)

You should be able to see the exception thrown by DriverManager.getConnection in your debug log of course - that should be the first port of call, as well as fixing the exception handling. 您当然应该能够在调试日志中看到DriverManager.getConnection引发的异常-它应该是调用的第一个端口,并且可以修复异常处理。

将localhost替换为数据库所在的计算机名称,然后重试。

The problem is that you should return the conn inside the try{} thread. 问题是您应该在try {}线程内返回conn。 Or, you can create a connection variable as a class field, and then assign the conn value to the connection inside the try{} tread, then problem will be solved hopefully. 或者,您可以将连接变量创建为类字段,然后将conn值分配给try {}踏面内的连接,这样就有望解决问题。

    public static Connection conn1 = null;

public static Connection getConnection(){
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection conn = DriverManager.getConnection 
                ("jdbc:mysql://localhost/?user=root&password=root");
        if(conn != null){
            System.out.println("Connect database successfully.");
            conn1 = conn;
        }
        else System.out.println("Connection failed...");
        return conn;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM