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

Floating 2002-05-29 05:19:14
多谢了。/
我找以前的没找到有,只好又发了一贴,请多多帮忙。
...全文
23 点赞 收藏 3
写回复
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
iloveyouonlyonce 2002-05-29
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;
}
}
回复
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);
}
}
回复
虎叔 2002-05-29
看看URL即可!!!
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

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