hello everyone I am newto programming and I am just a little bit confused. I have three files in java
Product.java
this file defines the product attributes like product name, quant开发者_开发技巧ity, ID
in this file i dont have a main
ProductCollection.java
this file create a vector to hold data temporarily when accessing database
Database.java
database connection
EServer
communicate with client and retrieve access database
rs = query.executeQuery(queryString);
pColl = new ProductCollection();<-------------------confuse
product extractedProduct; <------------------------ confuse
while(rs.next())
{
extractedProduct = (Product).addProduct(rs);
// Object [ ] extractedProduct = ProductCollection.addProduct(rs);
}
EClient.java
can someone explain this two line please
[ pColl = new ProductCollection();] [ product extractedProduct;]
also I am struggling to add an object to store the result temporary. Can someone help me please I want extract out the contents of a row and set up a Product object extractedProduct
pColl = new ProductCollection();
This is as simple assignment. On the left hand side you have a variable, pColl
, and on the right hand side you have an expression that creates a new object of type ProductCollection
and returns a reference to it.
product extractedProduct;
This is a variable declaration which says, "from this line on, I want to be able to use a variable which I call extractedProduct
and it refers to a product
" (or, put another way, you declare a variable of type product
).
The Java convention says that class names should start with a capital letter, so you probably want to change the name of the class so it reads
Product extractedProduct;
Useful links:
- Official trail on Java Variables
ProductCollection is a class. Typically in any OO language the program operates on instances of objects. Think of the class as a template for creating new instances of the class.
so the
pColl = new ProductCollection();
line creates a new ProductCollection instance and gives your a reference to that instance, pColl. On that particular line the type of pColl is not defined, so it must be somewhere else in the code before that line, or there is an error. You could do
ProductCollection pColl = new ProductCollection();
if that is the case. The next line
product extractedProduct;
means that extranctedProduct is a reference to an instance of product. As a note, if product is a class it should be Product with a capital P. Since you don't assign a value, extractedProduct is uninitialized.
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");}
}
}
精彩评论