Results 1 to 4 of 4
  1. #1
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default How can I call my database read method to display its ArrayList?

    Hi there,

    I have created a class/method which performs a database read and fills an ArrayList.

    Java Code:
    public class EntityProvider {
    
    
        public void readEntities() throws SQLException, IOException {
    
            ArrayList <String> fileArray = new ArrayList<String>();
            
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            int count = 0;
    
            String serverName = "francisca";
            int port = 1521;
            String user = "niku";
            String password = "niku";
            String SID = "nikuuat";
            String URL = "jdbc:oracle:thin:@" + serverName + ":" + port + ":" + SID;
    
            Connection conn = DriverManager.getConnection(URL, user, password);
            String SQL = "SELECT DISTINCT level2_name " +
                    "FROM niku.nbi_dim_obs " +
                    "WHERE level1_name = 'DSTi Global' " +
                    "AND level2_name is not null";
    
            String newLine = System.getProperty("line.separator");
    
    
            Statement stat = conn.createStatement();
            ResultSet rs = stat.executeQuery(SQL);
            
    
            while (rs.next()) { //cycle through result set
                
                fileArray.add(rs.getString(1));
    
            }
    
            for(String name : fileArray){
                System.out.println(name);
            }
            stat.close();
            conn.close();
      }
    }

    In a second class I am trying to call this method to display its list, but I am getting an error:
    Void Type not allowed here

    public class ShowEntities {

    Java Code:
        public static void main(String args[]){
    
            EntityProvider entityCombo = new EntityProvider();
            
            System.out.println(entityCombo.readEntities()); 
            
            
    
    
        }
    
    }

    please can somebody advise?
    i'm a little lost with Java at the moment.
    thanks in advance,
    Matt

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    perhaps you want to make the readEntities return an ArrayList and not return void as currently written. Then you could just return the fileArray from the method when it's through.

    Also, this class appears to be something like a utility class that only holds helper methods. I think (correct me if I'm wrong anybody), that this may be an OK place to use static methods.

    Best of luck!

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Fubarable is right.

    Your exception is caused by the fact that readEntities() returns void; println() can't do anything with a void return.

    You are already printing the name in the first class. Just get rid of the println() in the second.

    As Fubarable pointed out, your first class isn't of much value unless it returns something...

  4. #4
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default

    thanks guys.
    I removed the println and also had to throw the SQLException in the main method (as well as importing the SQL libraries)
    Is it right that I need to import java.sql.* in the class that calls another class containing SQL code?

Similar Threads

  1. Replies: 3
    Last Post: 02-17-2009, 12:11 PM
  2. How do i call display.setcurrent
    By arnab321 in forum CLDC and MIDP
    Replies: 7
    Last Post: 11-04-2008, 10:00 PM
  3. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 09:37 AM
  4. Read-File Write Display substring
    By hiklior in forum New To Java
    Replies: 3
    Last Post: 04-18-2008, 12:45 PM
  5. Simplest way to read and display a jpeg image
    By Hasan in forum New To Java
    Replies: 1
    Last Post: 05-31-2007, 04:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •