Results 1 to 3 of 3
  1. #1
    mishy is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Help with database driven cd collection app

    Hello everyone. I am new to java and trying to get some experience by doing a simple cd collection app that runs from a database and I need some help on how to go about it.

    I am using mysql database and I have downloaded the library also I have managed to get the database connection and query working. I need to know if I need to create separate classes to add/remove/edit items and view items? Do I need to put my database connection script in every class that I create or should I create methods for both the connection and the queries that will be called in the additional classes or methods that I have?

    Below is what I have written so far and it is working, I will change the database and query soon to reflect the task I need to do because I have followed a tutorial.

    Java Code:
    /**
     * cdCollection.java
     */
    
    package org.com.mm00422_prototype;
    
    //Import for the SQL package
    import java.sql.*;
    
    //Registering the JDBC driver
    //Class.forName("com.mysql.jdbc.Driver");
    
    /**
     * @author mm00422
     */
    public class FirstExample {
      
      //JDBC driver name and database URL
      static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
      static final String DB_URL = "jdbc:mysql://localhost:3306/emp";
      
      //List of Database credentials
      static final String USER = "root";
      static final String PASSWORD = "play1back";
      
      public static void main(String[] args){
        Connection conn = null;
        Statement stmt = null;
        
        try{
          //Process to register the JDBC driver
          Class.forName("com.mysql.jdbc.Driver");
          
          //Opening the connection 
          System.out.println("Connecting to database............");
          conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
          
          //Execute the query
          System.out.println("Creating statement...........");
          stmt = conn.createStatement();
          String sql;
          sql = "SELECT id, first, last, age FROM employees";
          ResultSet rs = stmt.executeQuery(sql);
          
          //Extract data from the result set
          while(rs.next()){
            //retrieve the data by column name
            int id = rs.getInt("id");
            int age = rs.getInt("age");
            String first = rs.getString("first");
            String last = rs.getString("last");
            
          //display the returned values
            System.out.println("ID: " + id );
            System.out.println(", Age: " + age );
            System.out.println(", First: " + first);
            System.out.println(", Last: " + last);
          }
          
          //Clean up the environment
          rs.close();
          stmt.close();
          conn.close();
        }
        
        catch(SQLException se){
          //Handle Errors for JDBC
          se.printStackTrace();
        }
        
        catch(Exception e){
          //Handle errors for Class.forName
          e.printStackTrace();
        }
        
        finally{
          //finally bloc used to close resources
        
        
        try{
          if(stmt != null)
            stmt.close();
        }
        
        catch(SQLException se2){
          //nothing we can do
        }
        try{
          if(conn !=null)
            conn.close();
        }
        
        catch(SQLException se){
          se.printStackTrace();
        }// end finally try
      } //end try
    System.out.println("Goodbye!");
    
    }  
    }

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Help with database driven cd collection app

    My suggestion is you can try to learn something call as the Data Access Object (DAO) Pattern. This will teach you how you can abstract and implements the data management in you program. You can start implementing a simple CRUD (Create, Read, Update and Delete) using the DAO.

  3. #3
    mishy is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Re: Help with database driven cd collection app

    Quote Originally Posted by wsaryada View Post
    My suggestion is you can try to learn something call as the Data Access Object (DAO) Pattern. This will teach you how you can abstract and implements the data management in you program. You can start implementing a simple CRUD (Create, Read, Update and Delete) using the DAO.
    Thanks for the advice, I have had a look at a few tutorials for CRUD and I will do the same for DAO soon but thanks again for pointing me in the right direction.

Similar Threads

  1. MDD - Mock Driven Development
    By Temujin_12 in forum Advanced Java
    Replies: 1
    Last Post: 10-08-2012, 11:12 AM
  2. Menu-driven program
    By Link in forum New To Java
    Replies: 2
    Last Post: 01-24-2012, 10:44 AM
  3. Write-driven lexer?
    By kjkrum in forum Advanced Java
    Replies: 6
    Last Post: 08-01-2011, 12:32 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
  •