Java overloads (Implementing the same method name, different parameters)

Source: Internet
Author: User

background:A few days ago when you write a database connection, because you want to execute SQL, some point to get all the execution results, or to get the results of a field based on the results of the execution. At this point I want to get different results by using the same method name, different parameters. It turns out that Java's method cannot set the default value = = for the parameter, but can implement the process by overloading. (Of course, I do not rule out the reason I do not understand Java, if you have any way to tell me, thank you ~)  overloading: can be understood as the same method name, different parameters, approximate examples are as follows:def void A (int i) {#使用参数i的方法A}def void A (int j,int k) {#使用参数j和k的方法A}  Note: This can be done by invoking the same method name, giving different parameters, and implementing different effects.   Special Note: if the methods and parameters are the same, there can only be one method, because I began to want to return the result of int and string two types by the same parameter, the same method name, so it is not possible to redefine only one method name. Attach the error example:public int A (String i) {return the result of an int type}Public string A (String i) {return the result of a string type}  Finally, I enclose a result of SQL execution that I implemented by overloading, one that only gets the results of the query, and one that gets the content of the specific field:Public ResultSet GetResult (String sql) {
try {
this.rs = this.stmt.executeQuery (sql);
//System.out.print (this.rs);
//while (Rs.next ()) {
//Long info_id = Rs.getlong ("info_id");
//System.out.println ("\ninfo_id:" + info_id);
//                }
return this.rs;
}catch (SQLException e) {
System.out.println ("the field to find is not found in SQL!") ");
e.printstacktrace ();
return this.rs;
        }
    }
//This method is directly get the field name to find the corresponding result, only take one, so will continue to overwrite, take to the last, trial and SQL results only one case, if the result is a lot of data, need to use Excutesql method
Public string GetResult (String sql,string key) {
try {
this.rs = this.stmt.executeQuery (sql);
}catch (SQLException e) {
System.out.println ("the field to find is not found in SQL!") ");
e.printstacktrace ();
        }
try{
While (This.rs.next ()) {
This.result = this.rs.getString (key);
            }
}catch (Exception e) {
System.out.println ("SQL Gets the result exception!");
e.printstacktrace ();
        }
return this.result;
    }

Java overloads (Implementing the same method name, different parameters)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.