那位朋友可以给个applet通过servlet取数据库数据的例子?多谢!

Floating 2002-05-29 05:24:45
多谢了。/
我找以前的没找到有,只好又发了一贴,请多多帮忙。
...全文
6 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Floating 2002-05-30
我指多个参数。 :P
我测试没有成功我就是用上面的方法多发几次,可是不能那样做?为何?
回复
Floating 2002-05-30
再次请教:

如果要传参数来取数据呢?

怎么般?
回复
zmrljl 2002-05-29
写的非常好!
回复
soldier1002 2002-05-29
and clients site:

private void defineOrgStruc(ArrayList levellist) throws MalformedURLException, IOException{
URL url = new URL(servleturl);
URLConnection con = url.openConnection();
con.setUseCaches(false);
System.out.println(levellist.toString());
try{
ObjectOutputStream outputToServlet = null;
con.setDoOutput(true);
outputToServlet = new ObjectOutputStream(con.getOutputStream());
outputToServlet.writeObject(levellist);
outputToServlet.flush();
outputToServlet.close();
}catch (IOException se){
JOptionPane.showMessageDialog(null, "Can not establish connection to server", "Connection failed", JOptionPane.PLAIN_MESSAGE);
return;
}

try{
InputStream in = con.getInputStream();
ObjectInputStream objStream;
objStream = new ObjectInputStream(in);
objStream.readObject();
}catch(Exception re){
JOptionPane.showMessageDialog(null, "Can not establish connection to server", "Connection failed", JOptionPane.PLAIN_MESSAGE);
return;
}
JOptionPane.showMessageDialog(null, "Structure is saved!");
}
回复
soldier1002 2002-05-29
basic idea is, you are passing objects between applets and servlet.
At the servlet, you are using :

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{

try{
ObjectInputStream inputFromApplet = null;
inputFromApplet = new ObjectInputStream(request.getInputStream());
String clientmsg = (String)inputFromApplet.readObject();
System.out.println("reading client request");

if(clientmsg.equals("GetOrgStruc")){
ObjectOutputStream servout = new ObjectOutputStream(response.getOutputStream());
System.out.println(levelnamelist);
servout.writeObject(levelnamelist);
servout.flush();
servout.close();
}
}catch(Exception se){
System.out.println(se.getMessage());
}
}
回复
gularess 2002-05-29
楼上写得够清楚了!
回复
iloveyouonlyonce 2002-05-29
import java.io.*;
import java.awt.*;
import java.applet.*;

public class DataBaseApplet extends Applet implements Runnable
{
int i;
String response;
Thread theThread;

TextArea messInfo=new TextArea("",16,45);
TextField txtDbIP=new TextField(30)
,txtPort=new TextField(4)
,txtUID=new TextField(10)
,txtPass=new TextField(10)
,txtSql=new TextField(30);
Button btnConnect=new Button("Connect");

public void init()
{

this.setSize(500,300);

setLayout(new BorderLayout());
Panel mainPanel=new Panel();
Panel panel1=new Panel();
Panel panel2=new Panel();
Panel panel3=new Panel();

messInfo.setEditable(false);
add("North",messInfo);
//add("North",panel1);

panel2.add(new Label("DB IP:"));
panel2.add(txtDbIP);
panel2.add(new Label("Port:4444"));
panel2.add(txtPort);
add("Center",panel2);

panel3.add(new Label("username:"));
panel3.add(txtUID);
panel3.add(new Label("password:"));
panel3.add(txtPass);
panel3.add(btnConnect);
add("South",panel3);

theThread=new Thread(this);
}

public void start()
{
theThread.start();
}

public void run()
{

while(true)
{
i++;
this.req();

// Graphics g=getGraphics();
//g.drawString("read from server:"+this.response,10,20*i);
//g.dispose();
this.messInfo.append("read from server:"+this.response);
try
{
theThread.sleep(2000);
}
catch(InterruptedException e){}
}

}

public void req()
{
try
{
java.net.URL url=
new java.net.URL(getDocumentBase(),"/servlet/DataBaseServlet");
System.out.println("getDocumentBase(): "+getDocumentBase());
java.net.URLConnection con = url.openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);

ByteArrayOutputStream byteout=new ByteArrayOutputStream();
DataOutputStream out=new DataOutputStream(byteout);
out.writeUTF("Hello world!?");
out.flush();

byte buf[]=byteout.toByteArray();
con.setRequestProperty("Content-type","application/octet-stream");
con.setRequestProperty("Content-length",""+buf.length);

DataOutputStream dataout=new DataOutputStream(con.getOutputStream());
dataout.write(buf);
dataout.flush();
dataout.close();

DataInputStream in=new DataInputStream(con.getInputStream());
this.response=in.readUTF();
//repaint();
//System.out.println("read from server:"+response);

in.close();
}
//catch(java.net.MalformedURLException e){}
catch(Exception e){
e.printStackTrace();
}
}

public void paint(Graphics g)
{
//g.drawString("read from server:"+this.response,10,20);
}
}
-----------------------------------------
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;


import javax.naming.*;import javax.sql.*;
public class DataBaseServlet extends HttpServlet{
String respData;
public void service(HttpServletRequest req,HttpServletResponse resp)
{
try
{
DataInputStream in=new DataInputStream(req.getInputStream());
resp.setContentType("application/octet-stream");
ByteArrayOutputStream byteout=new ByteArrayOutputStream();
DataOutputStream out=new DataOutputStream(byteout);
this.getDbData();
String response=in.readUTF()+" "+this.respData+" "+this.nowTime()+"\n";
System.out.println(response);
out.writeUTF(response);
out.flush();
byte buf[]=byteout.toByteArray();
resp.setContentLength(buf.length);
ServletOutputStream servletout=resp.getOutputStream();
servletout.write(buf);
servletout.close();
}
catch(java.io.IOException e){
e.printStackTrace();
}
}


public void getDbData()
{

try
{

String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=MYDB";
String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";

Class.forName(driver).newInstance();

String login = "admin";
String password = "password";
Connection con=DriverManager.getConnection(url,login,password);

Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM customer");

rs.next();
this.respData=String.valueOf(rs.getInt("cust_id"))+"tttt";

st.close();
con.close();

}
catch(java.sql.SQLException e){System.out.println(e.toString());}
catch(java.lang.ClassNotFoundException e){System.out.println(e.toString());}
catch(java.lang.InstantiationException e){System.out.println(e.toString());}
catch(java.lang.IllegalAccessException e){System.out.println(e.toString());}
}



public String nowTime()
{
java.util.Calendar currTimeCal=java.util.Calendar.getInstance();
int currDate=currTimeCal.get(Calendar.DATE);
int currYear=currTimeCal.get(Calendar.YEAR);
int currMonth=currTimeCal.get(Calendar.MONTH);
int currHour=currTimeCal.get(Calendar.HOUR_OF_DAY);
int currMinute=currTimeCal.get(Calendar.MINUTE);
int currSecond=currTimeCal.get(Calendar.SECOND);

String currTime=String.valueOf(currYear)
+"-"+String.valueOf(currMonth)
+"-"+String.valueOf(currDate)
+":"+String.valueOf(currHour)
+":"+String.valueOf(currMinute)
+":"+String.valueOf(currSecond);


return currTime;
}
}
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告