当前位置: 技术问答>java相关
帮JAVA新人看看!
来源: 互联网 发布时间:2015-05-24
本文导语: 近来学JAVA,学写了一个中间层对数据库操作,但在中间层对数据库进行比如插入后,如何返回一个值给前面的APPLET,? 前端的APPLET代码如下:ShopperApplet.java import javax.swing.*; import java.awt.*; import java.awt.event.*; import ...
近来学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
中间层代码如下: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在下贴中
前端的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
中间层代码如下: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在下贴中
|
你用的SOCKET连接ServerSocket
既然Server端(也就是你说的中间层)可得到客户端连接过来的Socket,就可以象它回写数据啊,你的代码中不是用了writeObject吗,一样的啊。
或则我没有理解清你的意思
“如何返回一个值给前面的APPLET,?”请把这句话说详细点,具体你想返回什么东西给APPLET?
既然Server端(也就是你说的中间层)可得到客户端连接过来的Socket,就可以象它回写数据啊,你的代码中不是用了writeObject吗,一样的啊。
或则我没有理解清你的意思
“如何返回一个值给前面的APPLET,?”请把这句话说详细点,具体你想返回什么东西给APPLET?