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:
package DAO; import model.Pencil; import model.*; import java.sql.*; import java.util.*; /** * Data Access Class for the Pencil database */ public class PencilDAO extends PencilsApplicationDAO{ /** * The Prepared statement used for selecting Pencils */ private static PreparedStatement selectAllPreparedStatement = null; /** * Retrieves all book records from the tblPencils * table in a database as a List of Pencil objects. Will create the * selectAllPreparedStatement object only once (the first time the method * is called, ie when selectAllPreparedStatement==null) * @return A list of Pencil objects which are all of the records from the table * tblPencils. * Will be null if any error occurs (eg table does not exist) */ public static List<Pencil> retrieveAllColourPencils() { List<Pencil> results = null; String SQLStatementStr = "SELECT colour, size "+ "FROM tblPencils " + "ORDER BY colour, size"; // "SELECT colourKey,length,colourName "+ // "FROM tblPencil,tblColour " + // "WHERE tblColour.colourKey=tblColour.colourKey "+ // "ORDER BY colourKey"; ResultSet rs; try { if (selectAllPreparedStatement == null) { selectAllPreparedStatement = con.prepareStatement(SQLStatementStr); } rs = selectAllPreparedStatement.executeQuery(); results = new ArrayList<Pencil>(); while (rs.next()) { Pencil record; record = new Pencil(rs.getString("colour"), rs.getInt("size")); results.add(record); } } catch (SQLException ex) { results = null; } return results; } }
PencilsApplicationDAO:
java Code:
package DAO; import model.Pencil; import java.sql.*; import java.util.*; import model.*; /** * General database access routines */ public class PencilsApplicationDAO { /** * The Prepared statement used for selecting Pencils for * a particular colour */ private static PreparedStatement selectColourPreparedStatement = null; /** * The database for the Pencil Application */ public static final String DB_NAME= "5IJA_DB"; /** * The connection to the database */ protected static Connection con=null; /** * Connects to the database assuming a suitable driver is available * Will throw an exception if an error occurs. Otherwise a connection is * established and the con class variable is ready to be used. * @throws SQLException */ public static void connectToDatabase() throws SQLException { // con = DriverManager.getConnection("jdbc:odbc:" + DB_NAME); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+DB_NAME , "root", "password"); } /** * Getter for connection object if needed * @return the Connection object */ public static Connection getConnection() { return con; } /** * Retrieves all pencil records of a particular colour from the tblPencils * table in a database as a List of Pencil objects. Will create the * selectColourPreparedStatement object only once (the first time the method * is called, ie when selectColourPreparedStatement==null) * @param colour The colour of the pencils to retrieve * @return A list of Pencil objects which are all of the records from the table * tblPencils with the colour as per the provided parameter. * Will be null if any error occurs (eg table does not exist) */ public static List<Pencil> retrievePencils(String colour) { List<Pencil> results = null; String SQLStatementStr = "SELECT colour, size "+ "FROM tblPencils " + "WHERE colour = ? "+ "ORDER BY colour"; ResultSet rs; try { if (selectColourPreparedStatement == null) { selectColourPreparedStatement = con.prepareStatement(SQLStatementStr); } selectColourPreparedStatement .setString(1, colour); rs = selectColourPreparedStatement.executeQuery(); results = new ArrayList<Pencil>(); while (rs.next()) { Pencil record; record = new Pencil(rs.getString("colour"), rs.getInt("size")); results.add(record); } } catch (SQLException ex) { results = null; } return results; } }
Files in the Controller:
PencilApplication:
Thanks,java Code:
package controller; import DAO.*; import view.*; import model.*; import java.sql.*; import java.util.*; /** * Displays and controls a simple view of a book table */ public class PencilApplication { private PencilMainViewFrame mainFrame; private List<Pencil> data; private List<String> colourList; public PencilApplication() { // Create the view letting it know the controller mainFrame = new PencilMainViewFrame(this); mainFrame.setVisible(true); // Connect to DB. If this fails the view will need to display a // message and program will abort connectToDatabase(); // Setup the model for the distinct publishers list box List<String> colourList; colourList = PencilsApplicationDAO.retrieveDistinctColours(); if (colourList == null) { mainFrame.displayDatabaseUnExpectedError("Unable to Retrieve colour of pencils!"); } else { mainFrame.displaynewListOfColours(colourList); } // Initial data of books is null until user makes a choice. This 'null' based // model will at least allow the JTable column headings to be visible. data = null; //mainFrame.displaynewListOfColours; } /** * Uses the DAO to connect to the database. * Will display a popup message dialogue if the connection fails and then * exit the application. Otherwise a connection is established and the instance * variable is ready to be used. */ private void connectToDatabase() { try { PencilsApplicationDAO.connectToDatabase(); } catch (SQLException ex) { mainFrame.displayDatabaseConnectionError(); System.exit(-1); } } /** * User (through view) has requested display of Books of a particular * pubName. These will be retrieved from the table using the DAO methods. Then * the view will be asked to update itself using the new BookJTableModel or * display an error if something goes wrong. * @param pubName The pubName of the books to be displayed */ public void displaycolourpencils(String colour) { data = PencilDAO.retrievePencils(colour); if (data == null) { mainFrame.displayDatabaseUnExpectedError("Unable to Retrieve colour of pencils " + colour); } else { this.mainFrame.displayNewPencilData(data); } } /** * Get everything going * @param args Not used */ public static void main(String args[]) { new PencilApplication(); } }
Nightwalker




Reply With Quote