1.

 package mypack;

 import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*; public class MonkeyGui implements ActionListener{
private BusinessService businessService=new BusinessService(); //界面的主要窗体组件
protected JFrame frame;
protected Container contentPane; //主面板上的组件
protected JPanel custPan=new JPanel();
protected JLabel nameLb=new JLabel("猴子姓名");
protected JLabel genderLb=new JLabel("性别");
protected JLabel ageLb=new JLabel("年龄");
protected JLabel logLb=new JLabel(""); protected JTextField nameTf=new JTextField(25);
protected JTextField genderTf=new JTextField(25);
protected JTextField ageTf=new JTextField(25);
protected JButton addBt=new JButton("保存"); /** 构造方法 */
public MonkeyGui(){
buildDisplay();
} /** 创建图形界面 */
private void buildDisplay(){
frame=new JFrame("花果山信息管理系统");
buildCustPanel(); //向主窗体中加入custPan面板
contentPane=frame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(custPan,BorderLayout.CENTER); frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); } /** 创建custPan面板 */
private void buildCustPanel(){
custPan.setLayout(new GridLayout(4,2,5,5));
custPan.add(nameLb);
custPan.add(nameTf);
custPan.add(genderLb);
custPan.add(genderTf);
custPan.add(ageLb);
custPan.add(ageTf); custPan.add(addBt);
custPan.add(logLb); addBt.addActionListener(this); } public void actionPerformed(ActionEvent event){
try{
Monkey monkey=new Monkey();
monkey.setName(nameTf.getText().trim());
monkey.setAge(Integer.parseInt(ageTf.getText().trim()));
monkey.setGender(genderTf.getText().trim().charAt(0));
businessService.saveMonkey(monkey);
logLb.setText("猴子信息已经保存成功。");
}catch(Exception e){
logLb.setText("猴子信息保存失败。");
e.printStackTrace();
} } public static void main(String args[]){
new MonkeyGui();
} }

2.

 package mypack;
import java.io.*;
import java.util.*;
import java.sql.*; public class BusinessService{
private String dbUrl ="jdbc:mysql://localhost:3306/SAMPLEDB";
private String dbUser="root";
private String dbPwd="1234"; static{
try{
Class.forName("com.mysql.jdbc.Driver");
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
}catch(Exception e){throw new RuntimeException(e);}
} /** 持久化一个Monkey对象 */
public void saveMonkey(Monkey monkey){
Connection con=null;
try {
//建立数据库连接
con = java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd);
//创建一个SQL声明
Statement stmt = con.createStatement();
//向MONKEYS表插入记录
stmt.executeUpdate("insert into MONKEYS(NAME,AGE,GENDER) values( "
+"'"+monkey.getName()+"',"
+monkey.getAge()+","
+"'"+monkey.getGender()+"')");
stmt.close();
}catch(Exception e) {
throw new RuntimeException(e);
} finally {
try{
if(con!=null)con.close();
}catch(Exception e){e.printStackTrace();}
}
} }

3.

 package mypack;

 public class Monkey{
private Long id;
private String name;
private int age;
private char gender; public Monkey(){} public Long getId(){
return id;
} private void setId(Long id){
this.id = id;
} public String getName(){
return name;
} public void setName(String name){
this.name=name;
} public int getAge(){
return age;
} public void setAge(int age){
this.age =age ;
} public char getGender(){
return gender;
} public void setGender(char gender){
this.gender =gender ;
}
}

4.

 drop database if exists SAMPLEDB;
create database SAMPLEDB DEFAULT CHARACTER SET utf8;
use SAMPLEDB; create table MONKEYS (
ID bigint not null auto_increment primary key,
NAME varchar(30) not null,
AGE int,
GENDER char(1)
);

5.build.xml

 <?xml version="1.0"?>
<project name="Learning Hibernate" default="prepare" basedir="."> <!-- Set up properties containing important project directories -->
<property name="source.root" value="src"/>
<property name="class.root" value="classes"/>
<property name="lib.dir" value="lib"/> <!-- Set up the class path for compilation and execution -->
<path id="project.class.path">
<!-- Include our own classes, of course -->
<pathelement location="${class.root}" />
<!-- Include jars in the project library directory -->
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</path> <!-- Create our runtime subdirectories and copy resources into them -->
<target name="prepare" description="Sets up build structures">
<delete dir="${class.root}"/>
<mkdir dir="${class.root}"/> <!-- Copy our property files and O/R mappings for use at runtime -->
<copy todir="${class.root}" >
<fileset dir="${source.root}" >
<include name="**/*.properties"/>
<include name="**/*.hbm.xml"/>
<include name="**/*.xml"/>
</fileset>
</copy>
</target> <!-- Compile the java source of the project -->
<target name="compile" depends="prepare"
description="Compiles all Java classes">
<javac srcdir="${source.root}"
destdir="${class.root}"
debug="on"
optimize="off"
deprecation="on">
<classpath refid="project.class.path"/>
</javac>
</target> <target name="rungui" description="Run a Hibernate sample"
depends="compile" >
<java classname="mypack.MonkeyGui" fork="true">
<classpath refid="project.class.path"/>
</java>
</target> </project>

6.在命令行中输入 ant rungui,会编译代码后运行MonkeyGui类的main()方法

7.

package mypack;
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.table.*;import java.util.*;
public class MonkeyGui implements ActionListener{  private BusinessService businessService=new BusinessService();
  //界面的主要窗体组件  protected JFrame frame;  protected Container contentPane;
  //主面板上的组件  protected JPanel custPan=new JPanel();  protected JLabel nameLb=new JLabel("猴子姓名");  protected JLabel genderLb=new JLabel("性别");  protected JLabel ageLb=new JLabel("年龄");  protected JLabel logLb=new JLabel("");
  protected JTextField nameTf=new JTextField(25);  protected JTextField genderTf=new JTextField(25);  protected JTextField ageTf=new JTextField(25);  protected JButton addBt=new JButton("保存");
    /** 构造方法 */  public MonkeyGui(){    buildDisplay();  }    /** 创建图形界面 */  private void buildDisplay(){   frame=new JFrame("花果山信息管理系统");   buildCustPanel();         //向主窗体中加入custPan面板   contentPane=frame.getContentPane();   contentPane.setLayout(new BorderLayout());   contentPane.add(custPan,BorderLayout.CENTER);    frame.pack();   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   frame.setVisible(true);
  }     /** 创建custPan面板 */  private void buildCustPanel(){   custPan.setLayout(new GridLayout(4,2,5,5));   custPan.add(nameLb);   custPan.add(nameTf);   custPan.add(genderLb);   custPan.add(genderTf);   custPan.add(ageLb);   custPan.add(ageTf);
   custPan.add(addBt);   custPan.add(logLb);    addBt.addActionListener(this);
  }    public void actionPerformed(ActionEvent event){    try{      Monkey monkey=new Monkey();      monkey.setName(nameTf.getText().trim());      monkey.setAge(Integer.parseInt(ageTf.getText().trim()));      monkey.setGender(genderTf.getText().trim().charAt(0));      businessService.saveMonkey(monkey);        logLb.setText("猴子信息已经保存成功。");    }catch(Exception e){      logLb.setText("猴子信息保存失败。");      e.printStackTrace();    }      }   public static void main(String args[]){    new MonkeyGui();  }   }

Hibernate逍遥游记-第1章-JDBC访问数据库的更多相关文章

  1. Hibernate逍遥游记-第15章处理并发问题-003乐观锁

    1. 2. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; drop table if exists ...

  2. Hibernate逍遥游记-第15章处理并发问题-002悲观锁

    1. 2. hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.mys ...

  3. Hibernate逍遥游记-第12章 映射值类型集合-002映射Bag(<idbag><collection-id>)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  4. Hibernate逍遥游记-第12章 映射值类型集合-001映射set(<element>)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  5. Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  6. Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  7. Hibernate逍遥游记-第10章 映射继承关系-001继承关系树中的每个具体类对应一个表

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  8. Hibernate逍遥游记-第9章 Hibernate的映射类型

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  9. Hibernate逍遥游记-第8章 映射组成关系(<component>、<parent>)

    一. 1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

随机推荐

  1. VS2013编译WEBKIT

    0,安装VS2013:DXSDK_Jun10.exe:QuickTimeSDK.exe 1,WebKit-r174650.tar.bz2 以管理员解压(非管理员解压最后几下总是报错) 2,设置环境变量 ...

  2. 《QQ欢乐斗地主》山寨版

    使用Cocos2d-x编写,模仿<QQ欢乐斗地主>的界面实现了一个具有简单AI的单机版斗地主游戏. 游戏的详细说明请查看游戏目录下的help.txt文件. 下载地址: http://dow ...

  3. HTML5之 WebSockets

    ------- 新的网络连接技术 - Web-Sockets 持续连接数据流 全双工工作方式 http补充品而非替代品 - 应用场景 聊天室 股票显示 在线游戏(尤为突出) - 2byte的通信 1b ...

  4. java 读写锁

    http://tutorials.jenkov.com/java-concurrency/read-write-locks.html 翻译 读写锁比LOCK的实现更复杂,想象有一个应用程序能读和写一些 ...

  5. [Interview][CodingExam]

    這次去Interview, 其中有一個公司 把我列為 2/25的考慮對象, 在Final 的 1/2, 我被刷掉了. 因為第一輪的程式,我稍微google了一下,參考了既有的寫法. 即使第二輪我用完全 ...

  6. CorelDRAW 文件实用工具 CDRTools 2

    随着 CorelDRAW 更新脚步越来越频繁,版本之间兼容性问题越来越突出,特别是跨版本之间打开会有很多问题,比如:文字跑位.透镜变向.位图出错.颜色改变,甚至会造成文件损坏.最好的办法就是哪一个版本 ...

  7. C语言多线程编程

    HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, DWORD dwStackSize, LPTHREAD_START_ROUT ...

  8. win系统一键安装JDK和Tuxedo

    @echo off title JDK和tuxedo环境变量设置 color 0a set /p inputTUX= [请输入你要设置的tuxedo的安装目录:] if /i "%input ...

  9. 【BZOJ 1497】 [NOI2006]最大获利

    Description 新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战.THU集团旗下的CS&T通讯公司在新一代通讯技术血战的前夜,需要做太多的准备工作,仅就站址选择一 ...

  10. 【斜率DP】BZOJ 3675:[Apio2014]序列分割

    3675: [Apio2014]序列分割 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 1066  Solved: 427[Submit][Statu ...