Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>import java.sql.*; import java.net.*; import java.io.*; import java.util.Enumeration; public class EServer { public static void main(String[] args) { // Net and IO objects ServerSocket ss= null; Socket s = null; BufferedReader bf = null; PrintWriter pw = null; // Queries // Query which retireves all products String allQuery = "Select * from StoreProducts"; // Query which retrieves products which are out of stock String outOfStockQuery = "Select * from StoreProducts where Quantity = 0"; String queryString = ""; String messageBack = ""; // Database objects Connection cn = null; Statement query = null; ResultSet rs = null; String lineRead= ""; ProductCollection pColl = new ProductCollection(); Enumeration pEnumeration; try { // Server socket set up on port 2000 ss = new ServerSocket(2000); System.out.println("...Server socket set up"); // Set up a database Database db = new Database ("darrel", "", "sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:", "products"); System.out.println("...Database set up"); // Get a connection to the database Database.establishConnection(); cn = Database.getConnection(); System.out.println("...Database connection set up"); //Set up a query object query = cn.createStatement(); // Loop waiting for connections int count = 1; while(true) { System.out.println("...Waiting for connection "+count); count++; s = ss.accept(); pw = new PrintWriter(s.getOutputStream(), true); bf = new BufferedReader(new InputStreamReader(s.getInputStream())); boolean looper = true; while(true) { lineRead = bf.readLine(); switch(lineRead.charAt(0))// { // Out of Stock command case 'O':{ queryString = outOfStockQuery; break; } // All products case 'A':{ queryString = allQuery; break; } // Client has terminated case 'E':{ looper = false; break; } } //Check if client has terminated if(!looper) break; // Client has not terminated // Execute the required query and create result set rs = query.executeQuery(queryString); // Create collection of products pColl = new ProductCollection(); // Process the rows that have been extracted // Place them in pColl Product extractedProduct; while(rs.next()) { *extractedProduct = (Product) pColl.addProduct(rs);* } // Form the collection of products, each terminated by asterisk pEnumeration = pColl.elements(); // messageBack is to be concatenated to so initialise it to the empty string messageBack= ""; while(pEnumeration.hasMoreElements()) { messageBack+=(Product)pEnumeration.nextElement()+"*"; } // Now send back the collection string to the client for display pw.println(messageBack); } } } catch(Exception e) {System.out.println("Trouble setting up the database "+e);} // Close database connection try { cn.close(); } catch(Exception e) {System.out.println("Problem closing connection");} } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload