大家好我是编程新手,我只是有点困惑。我在java中有三个文件
Product.java
此文件定义产品属性,如产品名称,数量,ID
在这个文件中,我没有主要的
ProductCollection.java
此文件创建一个向量来在访问数据库时临时保存数据
Database.java
数据库连接
EServer
与客户沟通并检索访问数据库
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
请有人解释这两行
[ pColl = new ProductCollection();] [ product extractedProduct;]
我也在努力添加一个对象来临时存储结果。有人可以帮助我,我想要提取行的内容并设置一个Product对象extractedProduct
答案 0 :(得分:3)
pColl = new ProductCollection();
这是一个简单的任务。在左侧,您有一个变量pColl
,在右侧有一个表达式,用于创建ProductCollection
类型的新对象并返回对它的引用。
product extractedProduct;
这是变量声明,它说:“从这一行开始,我希望能够使用我称之为extractedProduct
的变量,它引用product
“或者换句话说,你声明一个product
类型的变量。)
Java约定说类名应该以大写字母开头,所以你可能想要更改类的名称,使其读取
Product extractedProduct;
有用的链接:
答案 1 :(得分:1)
ProductCollection是一个类。通常,在任何OO语言中,程序都在对象的实例上运行。可以将该类视为创建类的新实例的模板。
所以
pColl = new ProductCollection();
行创建一个新的ProductCollection实例,并为您提供对该实例pColl的引用。在该特定行上,未定义pColl的类型,因此它必须在该行之前的代码中的其他位置,或者存在错误。你可以做到
ProductCollection pColl = new ProductCollection();
如果是这样的话。下一行
product extractedProduct;
表示extranctedProduct是对产品实例的引用。注意,如果产品是一个类,它应该是具有大写字母P的Product。因为你没有赋值,所以extractProduct是未初始化的。
答案 2 :(得分:0)
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");}
}
}