|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
轮性能微软曾做过一个例子,就是同一个项目用java和.net来作,结果开发周期,.net是java的一半,性能java是.net的十分之一,代码量java是.net的三倍。呵呵,这说明了什么,.net的全方位比java好。但是有的人说.net不能跨平台,这个问题我和我同学曾讨论过,都认为微软的.net很可能早都可以跨平台了,但是微软为了保护他们的操作系统,所以才没有推出跨平台的.net,只是推出了跨语言的.net,程序|数据|数据库|显现道理:
<1>从数据库中读取blob到Blob对象.Blobblob=rs.getBlob(2);
<2>把blob传给Stream.
<3>用Stream参数创立Image对象
<4>显现Image对象.
申明:这只是例子,没有思索其他方面.但愿人人能对java毗连数据库,和取对象有个详细的懂得.
关于数据库的sql
CREATETABLEphoto(
photo_noint(6)unsignedNOTNULLauto_increment,
imageblob,
PRIMARYKEY(`photo_no`)
)
本例子用到两个文件:
DrawPanel.java以下
importjava.awt.*;
importjava.sql.*;
importjava.awt.image.*;
importjava.io.*;
importjavax.swing.*;
/**
*<p>Title:</p>
*
*<p>Description:vincentlaw@163.com</p>
*
*<p>Copyright:Copyright(c)2005</p>
*
*<p>Vincent</p>
*
*@authorvincent
*@version1.0
*/
publicclassDrawPanelextendsPanel{
Imageim;
Insetsinsets;
publicDrawPanel(){
im=Toolkit.getDefaultToolkit().getImage("c:1.jpg");//默许的文件
try{
jbInit();
}catch(Exceptionex){
ex.printStackTrace();
}
}
publicvoidaddNotify(){
super.addNotify();
insets=getInsets();
setBounds(100,100,217+insets.left,321+insets.top);
}
publicvoidpaint(Graphicsg){
g.drawImage(im,insets.left,insets.top,this);
}
privatevoidjbInit()throwsException{
}
publicvoidchangeImage(StringfileName)
{
try{
//Class.forName("oracle.jdbc.driver.OracleDriver");
//Connectionconn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl2","user","password");
Class.forName("com.mysql.jdbc.Driver");
Connectionconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/job?user=root&password=5672831");
Statementstmt=conn.createStatement();
ResultSetrs=stmt.executeQuery("selectphoto_no,photo_imagefromphoto");
rs.next();
Blobblob=rs.getBlob(2);
//数据库毗连
InputStreamfin=blob.getBinaryStream();
im=javax.imageio.ImageIO.read(fin);
rs.close();
stmt.close();
conn.close();
}
catch(Exceptione)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null,"毛病,装载当地文件");
im=Toolkit.getDefaultToolkit().getImage(fileName);
}
this.repaint();
}
}
ShowImage.java以下:
importjava.awt.BorderLayout;
importjava.awt.*;
importjavax.swing.*;
importjavax.swing.JToggleButton;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
/**
*<p>Title:</p>
*
*<p>Description:vincentlaw@163.com</p>
*
*<p>Copyright:Copyright(c)2005</p>
*
*<p>Vincent</p>
*
*@authorvincent
*@version1.0
*/
publicclassShowImageextendsJFrame{
BorderLayoutborderLayout1=newBorderLayout();
JToggleButtonjToggleButton1=newJToggleButton();
DrawPanelpanel=newDrawPanel();
publicShowImage(){
try{
jbInit();
}catch(Exceptionexception){
exception.printStackTrace();
}
}
privatevoidjbInit()throwsException{
getContentPane().setLayout(borderLayout1);
jToggleButton1.setText("jToggleButton1");
jToggleButton1.addActionListener(new
ShowImage_jToggleButton1_actionAdapter(this));
this.getContentPane().add(jToggleButton1,java.awt.BorderLayout.NORTH);
this.getContentPane().add(panel,java.awt.BorderLayout.CENTER);
}
publicvoidjToggleButton1_actionPerformed(ActionEvente){
panel.changeImage("E:photohoto1281.jpg");//备用图象文件
}
publicfinalstaticvoidmain(String[]args){
JFrameframe=newShowImage();
frame.setSize(800,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
classShowImage_jToggleButton1_actionAdapterimplementsActionListener{
privateShowImageadaptee;
ShowImage_jToggleButton1_actionAdapter(ShowImageadaptee){
this.adaptee=adaptee;
}
publicvoidactionPerformed(ActionEvente){
adaptee.jToggleButton1_actionPerformed(e);
}
}
java主要分三块,j2se:java的基础核心语言。j2me:java的微型模块,专门针对内存小,没有持续电源等小型设备。j2ee:java的企业模块,专门针对企业数据库服务器的连接维护。 |
|