帮JAVA新人看看!
近来学JAVA,学写了一个中间层对数据库操作,但在中间层对数据库进行比如插入后,如何返回一个值给前面的APPLET,?
前端的APPLET代码如下:ShopperApplet.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
class Shopper implements Serializable
{
String cShopperid,cPassword,vFirstName,vLastName,vEmailId,vAddress,cCity,cState,cPhone,cCreditCardNo,vCreditCardType;
}
public class ShopperApplet extends JApplet
{
JLabel labelShopperId;
JLabel labelPass;
JLabel labelEmail;
JLabel labelFname;
JLabel labelLname;
JLabel labelAdd;
JLabel labelCity;
JLabel labelState;
JLabel labelCountry;
JLabel labelPhone;
JLabel labelCreditNo;
JLabel labelCreditType;
JTextField textShopperId;
JTextField textPass;
JTextField textEmail;
JTextField textFname;
JTextField textLname;
JTextField textAdd;
JTextField textCity;
JTextField textState;
JTextField textPhone;
JTextField textCreditNo;
JButton submit;
JButton cancel;
JComboBox comboCountry;
JComboBox comboCreditType;
public void init()
{
CreateApplet();
}
public void CreateApplet()
{
Container content = new Container();
content = getContentPane();
content.setLayout(new FlowLayout());
labelShopperId = new JLabel(" Shopper ID:");
labelPass = new JLabel("Password:");
labelEmail= new JLabel("Email Adress:");
labelFname= new JLabel("First Name:");
labelLname= new JLabel("Last Name:");
labelAdd= new JLabel("Address:");
labelCity = new JLabel("City :");
labelState= new JLabel("State:");
labelCountry= new JLabel("Country:");
labelPhone= new JLabel("Phone:");
labelCreditNo = new JLabel("Credit Card No.");
labelCreditType = new JLabel("Credit Card Type:");
textShopperId = new JTextField(6);
textPass= new JTextField(10);
textEmail= new JTextField(12);
textFname= new JTextField(20);
textLname= new JTextField(10);
textAdd= new JTextField(15);
textCity= new JTextField(10);
textState = new JTextField(10);
textPhone= new JTextField(10);
textCreditNo = new JTextField(15);
String arr[]={"Italy","USA","Japan","India"};
comboCountry = new JComboBox(arr);
String creditType[] = {"Master","Visa"};
comboCreditType = new JComboBox(creditType);
submit = new JButton("Submit");
cancel = new JButton("Cancel");
content.add(labelShopperId);
content.add(textShopperId);
content.add(labelPass);
content.add(textPass);
content.add(labelEmail);
content.add(textEmail);
content.add(labelFname);
content.add(textFname);
content.add(labelLname);
content.add(textLname);
content.add(labelAdd);
content.add(textAdd);
content.add(labelCity);
content.add(textCity);
content.add(labelState);
content.add(textState);
content.add(labelCountry);
content.add(comboCountry);
content.add(labelPhone);
content.add(textPhone);
content.add(labelCreditNo);
content.add(textCreditNo);
content.add(labelCreditType);
content.add(comboCreditType);
content.add(submit);
content.add(cancel);
validateAction validateButton = new validateAction();
submit.addActionListener(validateButton);
}
//Listener interface implementation for the button
class validateAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
//Extracting source of action
Object obj = evt.getSource();
if(obj == submit)
{
Shopper data=new Shopper();
data.cShopperid = textShopperId.getText();
data.cPassword = textPass.getText();
data.vFirstName = textFname.getText();
data.vLastName = textLname.getText();
data.vEmailId = textEmail.getText();
data.vAddress = textAdd.getText();
data.cCity = textCity.getText();
data.cState = textState.getText();
data.cPhone = textPhone.getText();
data.cCreditCardNo = textCreditNo.getText();
data.vCreditCardType ="002";
try
{
Socket toServer;
toServer = new Socket("127.0.0.1",1001);
ObjectOutputStream streamToServer=new ObjectOutputStream(toServer.getOutputStream());
//Sending the data to the server for processing
streamToServer.writeObject((Shopper)data);
streamToServer.close();
}
catch(InvalidClassException e)
{
showStatus("The Shopper class is invalid" + e);
}
catch(NotSerializableException e)
{
showStatus("The object is not serializable" + e);
}
catch(IOException e)
{
showStatus("Cannot write to the server" + e);
}
}
}
}
}
html代码如下:ShopperApplet.html
<html>
<applet code=ShopperApplet.class
height = 300 width = 300>
</applet>
</html>
中间层代码如下:AppServer.java
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
//The Shopper class needs to implement serializable
class Shopper extends Object implements java.io.Serializable
{
String cShopperid,cPassword,vFirstName,vLastName,vEmailId,vAddress,cCity,cState,cPhone,cCreditCardNo,vCreditCardType;
}
//Code for the AppServer class
class AppServer implements Runnable
{
ServerSocket server;
Socket fromClient;
Thread serverThread;
public AppServer()
{
try
{
server = new ServerSocket(1001);
serverThread = new Thread(this);
serverThread.start();
}
catch(Exception e)
{
System.out.println("Cannot start the thread" + e);
}
}
public static void main(String args[])
{
new AppServer();
}
public void run()
{
try
{
while(true)
{
//Listening to the clients request
fromClient = server.accept();
//Creating the connect object
Connect con = new Connect(fromClient);
}
}
catch(Exception e)
{
System.out.println("Cannot listen to the client" + e);
}
}
}
//Code for the connect class
class Connect
{
Shopper data;
ObjectInputStream streamFromClient;
public Connect(Socket inFromClient)
{
//Retrieving the clients stream
try
{
streamFromClient = new ObjectInputStream(inFromClient.getInputStream());
try
{
//Retrieving the Shopper details from the client
data = (Shopper)streamFromClient.readObject();
}
catch(InvalidClassException e)
{
System.out.println("Cannot serialize the Shopper class" + e);
}
catch(NotSerializableException e)
{
System.out.println("The object is not serializable" + e);
}
catch(IOException e)
{
System.out.println("Cannot read from the client stream" + e);
}
//Call the submit method
submit();
}
catch(Exception e)
{
System.out.println("Cannot get the client stream" + e);
}
}
//Code for the submit method
public void submit()
{
try
{
//Code for submiting the Shopper details to the //database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con;
con = DriverManager.getConnection("jdbc:odbc:Toy","sa",null);
PreparedStatement stat=con.prepareStatement(
"insert into shopper(cShopperid,cPassword,vFirstName,vLastName,vEmailId,vAddress,cCity,cState,cPhone,cCreditCardNo,vCreditCardType) values(?,?,?,?,?,?,?,?,?,?,?)");
stat.setString(1,data.cShopperid);
stat.setString(2,data.cPassword);
stat.setString(3,data.vFirstName);
stat.setString(4,data.vLastName);
stat.setString(5,data.vEmailId);
stat.setString(6,data.vAddress);
stat.setString(7,data.cCity);
stat.setString(8,data.cState);
stat.setString(9,data.cPhone);
stat.setString(10,data.cCreditCardNo);
stat.setString(11,data.vCreditCardType);
stat.executeUpdate();
}
catch(Exception e)
{
System.out.println("Cannot insert the data in the table" + e);
}
}
}
数据库的SQL在下贴中
问题点数:50、回复次数:19Top
1 楼xioyoo(逍遥)回复于 2002-05-06 15:15:20 得分 50
你用的SOCKET连接ServerSocket
既然Server端(也就是你说的中间层)可得到客户端连接过来的Socket,就可以象它回写数据啊,你的代码中不是用了writeObject吗,一样的啊。
或则我没有理解清你的意思
“如何返回一个值给前面的APPLET,?”请把这句话说详细点,具体你想返回什么东西给APPLET?Top
2 楼yyb2000(三流编程机器)回复于 2002-05-07 11:25:54 得分 0
我的意思就是,在插入数据成功后,如何返回一个消息,通知客户端进行下一步工作Top
3 楼xioyoo(逍遥)回复于 2002-05-07 14:41:03 得分 0
你的Server端可以通过客户端连接过来的Socket得到向客户端输出的流:
PrintStream streamToClient=new PrintStream(new BufferedOutputStream(inFromClient.getOutputStream()));
得到这个对象就可以向客户端返回一些消息了
如:streamToClient.println("execute sql successful");
streamToClient.flush();
同时在客户端需要一个输入流来获得这个消息
DataInputStream streamFromServer=new DataInputStream(new BufferedInputStream(toServer.getInputStream()));
以后一旦服务器发确认消息过来将它保存为一个字符串对象
String result=streamFromServer.readLine();
打印出这个消息看是否正确:
System.out.println(result);Top
4 楼yyb2000(三流编程机器)回复于 2002-05-07 15:00:23 得分 0
楼上这位老大,首先谢你了,,我试试,,
还有一问题:
如果成功后,我要再打开一个HTML如何写?
getAppletContext().showDocument(new URL("d:\\mysoft\\javaproj\\client\\Order.html"));
为什么有错?Top
5 楼xioyoo(逍遥)回复于 2002-05-07 15:07:35 得分 0
File file=new File("d:\\mysoft\\javaproj\\client\\Order.html");
URL u=File.toURL();
getAppletContext().showDocument(u);Top
6 楼yyb2000(三流编程机器)回复于 2002-05-07 15:25:24 得分 0
老大,为什么不相等
DataInputStream streamFromServer=new DataInputStream(new BufferedInputStream(toServer.getInputStream()));
String result=streamFromServer.readLine();
if (result=="submitok") //服务器返回为"submitok"
{
File file=new File("d:\\mysoft\\javaproj\\client\\Order.html");
URL u=file.toURL();
getAppletContext().showDocument(u);
}
else
System.out.println("不相等");Top
7 楼xioyoo(逍遥)回复于 2002-05-07 15:38:14 得分 0
System.out.println(result);打印的是什么结果Top
8 楼xioyoo(逍遥)回复于 2002-05-07 15:43:57 得分 0
比较字符串内容不要用“==”!
if (result.equals("submitok"))Top
9 楼yyb2000(三流编程机器)回复于 2002-05-07 15:44:24 得分 0
System.out.println(result)
submitokTop
10 楼xioyoo(逍遥)回复于 2002-05-07 15:47:04 得分 0
比较字符串内容不要用“==”
应该是if (result.equals("submitok"))Top
11 楼yyb2000(三流编程机器)回复于 2002-05-07 15:55:57 得分 0
改过来了,应是对了,不过那个打开网页的操作好象没反应一样??
我是用APPLETVIEWER ,,没用IE,有关系吗?Top
12 楼xioyoo(逍遥)回复于 2002-05-07 16:10:16 得分 0
这个我还真没试过了,你用IE试试吧:)Top
13 楼yyb2000(三流编程机器)回复于 2002-05-07 16:13:48 得分 0
我在IE里,说"CLASS NOT FOUND"Top
14 楼xioyoo(逍遥)回复于 2002-05-07 16:29:16 得分 0
因为你的APPLET用了SWING包罗Top
15 楼yyb2000(三流编程机器)回复于 2002-05-07 16:40:45 得分 0
HTMLConverter我用不来了,,,:(Top
16 楼yyb2000(三流编程机器)回复于 2002-05-07 16:55:15 得分 0
不过你还是帮我看看为什么不显示下一页面?Top
17 楼xioyoo(逍遥)回复于 2002-05-07 17:01:22 得分 0
<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
codeBase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0"
width=300
height=320
align=center>
<param name="code" value="*****.class">
</OBJECT>
手动修改HTML,把上面这段代替以前的<applet ......>
把*****.class换成你自己的Top
18 楼yyb2000(三流编程机器)回复于 2002-05-07 17:13:29 得分 0
我以已下了1.3pulgin了,,HTMLConverter也会用了,,
请领分。。。
谢了。。。。。。记住下次还要帮我Top
19 楼yyb2000(三流编程机器)回复于 2002-05-07 17:14:51 得分 0
终于出来了,,,Top




