Hibernate逍遥游记-第1章-JDBC访问数据库
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 /** 构造方法 */ 30 public MonkeyGui(){ 31 buildDisplay(); 32 } 33 34 /** 创建图形界面 */ 35 private void buildDisplay(){ 36 frame=new JFrame("花果山信息管理系统"); 37 buildCustPanel(); 38 39 //向主窗体中加入custPan面板 40 contentPane=frame.getContentPane(); 41 contentPane.setLayout(new BorderLayout()); 42 contentPane.add(custPan,BorderLayout.CENTER); 43 44 frame.pack(); 45 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 46 frame.setVisible(true); 47 48 } 49 50 51 /** 创建custPan面板 */ 52 private void buildCustPanel(){ 53 custPan.setLayout(new GridLayout(4,2,5,5)); 54 custPan.add(nameLb); 55 custPan.add(nameTf); 56 custPan.add(genderLb); 57 custPan.add(genderTf); 58 custPan.add(ageLb); 59 custPan.add(ageTf); 60 61 custPan.add(addBt); 62 custPan.add(logLb); 63 64 addBt.addActionListener(this); 65 66 } 67 68 public void actionPerformed(ActionEvent event){ 69 try{ 70 Monkey monkey=new Monkey(); 71 monkey.setName(nameTf.getText().trim()); 72 monkey.setAge(Integer.parseInt(ageTf.getText().trim())); 73 monkey.setGender(genderTf.getText().trim().charAt(0)); 74 businessService.saveMonkey(monkey); 75 logLb.setText("猴子信息已经保存成功。"); 76 }catch(Exception e){ 77 logLb.setText("猴子信息保存失败。"); 78 e.printStackTrace(); 79 } 80 81 } 82 83 public static void main(String args[]){ 84 new MonkeyGui(); 85 } 86 87 }
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 47 <target name="rungui" description="Run a Hibernate sample" 48 depends="compile" > 49 <java classname="mypack.MonkeyGui" fork="true"> 50 <classpath refid="project.class.path"/> 51 </java> 52 </target> 53 54 </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(); } }