简体   繁体   English

函数返回结果集,进入另一个类-JAVA

[英]function return resultset, get in another class - JAVA

I am new in Java. 我是Java新手。 I am doing this thing it creates error, please let me know why I am going in this function dispatchUncaughtException . 我正在做的事情会产生错误,请让我知道为什么我要使用此函数dispatchUncaughtException Am I doing right ? 我做对了吗? If I am not so how can this thing possible ? 如果不是这样,怎么可能呢?

This is dataCollection Class 这是dataCollection类

public class dataCollection {

    private ResultSet my_rs1 =  null;

    public ResultSet mydata()
    {
        try
        {
            String get_data_query = "SELECT * FROM my_table ";
            my_rs1 = cn.stmt.executeQuery(aggregator_data_query);

        } catch(SQLException SQLex)
        {
            System.out.println("SQL Error: " + SQLex.getMessage());
        }

        return my_rs1;        

    }

}

This is callingClass // where we call ResultSet 这是CallingClass //,我们在其中调用ResultSet

public class callingClass
{
    dataCollection dc = new dataCollection;
    private ResultSet my_call_data = dc.mydata();
    while(my_call_data.next())
    {
        //Code Here
    }
}

EDIT 编辑

Exception in thread "Thread-2" java.lang.NullPointerException
    at sms_sender.dataCollection.aggregator_data(dataCollection.java:29)
    at sms_sender.threadClass.run(threadClass.java:27)

correct your parameter : 更正您的参数:

String get_data_query = "SELECT * FROM my_table";
my_rs1 = cn.stmt.executeQuery(get_data_query);

Edit: Step 2 => Check your db connection its working properly. 编辑:步骤2 =>检查您的数据库连接是否正常工作。 i think your stmt is empty or incorrect. 我认为您的stmt为空或不正确。

code should be in function or block in java. 代码应该在Java中的函数或块中。

    public class callingClass
    {
        dataCollection dc = new dataCollection;
        private ResultSet my_call_data;

        public void showData(){
        my_call_data = dc.mydata();
         while(my_call_data.next()){
            //Code Here
         }
       }

    }

What is happening is that you are not catching an Exception so the Thread.UncaughtExceptionHandler will try to forward this Exception. 发生的情况是您没有捕获到异常,因此Thread.UncaughtExceptionHandler将尝试转发此异常。

First try to catch the Exception and give us the stacktrace : 首先尝试捕获异常并为我们提供stacktrace:

catch (Exception e) {
  e.printStackTrace(); // if you are not using a logger like log4j just print it.
}

Then i would recommend that you read more about Exceptions 然后,我建议您阅读有关异常的更多信息

And finally, as a notice class names should begin by a Capital letter see Naming Conventions . 最后,通知类名称应以大写字母开头,请参阅命名约定

Good reading :) 好书:)

PS : i did not notice that your code was not in a method / function, but i suppose this is just a bad edit as the class would not even compile. PS:我没有注意到您的代码不在方法/函数中,但是我认为这是一个不好的编辑,因为该类甚至无法编译。

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

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