Page 4 of 6 FirstFirst ... 23456 LastLast
Results 31 to 40 of 56

Thread: Java Application for a website

  1. #31
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: Java Application for a website

    I think you may misunderstand the concept of a data access object (DAO).

    https://en.wikipedia.org/wiki/Data_access_object
    http://www.oracle.com/technetwork/ja...ct-138824.html

    A recipe DAO will find recipes, create recipes, delete recipes and so on. The class you have creates connections so is more of a connection factory than a data access object.

    I don't think you have the pieces set out in your mind and how they will fit together. You need to work on that.

    Your compilation errors are because:

    a. You are mixing a class definition with a method definition. You should have something more like:

    Code:
    public class ConnectionFactory {
    
    	public Connection getConnection() throws SQLException {
                ...
    b. You are referring to members of the class that don't exist: userName, password and dbms

    But ... rather than just putting your head down and coding, you need to have a clear picture of how your main components fit together. That was the original question: how to split your application up into manageable components ... but I don't think you have the answer in your head.

    Looking back at the thread, the main components appear to be:

    1. Your main program
    2. Something that manages connections (ConnectionFactory?)
    3. Something that manages recipes (RecipeDAO?)

    As an exercise, you could consider writing Hello World using two collaborating classes:

    Hello.java
    Code:
    public class Hello {
    
        ...
    
    }
    MessageProvider.java
    Code:
    public class MessageProvider {
    
        public String getMessage() {
            return "Hello World";
        }
    
    }
    If you can do that and understand it, it's a step forward to reorganising your real application.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  2. #32
    Join Date
    Aug 2012
    Location
    Quincy, Illinois
    Beans
    Hidden!
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Java Application for a website

    Yes, I realize that I probably need to think my application through a little more clearly, but the thing is that I program to relieve stress and mellow out. After a long hard day of work, and little before work, I need to relax and this is one way of coping. Especially since I feel like and my parents agree that my employer is seriously screwing me out of every possible promotion that is opening up. Three times now I've been passed over. Enough of the ranting though, How might I call the getConnection() method from the ConnectionFactory file in the power file? I'm I correct in assuming that it might look somewhat like this:
    Code:
    connectionfactory.getConnection();
    I'm currently calling the dao and connectionfactory files with the following code:
    Code:
        // call to the database ConnectionFactory
        private ConnectionFactory connectionfactory;
        // call to the database access objects
        private RecipeDAO recipeDAO;
        private CommentsDAO commentsDAO;
        private UsersDAO usersDAO;
    Working is good for your soul and your wallet! But video games are just fun to play!

  3. #33
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: Java Application for a website

    When you program for pleasure, you have the freedom to create a piece of programming art that you can admire and be proud of.

    This is looking good (it's so good, you don't even need the comments):

    Code:
    connectionfactory.getConnection();
    Code:
        // call to the database ConnectionFactory
        private ConnectionFactory connectionfactory;
        // call to the database access objects
        private RecipeDAO recipeDAO;
        private CommentsDAO commentsDAO;
        private UsersDAO usersDAO;
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  4. #34
    Join Date
    Aug 2012
    Location
    Quincy, Illinois
    Beans
    Hidden!
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Java Application for a website

    Need some help with my viewallrecipes method, I'm trying to create a way to display a list of all the recipes within the database so that I can ensure my connecionfactory is truly working it's wonderful magic and I'm having a little trouble calling the method in the power file as well as the printsqlexception at the end of the viewallrecipes method in the recipesdao file, However that can wait a little bit right now.

    the following is the code used to call the viewallrecipes method in the power file
    Code:
        private void makeCentralRegion() throws SQLException
        {
            Connection con = null;
            recipeDAO.viewAllRecipes(con, "sociable_recipe");
        }
    and here is the viewallrecipes method code
    Code:
        public void viewAllRecipes(Connection con, String dbname) throws SQLException {
            Statement stmt = null;
            String query = "select recipeid, title, poster, shortdesc from recipes";
            try {
                stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(query);
                while(rs.next()) {
                    int recipeid = rs.getInt("recipeid");
                    String title = rs.getString("title");
                    String poster = rs.getString("poster");
                    String shortdesc = rs.getString("shortdesc");
                    System.out.println(recipeid + "/t" + title + "/t" + "posted by: Chef" + poster + "/t" + shortdesc);
                }
            } catch(SQLException e){
                power.printSQLException(e);
            } finally {
                if(stmt != null) { stmt.close(); }
            }
        }
    My main issue is that I can't seem to understand how and what arguments to place in the parentheses.
    Working is good for your soul and your wallet! But video games are just fun to play!

  5. #35
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: Java Application for a website

    I wasn't sure which parentheses you meant.

    At this stage, your DAO should look more like:

    Code:
    public class RecipeDAO {
    
        private Connection connection;
    
        public RecipeDAO(Connection connection) {
            this.connection = connection;
        }
    
        public List<Recipe> findAllRecipes() throws SQLException {
            Statement s = null;
            List<Recipe> recipes = new ArrayList<Recipe>();
            try {
                s = this.connection.createStatement();
                ...
                // Run your query, iterate over the results and add recipes to the list
                ...
                return recipes;
            }
            finally {
                // Close statement and resultset (but not the connection)
                ...
            }
        }
    
    }
    You don't want to catch the exception because you aren't doing anything useful with it. For now, let's just declare that you throw it and let your main program deal with it.

    In your main program, you'll get your connection from your connection factory and create the DAO:

    Code:
    private void connectToDatabase() throws SQLException {
        ConnectionFactory factory = new ConnectionFactory();
        this.connection = connectionFactory.getConnection();
        this.recipeDAO = new RecipeDAO(connection);
        ...
    }
    When you want a list of recipes, you call the DAO:

    Code:
    List<Recipe> recipes = recipeDAO.findAllRecipes();
    for (Recipe recipe : recipes) {
        // Do something with each recipe, e.g. add to the model for a Swing table
    }
    I'm assuming you understand the basics of using generics (the things in the <> brackets). If not, please say so.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  6. #36
    Join Date
    Aug 2012
    Location
    Quincy, Illinois
    Beans
    Hidden!
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Java Application for a website

    I'm assuming you understand the basics of using generics (the things in the <> brackets). If not, please say so.
    I never really learned about these generics your talking about in the online courses I took, so I must answer no to your assumption. Could you please give a good example of how to code a query and iterate it over and over until all the recipes in the database are displayed. I have column named recipeid that auto-increments with every new recipe. As well, I was wondering if you could give a good example of how too close a statement and a resultset? In the recipe dao file everything is looking good but eclipse is showing me that lines 12 and 14 have errors in them and the errors are the <Recipe> generics? It could be that I just haven't added my iterated query and resultset yet but I don't know.
    Last edited by TheodoreP; November 30th, 2013 at 05:58 AM.
    Working is good for your soul and your wallet! But video games are just fun to play!

  7. #37
    Join Date
    Mar 2009
    Beans
    1,982

    Re: Java Application for a website

    Never mind, replied to something on the first page which no longer is pertinent.

  8. #38
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Java Application for a website

    Quote Originally Posted by MasterProgram View Post
    Could you please give a good example of how to code a query and iterate it over and over until all the recipes in the database are displayed.
    This is normally something you avoid. Whats is costly in DB interaction is the request. Unless you are handling tens of megabytes of data or making very complex queries, you get much better performance by getting all your data in one query.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  9. #39
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: Java Application for a website

    Quote Originally Posted by MasterProgram View Post
    I never really learned about these generics your talking about in the online courses I took, so I must answer no to your assumption.
    Generics do many things, but of relevance here is that the finder methods, e.g. findAllRecipes return a list of recipes, which is defined as a return type of List<Recipe>. The generic part defines the type of object in the list. This makes it easy to iterate over:

    Main Program
    Code:
    List<Recipe> recipes = recipeDAO.findAllRecipes();
    for (Recipe recipe : recipes) {
       // Do something with each recipe
    }
    Could you please give a good example of how to code a query and iterate it over and over until all the recipes in the database are displayed.
    If I did that, I'd have written the whole method. So I'll let you do that part.

    I have column named recipeid that auto-increments with every new recipe.
    You can define your primary key column as auto-increment. When you insert a recipe, don't set the primary key and the database will handle it.

    https://dev.mysql.com/doc/refman/5.5...increment.html

    Quote Originally Posted by OfNuts
    This is normally something you avoid. Whats is costly in DB interaction is the request. Unless you are handling tens of megabytes of data or making very complex queries, you get much better performance by getting all your data in one query.
    That's the way I read it. Then I read it a few more times and started to hope that it meant, "how to code a query and iterate over it".
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  10. #40
    Join Date
    Aug 2012
    Location
    Quincy, Illinois
    Beans
    Hidden!
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Java Application for a website

    So, just to get a little hint of how to code the query and iterate it over and over until all recipes in the database are displayed I'll post some code and you can tell me if I'm close.
    Code:
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.*;
    
    public class RecipeDAO {
        private Connection connection;
        public RecipeDAO(Connection connection) {
            this.connection = connection;
        }
        public List<Recipe> findAllRecipes() throws SQLException {
            Statement s = null;
            List<Recipe> recipes = new ArrayList<Recipe>();
            s = this.connection.createStatement();
            // Run your query, iterate over the results and add recipes to the list
            String query = "select recipeid, title, poster, shortdesc from recipes";
            try {
                ResultSet rs = s.executeQuery(query);
                while(rs.next()) {
                    int recipeid = rs.getInt("recipeid");
                    String title = rs.getString("title");
                    String poster = rs.getString("poster");
                    String shortdesc = rs.getString("shortdesc");
                    System.out.println(recipeid + "/t" + title + "/t" + "posted by: Chef" + poster + "/t" + shortdesc);
                }
                return recipes;        
            } finally {
                // Close statement and resultset (but not the connection)
                        if(s != null) { s.close(); }
            }
        }
    }
    So tell me, if I'm understanding how to query a database and iterate it over? If so, why is it telling me that the bolded generics are wrong or errors? As well, please let me know if I closed out the statement and resultset but, not the connection correctly?

    As well in my power file in these lines:
    Code:
        private void connectToDatabase() throws SQLException {
            ConnectionFactory factory = new ConnectionFactory();
            this.connection = ConnectionFactory.getConnection();
            this.recipeDAO = new RecipeDAO(connection);
        }
    The bolded lines are recommending that I go to the getConnection() method and add a static type before the Connection type. Well, I tried it but I undid it because it then gives a whole bunch of errors in the getConnection() method and that means that it's wrong because keeping it how I have it now gives zero errors. Please help me with this if you could?
    Last edited by TheodoreP; November 30th, 2013 at 11:55 PM. Reason: changed the code to the full recipe dao file
    Working is good for your soul and your wallet! But video games are just fun to play!

Page 4 of 6 FirstFirst ... 23456 LastLast

Bookmarks

Posting Permissions

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