当前位置: 技术问答>java相关
谁有applet存数据到数据库的例子啊,给我发一个吧
来源: 互联网 发布时间:2015-08-18
本文导语: freejob@163.com 急着用,大家救救急啊 用什么方法都无所谓,最好是源码,拿来就用的 不要再applet写数据库的连接信息,谢谢 | 关键问题是: 你不能用Applet与数据库直接交流,你必须于写一个Se...
freejob@163.com
急着用,大家救救急啊
用什么方法都无所谓,最好是源码,拿来就用的
不要再applet写数据库的连接信息,谢谢
急着用,大家救救急啊
用什么方法都无所谓,最好是源码,拿来就用的
不要再applet写数据库的连接信息,谢谢
|
关键问题是:
你不能用Applet与数据库直接交流,你必须于写一个Servlet类,让Servlet充当Applet与数据库的连接,我曾经写过一个Applet显示用户访问统计的小程序,不过不是从数据库里提取和存入,而是直接去修改文本文件。不过你自己可以举一反三的嘛:
先是Applet:
package counter;
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
public class ViewCounter extends Applet{
Label l;
public void init(){
super.init();
setLayout(new BorderLayout());
l=new Label(" ", Label.CENTER);
l.setFont(new Font("Dialog",Font.PLAIN,12));
add("Center",this.l);
this.setBackground(Color.black);
l.setBackground(Color.black);
l.setForeground(Color.white);
try{
String location="http://localhost:8080/bologu/servlet/CounterServlet";
URLConnection uc=(new URL(/tech-qa-java/location/index.html)).openConnection();
InputStream fromServlet=uc.getInputStream();
DataInputStream dis=new DataInputStream(
new BufferedInputStream(fromServlet));
String s;
StringBuffer sb=new StringBuffer();
while((s=dis.readLine())!=null){
sb.append(s);
}
l.setText(sb.toString());
dis.close();
fromServlet.close();
}catch(Exception e){l.setText("error!!");}
this.l.setAlignment(Label.CENTER);
}
}
****** Servlet *******
package counter;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class CounterServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private Properties proper=new Properties();
private int count;
public void init() throws ServletException {
super.init();
}
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
this.process(request,response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
this.process(request,response);
}
protected void process(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
response.setContentType("text/html;charset=gb2312");
DataInputStream in=new DataInputStream(
new BufferedInputStream(
new FileInputStream(new File("E:/bologu/bologu/file/visitedcount.props"))));
proper.load(in);
if(proper.getProperty("count")!=null) this.count=Integer.parseInt(proper.getProperty("count"));
this.count++;
String s="#访问量统计n"+"count="+this.count;
PrintStream note=new PrintStream(
new BufferedOutputStream(
new FileOutputStream(new File("E:/bologu/bologu/file/visitedcount.props"))));
note.println(s);
String outCount=Integer.toString(this.count);
PrintWriter out=response.getWriter();
StringBuffer sb=new StringBuffer(Integer.toString(this.count));
out.println(sb.toString());
out.flush();
in.close();
note.close();
out.close();
}
}
你不能用Applet与数据库直接交流,你必须于写一个Servlet类,让Servlet充当Applet与数据库的连接,我曾经写过一个Applet显示用户访问统计的小程序,不过不是从数据库里提取和存入,而是直接去修改文本文件。不过你自己可以举一反三的嘛:
先是Applet:
package counter;
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
public class ViewCounter extends Applet{
Label l;
public void init(){
super.init();
setLayout(new BorderLayout());
l=new Label(" ", Label.CENTER);
l.setFont(new Font("Dialog",Font.PLAIN,12));
add("Center",this.l);
this.setBackground(Color.black);
l.setBackground(Color.black);
l.setForeground(Color.white);
try{
String location="http://localhost:8080/bologu/servlet/CounterServlet";
URLConnection uc=(new URL(/tech-qa-java/location/index.html)).openConnection();
InputStream fromServlet=uc.getInputStream();
DataInputStream dis=new DataInputStream(
new BufferedInputStream(fromServlet));
String s;
StringBuffer sb=new StringBuffer();
while((s=dis.readLine())!=null){
sb.append(s);
}
l.setText(sb.toString());
dis.close();
fromServlet.close();
}catch(Exception e){l.setText("error!!");}
this.l.setAlignment(Label.CENTER);
}
}
****** Servlet *******
package counter;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class CounterServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private Properties proper=new Properties();
private int count;
public void init() throws ServletException {
super.init();
}
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
this.process(request,response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
this.process(request,response);
}
protected void process(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
response.setContentType("text/html;charset=gb2312");
DataInputStream in=new DataInputStream(
new BufferedInputStream(
new FileInputStream(new File("E:/bologu/bologu/file/visitedcount.props"))));
proper.load(in);
if(proper.getProperty("count")!=null) this.count=Integer.parseInt(proper.getProperty("count"));
this.count++;
String s="#访问量统计n"+"count="+this.count;
PrintStream note=new PrintStream(
new BufferedOutputStream(
new FileOutputStream(new File("E:/bologu/bologu/file/visitedcount.props"))));
note.println(s);
String outCount=Integer.toString(this.count);
PrintWriter out=response.getWriter();
StringBuffer sb=new StringBuffer(Integer.toString(this.count));
out.println(sb.toString());
out.flush();
in.close();
note.close();
out.close();
}
}