1.

  1. package mypack;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import javax.swing.table.*;
  7. import java.util.*;
  8.  
  9. public class MonkeyGui implements ActionListener{
  10. private BusinessService businessService=new BusinessService();
  11.  
  12. //界面的主要窗体组件
  13. protected JFrame frame;
  14. protected Container contentPane;
  15.  
  16. //主面板上的组件
  17. protected JPanel custPan=new JPanel();
  18. protected JLabel nameLb=new JLabel("猴子姓名");
  19. protected JLabel genderLb=new JLabel("性别");
  20. protected JLabel ageLb=new JLabel("年龄");
  21. protected JLabel logLb=new JLabel("");
  22.  
  23. protected JTextField nameTf=new JTextField(25);
  24. protected JTextField genderTf=new JTextField(25);
  25. protected JTextField ageTf=new JTextField(25);
  26. protected JButton addBt=new JButton("保存");
  27.  
  28. /** 构造方法 */
  29. public MonkeyGui(){
  30. buildDisplay();
  31. }
  32.  
  33. /** 创建图形界面 */
  34. private void buildDisplay(){
  35. frame=new JFrame("花果山信息管理系统");
  36. buildCustPanel();
  37.  
  38. //向主窗体中加入custPan面板
  39. contentPane=frame.getContentPane();
  40. contentPane.setLayout(new BorderLayout());
  41. contentPane.add(custPan,BorderLayout.CENTER);
  42.  
  43. frame.pack();
  44. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  45. frame.setVisible(true);
  46.  
  47. }
  48.  
  49. /** 创建custPan面板 */
  50. private void buildCustPanel(){
  51. custPan.setLayout(new GridLayout(4,2,5,5));
  52. custPan.add(nameLb);
  53. custPan.add(nameTf);
  54. custPan.add(genderLb);
  55. custPan.add(genderTf);
  56. custPan.add(ageLb);
  57. custPan.add(ageTf);
  58.  
  59. custPan.add(addBt);
  60. custPan.add(logLb);
  61.  
  62. addBt.addActionListener(this);
  63.  
  64. }
  65.  
  66. public void actionPerformed(ActionEvent event){
  67. try{
  68. Monkey monkey=new Monkey();
  69. monkey.setName(nameTf.getText().trim());
  70. monkey.setAge(Integer.parseInt(ageTf.getText().trim()));
  71. monkey.setGender(genderTf.getText().trim().charAt(0));
  72. businessService.saveMonkey(monkey);
  73. logLb.setText("猴子信息已经保存成功。");
  74. }catch(Exception e){
  75. logLb.setText("猴子信息保存失败。");
  76. e.printStackTrace();
  77. }
  78.  
  79. }
  80.  
  81. public static void main(String args[]){
  82. new MonkeyGui();
  83. }
  84.  
  85. }

2.

  1. package mypack;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.sql.*;
  5.  
  6. public class BusinessService{
  7. private String dbUrl ="jdbc:mysql://localhost:3306/SAMPLEDB";
  8. private String dbUser="root";
  9. private String dbPwd="1234";
  10.  
  11. static{
  12. try{
  13. Class.forName("com.mysql.jdbc.Driver");
  14. DriverManager.registerDriver(new com.mysql.jdbc.Driver());
  15. }catch(Exception e){throw new RuntimeException(e);}
  16. }
  17.  
  18. /** 持久化一个Monkey对象 */
  19. public void saveMonkey(Monkey monkey){
  20. Connection con=null;
  21. try {
  22. //建立数据库连接
  23. con = java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd);
  24. //创建一个SQL声明
  25. Statement stmt = con.createStatement();
  26. //向MONKEYS表插入记录
  27. stmt.executeUpdate("insert into MONKEYS(NAME,AGE,GENDER) values( "
  28. +"'"+monkey.getName()+"',"
  29. +monkey.getAge()+","
  30. +"'"+monkey.getGender()+"')");
  31. stmt.close();
  32. }catch(Exception e) {
  33. throw new RuntimeException(e);
  34. } finally {
  35. try{
  36. if(con!=null)con.close();
  37. }catch(Exception e){e.printStackTrace();}
  38. }
  39. }
  40.  
  41. }

3.

  1. package mypack;
  2.  
  3. public class Monkey{
  4. private Long id;
  5. private String name;
  6. private int age;
  7. private char gender;
  8.  
  9. public Monkey(){}
  10.  
  11. public Long getId(){
  12. return id;
  13. }
  14.  
  15. private void setId(Long id){
  16. this.id = id;
  17. }
  18.  
  19. public String getName(){
  20. return name;
  21. }
  22.  
  23. public void setName(String name){
  24. this.name=name;
  25. }
  26.  
  27. public int getAge(){
  28. return age;
  29. }
  30.  
  31. public void setAge(int age){
  32. this.age =age ;
  33. }
  34.  
  35. public char getGender(){
  36. return gender;
  37. }
  38.  
  39. public void setGender(char gender){
  40. this.gender =gender ;
  41. }
  42. }

4.

  1. drop database if exists SAMPLEDB;
  2. create database SAMPLEDB DEFAULT CHARACTER SET utf8;
  3. use SAMPLEDB;
  4.  
  5. create table MONKEYS (
  6. ID bigint not null auto_increment primary key,
  7. NAME varchar(30) not null,
  8. AGE int,
  9. GENDER char(1)
  10. );

5.build.xml

  1. <?xml version="1.0"?>
  2. <project name="Learning Hibernate" default="prepare" basedir=".">
  3.  
  4. <!-- Set up properties containing important project directories -->
  5. <property name="source.root" value="src"/>
  6. <property name="class.root" value="classes"/>
  7. <property name="lib.dir" value="lib"/>
  8.  
  9. <!-- Set up the class path for compilation and execution -->
  10. <path id="project.class.path">
  11. <!-- Include our own classes, of course -->
  12. <pathelement location="${class.root}" />
  13. <!-- Include jars in the project library directory -->
  14. <fileset dir="${lib.dir}">
  15. <include name="*.jar"/>
  16. </fileset>
  17. </path>
  18.  
  19. <!-- Create our runtime subdirectories and copy resources into them -->
  20. <target name="prepare" description="Sets up build structures">
  21. <delete dir="${class.root}"/>
  22. <mkdir dir="${class.root}"/>
  23.  
  24. <!-- Copy our property files and O/R mappings for use at runtime -->
  25. <copy todir="${class.root}" >
  26. <fileset dir="${source.root}" >
  27. <include name="**/*.properties"/>
  28. <include name="**/*.hbm.xml"/>
  29. <include name="**/*.xml"/>
  30. </fileset>
  31. </copy>
  32. </target>
  33.  
  34. <!-- Compile the java source of the project -->
  35. <target name="compile" depends="prepare"
  36. description="Compiles all Java classes">
  37. <javac srcdir="${source.root}"
  38. destdir="${class.root}"
  39. debug="on"
  40. optimize="off"
  41. deprecation="on">
  42. <classpath refid="project.class.path"/>
  43. </javac>
  44. </target>
  45.  
  46. <target name="rungui" description="Run a Hibernate sample"
  47. depends="compile" >
  48. <java classname="mypack.MonkeyGui" fork="true">
  49. <classpath refid="project.class.path"/>
  50. </java>
  51. </target>
  52.  
  53. </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. 跨域访问JSONP CORS

    一.JSONP 常用的Jquery框架支持jsonp方式请求,该方式只支持GET方法,传参大小有限,而且需要后台根据jsonp的请求方式进行封装结果返回. 其中参数jsonp默认为callback,j ...

  2. 关于内存的5个函数(malloc,VirtualAlloc,GlobalAlloc,LocalAlloc,HeapAlloc)

    VirtualAlloc 该函数的功能是在调用进程的虚地址空间,预定或者提交一部分页,如果用于内存分配的话,并且分配类型未指定MEM_RESET,则系统将自动设置为0 一次分配 1PAGE 以上的 R ...

  3. Hyper-V 虚拟机连接外部网络

    Hyper-V创建好虚拟机之后,在默认配置下是没有网络连接的,这个时候就需要进行简单的配置,即可让虚拟机连接外部网络: 在Hyper-V管理器中,右键点击后出现菜单,选择"虚拟交换机管理器& ...

  4. 清理c盘垃圾(将一下代码复制到记事本然后把后缀名改为xxx.bat,然后双击,就ok了!!)

    @echo off echo 正在清除系统垃圾文件,请稍等...... del /f /s /q %systemdrive%\*.tmp del /f /s /q %systemdrive%\*._m ...

  5. gen-cpp/.deps/ChildService.Plo: No such file or directory

    最近在编译 Thrift 的时候出现这种情况,我按照官方教程的要求,所有版本都是最新,但是还出现这种问题. ]: Entering directory `/home/yantze/dl/thrift/ ...

  6. asp.net 时间比较,常用于在某段时间进行操作

    DateTime.Compare(t1,t2)比较两个日期大小,排前面的小,排在后面的大,比如:2011-2-1就小于2012-3-2返回值小于零:  t1 小于 t2. 返回值等于零 : t1 等于 ...

  7. 纯JS焦点图特效(可一个页面多用)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. ListView 复制到剪切板

    private void 导出ToolStripMenuItem_Click(object sender, EventArgs e) { Clipboard.SetText(GetListView(l ...

  9. C# - write values to configuration file

    using System.Configuration;System.Configuration.Configuration config = ConfigurationManager.OpenExeC ...

  10. Spark小课堂Week5 Scala初探

    Spark小课堂Week5 Scala初探 Scala是java威力加强版. 对Java的改进 这里会结合StreamingContext.scala这个代码说明下对Java的改进方面. 方便测试方式 ...