1

I have a very simple Java applet that just works.

import java.sql.*;
import org.apache.commons.lang3.*;

public class doQuery {

    public static void main (String[] args) {
        ...
        try {
            ...
        } catch (Exception e) {
            ...
        }
        try {
                ...
            try {
                ...
            } catch (SQLException e) {
                ...
            } finally {
                ...
            }

        } catch (SQLException e) {
            ...
        }

    }
}

It allows me to open a database, do some queries, and perform a series of outputs that is captured through stdio of a bash script, connections closed and then bash script emails the output.

However, I am looking to expand it, and I am stuck. I am programmer, just not a Java programmer. What I have come up with is something I hacked together. I want to add some functions, and more. I have tried to the function definitions in different places in the code, but it always generates compilation errors.

Can anyone provide some insight as to what I can change to enable me to add some functions? Generally programming say define the function before you attempt to use it, but I probably am not using the right keywords or something.

I can not figure out where to place a simple function like:

function display_number(number) {
   return number + "";
}

in the source code that I can call and have it compile! :(

jewettg
  • 1,098
  • 11
  • 20
  • 2
    I'm not sure what you mean by "applet". You have a skeleton of a simple Java application. Java methods don't start with "function". They start with a access level and then a return type, or void. – Gilbert Le Blanc Jun 12 '15 at 16:46
  • Thanks Gilbert - I was going off this link: http://stackoverflow.com/questions/1267283/how-can-i-create-a-zerofilled-value-using-javascript where I am trying to utilize one of these functions. I use function as a generic term, any ideas? I am still searching/googling, have not found anything.. – jewettg Jun 12 '15 at 16:48
  • 1
    @jewettg the link you posted is for JavaScript... Java is not the same. – Sh4d0wsPlyr Jun 12 '15 at 16:50
  • UGH! ..and duh on my part! -- WOW! Sorry for that! I am confusing compilation errors with trying to do to much with the wrong language.. grr! – jewettg Jun 12 '15 at 16:56

2 Answers2

1

If you just want a quick and easy function, here is a layout you can use.

public void display_number(int number) {
  System.out.print(number);
}

Or if you prefer returning the number to use it somewhere else...

public int display_number() {
  return number;
}

Your methods can be basically anywhere between the start { and the end } of your class, assuming you do not place it inside another method (such as the main method from your code). As far as anything else I recommend reading up on the subject, I am sure a google search will give you millions of examples of method calls.

Sh4d0wsPlyr
  • 948
  • 12
  • 28
  • Thank you Sh4d0wsPlyr - I have been googling, but it just took someone to mention clearly what I needed. – jewettg Jun 12 '15 at 16:53
  • The problem I was having was declaring the local variable types of the public void ___ (INT ___). That along with placement and I was trying to do JavaScript in Java! :-( – jewettg Jun 12 '15 at 17:00
1
import java.sql.*;
import org.apache.commons.lang3.*;

public class doQuery {

    public static void main (String[] args) {
        ...
        try {
            ...
        } catch (Exception e) {
            ...
        }
        try {
            ...
            try {
                ...
            } catch (SQLException e) {
                ...
            } finally {
                ...
            }
        } catch (SQLException e) {
            ...
        }
        displayNumber(4); // Replace 4 with a variable to output or what not
    }

    private int displayNumber(int number) {
        return number;
    }
}

In Java you'll add your methods inside the scope of the class like so, when you want to use them call them inside of another function like in the example above. I'd recommend reading this link, it should give you a good understanding of how methods work, how to call them, etc.

Matthew Brzezinski
  • 1,685
  • 4
  • 29
  • 53