当前位置: 技术问答>java相关
那位朋友可以给个applet通过servlet取数据库数据的例子?多谢!
来源: 互联网 发布时间:2015-06-11
本文导语: 多谢了。/ 我找以前的没找到有,只好又发了一贴,请多多帮忙。 | import java.io.*; import java.awt.*; import java.applet.*; public class DataBaseApplet extends Applet implements Runnable { int i; String respo...
多谢了。/
我找以前的没找到有,只好又发了一贴,请多多帮忙。
我找以前的没找到有,只好又发了一贴,请多多帮忙。
|
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;
}
}
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;
}
}
|
and clients site:
private void defineOrgStruc(ArrayList levellist) throws MalformedURLException, IOException{
URL url = new URL(/tech-qa-java/servleturl/index.html);
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!");
}
private void defineOrgStruc(ArrayList levellist) throws MalformedURLException, IOException{
URL url = new URL(/tech-qa-java/servleturl/index.html);
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!");
}