Results 1 to 4 of 4

Thread: [RESOLVED] Convert to using a list instead of the model

Threaded View

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [RESOLVED] Convert to using a list instead of the model

    Hi,

    I need to know how to convert project to use a list of strings to populate the table without using the table model to populate the table with the data. This is my attempt at trying to make the project work without the table model but as you can see it has tons of errors.


    Files in the DAO:

    PencilDAO:
    java Code:
    1. package DAO;
    2.  
    3. import model.Pencil;
    4. import model.*;
    5. import java.sql.*;
    6. import java.util.*;
    7.  
    8. /**
    9.  * Data Access Class for the Pencil database
    10.  */
    11. public class PencilDAO extends PencilsApplicationDAO{
    12.  
    13.   /**
    14.    * The Prepared statement used for selecting Pencils
    15.    */
    16.   private static PreparedStatement selectAllPreparedStatement = null;
    17.  
    18.  
    19.   /**
    20.    * Retrieves all book records  from the tblPencils
    21.    * table in a database as a List of Pencil objects. Will create the
    22.    * selectAllPreparedStatement object only once (the first time the method
    23.    * is called, ie when selectAllPreparedStatement==null)
    24.    * @return A list of Pencil objects which are all of the records from the table
    25.    *         tblPencils.
    26.    *         Will be null if any error occurs (eg table does not exist)
    27.    */
    28.   public static List<Pencil> retrieveAllColourPencils() {
    29.     List<Pencil> results = null;
    30.     String SQLStatementStr =
    31.             "SELECT colour, size "+
    32.             "FROM tblPencils " +
    33.             "ORDER BY colour, size";
    34. //            "SELECT colourKey,length,colourName "+
    35. //            "FROM tblPencil,tblColour " +
    36. //            "WHERE tblColour.colourKey=tblColour.colourKey "+
    37. //            "ORDER BY colourKey";
    38.  
    39.     ResultSet rs;
    40.     try {
    41.       if (selectAllPreparedStatement == null) {
    42.         selectAllPreparedStatement = con.prepareStatement(SQLStatementStr);
    43.       }
    44.       rs = selectAllPreparedStatement.executeQuery();
    45.       results = new ArrayList<Pencil>();
    46.       while (rs.next()) {
    47.         Pencil record;
    48.         record = new Pencil(rs.getString("colour"), rs.getInt("size"));
    49.         results.add(record);
    50.       }
    51.     } catch (SQLException ex) {
    52.       results = null;
    53.     }
    54.     return results;
    55.   }
    56. }

    PencilsApplicationDAO:
    java Code:
    1. package DAO;
    2.  
    3. import model.Pencil;
    4. import java.sql.*;
    5. import java.util.*;
    6. import model.*;
    7.  
    8. /**
    9.  * General database access routines
    10.  */
    11. public class PencilsApplicationDAO {
    12.   /**
    13.    * The Prepared statement used for selecting Pencils for
    14.    * a particular colour
    15.    */
    16.   private static PreparedStatement selectColourPreparedStatement = null;
    17.  
    18.   /**
    19.    * The database for the Pencil Application
    20.    */
    21.   public static final String DB_NAME= "5IJA_DB";
    22.   /**
    23.    * The connection to the database
    24.    */
    25.   protected static Connection con=null;
    26.   /**
    27.    * Connects to the database assuming a suitable driver is available
    28.    * Will throw an exception if an error occurs. Otherwise a connection is
    29.    * established and the con class variable is ready to be used.
    30.    * @throws SQLException
    31.    */
    32.   public static void connectToDatabase() throws SQLException {
    33.  
    34. //    con = DriverManager.getConnection("jdbc:odbc:" + DB_NAME);
    35.  
    36.      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+DB_NAME , "root", "password");
    37.   }
    38.  
    39.   /**
    40.    * Getter for connection object if needed
    41.    * @return the Connection object
    42.    */
    43.   public static Connection getConnection() {
    44.     return con;
    45.   }
    46.  
    47.     /**
    48.    * Retrieves all pencil records of a particular colour from the tblPencils
    49.    * table in a database as a List of Pencil objects. Will create the
    50.    * selectColourPreparedStatement object only once (the first time the method
    51.    * is called, ie when selectColourPreparedStatement==null)
    52.    * @param colour The colour of the pencils to retrieve
    53.    * @return A list of Pencil objects which are all of the records from the table
    54.    *         tblPencils with the colour as per the provided parameter.
    55.    *         Will be null if any error occurs (eg table does not exist)
    56.    */
    57.   public static List<Pencil> retrievePencils(String colour) {
    58.     List<Pencil> results = null;
    59.     String SQLStatementStr =
    60.             "SELECT colour, size "+
    61.             "FROM tblPencils " +
    62.             "WHERE colour = ? "+
    63.             "ORDER BY colour";
    64.  
    65.     ResultSet rs;
    66.     try {
    67.       if (selectColourPreparedStatement  == null) {
    68.         selectColourPreparedStatement = con.prepareStatement(SQLStatementStr);
    69.       }
    70.       selectColourPreparedStatement .setString(1, colour);
    71.       rs = selectColourPreparedStatement.executeQuery();
    72.       results = new ArrayList<Pencil>();
    73.       while (rs.next()) {
    74.         Pencil record;
    75.         record = new Pencil(rs.getString("colour"), rs.getInt("size"));
    76.         results.add(record);
    77.       }
    78.     } catch (SQLException ex) {
    79.       results = null;
    80.     }
    81.     return results;
    82.   }
    83. }


    Files in the Controller:

    PencilApplication:
    java Code:
    1. package controller;
    2.  
    3. import DAO.*;
    4. import view.*;
    5. import model.*;
    6. import java.sql.*;
    7. import java.util.*;
    8.  
    9. /**
    10.  * Displays and controls a simple view of a book table
    11.  */
    12. public class PencilApplication {
    13.  
    14.   private PencilMainViewFrame mainFrame;
    15.   private List<Pencil> data;
    16.   private List<String> colourList;
    17.  
    18.   public PencilApplication() {
    19.     // Create the view letting it know the controller
    20.     mainFrame = new PencilMainViewFrame(this);
    21.     mainFrame.setVisible(true);
    22.  
    23.     // Connect to DB. If this fails the view will need to display a
    24.     // message and program will abort
    25.     connectToDatabase();
    26.  
    27.     // Setup the model for the distinct publishers list box
    28.     List<String> colourList;
    29.     colourList = PencilsApplicationDAO.retrieveDistinctColours();
    30.     if (colourList == null) {
    31.       mainFrame.displayDatabaseUnExpectedError("Unable to Retrieve colour of pencils!");
    32.     } else {
    33.       mainFrame.displaynewListOfColours(colourList);
    34.     }
    35.     // Initial data of books is null until user makes a choice. This 'null' based
    36.     // model will at least allow the JTable column headings to be visible.
    37.     data = null;
    38.     //mainFrame.displaynewListOfColours;
    39.   }
    40.  
    41.   /**
    42.    * Uses the DAO to connect to the database.
    43.    * Will display a popup message dialogue if the connection fails and then
    44.    * exit the application. Otherwise a connection is established and the instance
    45.    * variable is ready to be used.
    46.    */
    47.   private void connectToDatabase() {
    48.     try {
    49.       PencilsApplicationDAO.connectToDatabase();
    50.     } catch (SQLException ex) {
    51.       mainFrame.displayDatabaseConnectionError();
    52.       System.exit(-1);
    53.     }
    54.   }
    55.  
    56.   /**
    57.    * User (through view) has requested display of Books of a particular
    58.    * pubName. These will be retrieved from the table using the DAO methods. Then
    59.    * the view will be asked to update itself using the new BookJTableModel or
    60.    * display an error if something goes wrong.
    61.    * @param pubName The pubName of the books to be displayed
    62.    */
    63.   public void displaycolourpencils(String colour) {
    64.     data = PencilDAO.retrievePencils(colour);
    65.     if (data == null) {
    66.       mainFrame.displayDatabaseUnExpectedError("Unable to Retrieve colour of pencils " + colour);
    67.     } else {
    68.       this.mainFrame.displayNewPencilData(data);
    69.     }
    70.   }
    71.  
    72.   /**
    73.    * Get everything going
    74.    * @param args Not used
    75.    */
    76.   public static void main(String args[]) {
    77.     new PencilApplication();
    78.   }
    79. }
    Thanks,


    Nightwalker
    Attached Images Attached Images  
    Last edited by Nightwalker83; Jun 20th, 2012 at 01:59 AM. Reason: Adding more!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

Posting Permissions

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



Click Here to Expand Forum to Full Width