梦开始的地方

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  4 随笔 :: 0 文章 :: 3 评论 :: 0 Trackbacks

2006年9月7日 #

 Hibernate 的 一对一因映射关系,one-to-one的配置,有一些疑问
 举例说明
   Person.java
  class Person{
  private Mark mark;
  private int personId;
  private name;
 //get 和 set 方法深省略

}
  
 Mark.java
class Mark{
private int markid;
private Person;
//get和 set方法省略
}
//映射文件
Person.hbm.xml
<class name="test.Person" table="person">
 <id name="personId" type="integer">
            <column name="personId" />
            <generator class="increment" />
    </id>
<one-to-one name="address" class="test.Mark" cascade="save-update" />
</class>


Mark.hbm.xml
<class name="test.Mark" table="mark">
  <id name="markId" type="integer">
            <column name="addressId" />
            <generator class="increment" />
  </id>
<one-to-one name="address" class="test.Person" />
</class>

我的需求是:
增加或者修改表person表时,同时更新表mark表内容,而且不使用共享主键,每个表都有独立的主键,我这样配置时,始终报错,希望高手指点~在线等
posted @ 2006-10-17 18:42 Dushengjun 阅读(357) | 评论 (0)编辑 收藏

       在使用windows系统时,我们会发现,一种类型的文件总是可以用指定的应用程序打开,如:doc文件,双击可以打开文件,在使用delphi开发应用程序时,同样需要实现上边的功能,其核心就获取系统当前的文件,而ParamStr函数起到了非常重要的作用,先面我们简单用 Delphi开发以个应用程序来说明问题:
首先,用 delphi新建立一个工程,然后分别创建窗体MainFrm,MyMDIChildForm,添加相应的控件,如下图

            
                                           MainFrm

               
                                            MyMDIChildForm



代码如下:
Unit1.pas(对应于MainFrm窗体)
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus,Unit2, ComCtrls;

type
  TMainFrm = class(TForm)
    MainMenu1: TMainMenu;
    ItemNew: TMenuItem;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    OpenDialog1: TOpenDialog;
    StatusBar1: TStatusBar;
    procedure ItemNewClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    function CreateMDI():TMyMDIChildForm;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainFrm: TMainFrm;

implementation


{$R *.dfm}

procedure TMainFrm.ItemNewClick(Sender: TObject);
var
child: TMyMDIChildForm;
begin
 child:= TMyMDIChildForm.create(Self);
end;

procedure TMainFrm.N3Click(Sender: TObject);

begin
  if opendialog1.Execute then
   begin

      CreateMDI.RichEdit1.Lines.LoadFromFile(opendialog1.FileName);
      Self.StatusBar1.Panels[1].Text:=opendialog1.FileName;
   end;

end;

procedure TMainFrm.FormShow(Sender: TObject);
var
 FileName:String;
begin
   FileName:=ParamStr(1);
   if FileExists(FileName) then
    begin
      CreateMDI.RichEdit1.Lines.LoadFromFile(FileName);
    end;
end;

 

function TMainFrm.CreateMDI: TMyMDIChildForm;
var
child: TMyMDIChildForm;
begin
    child:= TMyMDIChildForm.create(Self);
    Result:=child;
end;

end.
unit2.pas(对应于MyMDIChildForm窗体)
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TMyMDIChildForm = class(TForm)
    RichEdit1: TRichEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MyMDIChildForm: TMyMDIChildForm;

implementation

{$R *.dfm}

procedure TMyMDIChildForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
 Action:=caFree;
end;

end.
这样我们实现了一个简单的记事本程序,运行结果如下图:


              


然后,我们将工程保存,并生成最终的EXE文件,然后我们在Windows系统目录任意创建一个文件,
如下图ResumeAction.java文件

                         

然后将它的打开方式设置成我们自己的可执行程序
                                   

然后再双击此文件,程序正确运行,并将它打开了!
            

程序的核心就是paramStr函数,它获得了系统接口,得到了当前的文件名称!

点击下载

posted @ 2006-09-20 22:00 Dushengjun 阅读(592) | 评论 (0)编辑 收藏

       Spring是当前比较流行的基于Java语言的MVC框架,所谓框架也就是它已经实现好了诸多东西,使java开发人员能把精力尽量放在业务逻辑上.
       Spring技术的特点是IOC, 即反向注入,主要应用的是XML技术和POJO(简单Java对象),Spring要达到的目的其实很简单,就是尽量简化原来Java中的地层数据访问的问题,众所周知,传统的使用Java访问地层数据库的方法是使用JDBC,如下的代码:
   try{
       }catch(SQLException e){
                   Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");/以MS SQL SERVER为例
                   Connection conn=DriverManager.getConnection(connurl,username,password);
                   Statement stmt=conn.createStatement();
                   ResultSet rs=stmt.executeQuery(sql);
         //实现业务逻辑的代码
                   System.out.println("产生异常:"+e);
          }
可以看到为了访问数据库,我们需要这么多多余的模式的代码,而且又要抛出不同的异常,代码可维护性比较差,而且不利于开发人员把精力投放到最终的业务逻辑上.上边的功能如果选择使用Spring实现,那就变得的非常简单了:
  <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="url" value="${database.url}"></property>
  <property name="driverClassName" value="${database.driver}"></property>
  <property name="username" value="${database.user}"></property>
  <property name="password" value="${database.password}"></property>
 </bean>  
我们只需要培植上文的XML文件,然后在真正使用数据库时,用
      ApplicationContext   ctx=new   FileSystemXmlApplicationContext("beans.xml");
        DriverManagerDataSource ds=(DriverManagerDataSource)ctx.getBean("datasource");
两条语句完成数据源的获得,这就简化掉了相当多的模式代码,让我们更加关心业务逻辑的实现了~ 
     
posted @ 2006-09-11 13:16 Dushengjun 阅读(940) | 评论 (3)编辑 收藏

    最近两天,很巧,又写delphi的电子邮件客户端,还写了Java的电子邮件发送的小程序,一个是工作需要,另一个则是帮朋友的忙,有点巧.
1.Java的电子有件发送程序
SendMailBean.java
package adu.com.kingsoft;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailBean {
    private String form;
    private String to;
    private String hostName;
    private String port;
    private String userName;
    private String password;
    private String subject;
    private String body;
    public String getBody() {
        return body;
    }
    public void setBody(String body) {
        this.body = body;
    }
    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public String getForm() {
        return form;
    }
    public void setForm(String form) {
        this.form = form;
    }
    public String getHostName() {
        return hostName;
    }
    public void setHostName(String hostName) {
        this.hostName = hostName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getPort() {
        return port;
    }
    public void setPort(String port) {
        this.port = port;
    }
    public String getTo() {
        return to;
    }
    public void setTo(String to) {
        this.to = to;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public boolean sendMail(){
          Properties props=new Properties();
        //  props.put("mail.smtp.protocol","smtp");
          props.put("mail.smtp.host",hostName);
          props.put("mail.smtp.auth","true");
          props.put("mail.smpt.port",port);    
          Session mailsession=Session.getInstance(props,null); //得到一个发送邮件的会话
         
          Message msg=new MimeMessage(mailsession);
          try{
           msg.setFrom(new InternetAddress(this.getForm()));
           msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(this.getTo()));
           msg.setSubject(this.getSubject());
           msg.setSentDate(new Date());
           msg.setText(this.getBody());
           Transport transport = mailsession.getTransport("smtp");
           transport.connect(this.getHostName(),this.getUserName(),this.getPassword());
           transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));
          // System.out.println("邮件以成功发送到dushengjun@gmail.com");
           transport.close();
           return true;
          }catch(Exception e)
          {
              System.out.println("产生异常:"+e);
              return false;
          }
    }
}
 
JavaMailApp .java
package adu.com.kingsoft;
import java.util.Date;
import java.util.Properties;
public class JavaMailApp {
   
    /**
     * @param args
     */
    public static void main(String[] args) {
       System.out.println("正在准备发送.....");
       SendMailBean smb=new SendMailBean();
       smb.setUserName("dushengjun");
       smb.setForm("dushengjun@kingsoft.net");
       smb.setHostName("mail.kingsoft.net");
       smb.setPassword("***************");
       smb.setPort("25");
       smb.setTo("dushengjun@gmail.com");
       smb.setSubject("你好");
       smb.setBody("你好 Java");
       if(smb.sendMail())
       System.out.println("发送成功");
       else
           System.out.println("发送失败");
    }
}
同时将如下的包加到classpath中:
activation.jar,dsn.jar,imap.jar,mailapi.jar,pop3.jar,smtp.jar
可以测试了,运行一下JavaMailApp 吧
 
2.Delphi的电子邮件客户端实现
 
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdMessageClient, IdSMTP, IdMessage, ComCtrls;
type
  TForm1 = class(TForm)
    idSMTP: TIdSMTP;
    Button1: TButton;
    idMessage: TIdMessage;
    letter_subject: TEdit;
    me_content: TRichEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    receiver_address: TEdit;
    Label4: TLabel;
    me_address: TEdit;
    me_pwd: TLabel;
    mypassword: TEdit;
    Label5: TLabel;
    me_port: TEdit;
    myHostName: TEdit;
    Label6: TLabel;
    Edit1: TEdit;
    Label7: TLabel;
    Button2: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
procedure   TForm1.Button1Click(Sender:   TObject);
var
 subject           :String;
 rec_address       :String;
 self_address      :String;
 password          :String;
 content           :String;
 port              :Integer;
 hostName          :String;
begin
subject:=letter_subject.Text;
rec_address:=receiver_address.Text;
self_address:=me_address.Text;
port:=StrToInt(me_port.Text);
password:=mypassword.text;
hostName:=myHostName.Text;
content:=me_content.text;
      if idMessage   =   NIL    then
          idMessage.Create(Form1);  
      if idSMTP=NIL   then
          idSMTP.Create(Form1);
        TIdAttachment.Create(IdMessage.MessageParts,edit1.Text);
       IdMessage.Body.Assign(me_content.lines);
       IdMessage.From.Text := self_address;
       IdMessage.ReplyTo.EMailAddresses := self_address;
       IdMessage.Recipients.EMailAddresses := rec_address;
       IdMessage.Subject := subject;
      idSMTP.AuthenticationType   :=   atLogin;
     // idSMTP.AuthenticationType:=atNone;
      idSMTP.Username   :=   self_address;
      idSMTP.Password   :=   password;
      idSMTP.Host           :=hostName;
      idSMTP.Port           :=   port;
      try
          idSMTP.Connect;
          idSMTP.Send(idMessage);
         //idSMTP.DisconnectSocket;
          idSMTP.Disconnect;
          //idSMTP.Destroy;
          showmessage('send   mail   ok');
      except
          //idSMTP.Destroy;
          //idMessage.Destroy;  
          showmessage('mail   send   false');  
      end;  
  end;
procedure TForm1.Button2Click(Sender: TObject);
begin
 if(openDialog1.Execute) then
  begin
   Edit1.Text:=opendialog1.FileName;
  end;
end;
end.
 

 
posted @ 2006-09-07 12:39 Dushengjun 阅读(291) | 评论 (0)编辑 收藏