Java英汉小词典.doc_第1页
Java英汉小词典.doc_第2页
Java英汉小词典.doc_第3页
Java英汉小词典.doc_第4页
Java英汉小词典.doc_第5页
免费预览已结束,剩余8页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

英汉小词典一实验目的:编写窗体应用程序,实现一个英汉词典的查询添加功能。该程序能够根据输入的英语单词查找该单词的汉语解释,若没有就提示错误信息;能够实现基本的添加英语单词与该单词汉语翻译的功能。二具体实现过程:编写相关类,实现添加查询单词功能,并且使用数据库对单词进行存储访问,主要要实现的类如下:1.编写窗体DictionaryFrm类,添加相应的标签,按钮,选项卡等组件实现词典主窗口界面。2.使用Oracle数据库建一张Dictionary表,对单词进行添加存储和查询操作。3.编写一个GetConn类主要用于实现与Oracle数据库的连接操作。4.编写一个Dictionary类,用于设置单词的ID,English,Chinese这3个字段,实现对单词的各字段的获取和修改;然后编写InsertDictionary类和FindDictionary类实现对单词的添加和查询操作。编写UpdateDictionary类实现对已有单词的修改操作。三相关类的作用与实现的功能:单词的存储用到Oracle数据库的表,表中包含3个属性字段,程序运行前已在数据库中建好了表Dictionary。1.Dictionary类,包含3个成员变量ID,English,Chinese。主要用于设置字典中对应单词的ID号,英文解释,中文翻译等。为了不允许外部直接访问和修改该对象中的属性,类中实现了对每个成员变量值的获取和设置的方法,即实现对该类对象的封装。2.DictionaryFrm类,主要实现英汉字典操作界面,通过选项卡设置2个选项,实现添加单词和查询单词两个功能,在两个选项中JLabel,JButton等相关组件,对按钮添加监听器,实现相关操作。3.GetConn类,主要用于实现java与Oracle数据库的连接,以便添加,修改与查询单词的过程中,实现单词在数据库中的存储与更新操作。4. InsertDictionary类和FindDictionary类,UpdateDictionary类,这三个类调用GetConn类实现与Oracle数据库的连接后,实现添加,查询,修改单词的相关操作,并将单词的添加修改保存到数据库中。四具体程序与代码实现如下:(1).Dictionary类代码:package com.xd.bean;public class Dictionary private int id; / 对应表中id字段private String english; / 对应表中英文信息字段private String chinese; / 对应表中文中信息字段/定义属性的设置器和访问器public int getId() return id;public void setId(int id) this.id = id;public String getEnglish() return english;public void setEnglish(String english) this.english = english;public String getChinese() return chinese;public void setChinese(String chinses) this.chinese = chinses;(2),Dictionary类的代码:package com.xd.bean;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.xd.jdbc.*;public class DictionaryFrm extends JFrame private JTextField chinJText, engJText, inEngJText, inChinJText;private JButton updateJButton;public static void main(String args) EventQueue.invokeLater(new Runnable() public void run() try DictionaryFrm frame = new DictionaryFrm();frame.setVisible(true); catch (Exception e) e.printStackTrace(););public DictionaryFrm() super();setBounds(300, 300, 400, 300);setResizable(false);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);final JPanel panel = new JPanel();panel.setLayout(null);getContentPane().add(panel, BorderLayout.CENTER);final JLabel nameJlabel = new JLabel(英汉小词典, JLabel.CENTER);nameJlabel.setFont(new java.awt.Font(幼圆, 0, 24);nameJlabel.setBounds(110, 10, 202, 41);panel.add(nameJlabel);final JTabbedPane tabbedPane = new JTabbedPane();tabbedPane.setBounds(0, 66, 404, 221);panel.add(tabbedPane);setTitle(英汉小词典);final JPanel panel1 = new JPanel();panel1.setLayout(null);tabbedPane.addTab(添加, null, panel1, null);final JLabel saveLabel = new JLabel(要添加的单词:);saveLabel.setBounds(10, 25, 117, 18);panel1.add(saveLabel);inEngJText = new JTextField();inEngJText.setBounds(146, 20, 192, 28);panel1.add(inEngJText);final JLabel label = new JLabel();label.setText(该单词的解释为:);label.setBounds(23, 77, 104, 18);panel1.add(label);inChinJText = new JTextField();inChinJText.setBounds(146, 72, 192, 28);panel1.add(inChinJText);final JButton btninsert = new JButton();btninsert.setText(添加);btninsert.setBounds(271, 125, 67, 28);panel1.add(btninsert);btninsert.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) insertJButtonActionPerformed(e););final JPanel pane2 = new JPanel();pane2.setLayout(null);tabbedPane.addTab(查询, null, pane2, null);final JLabel label1 = new JLabel();label1.setText(要查询的单词:);label1.setBounds(21, 27, 126, 18);pane2.add(label1);engJText = new JTextField();engJText.setBounds(153, 22, 179, 28);pane2.add(engJText);final JLabel label2 = new JLabel();label2.setText(该词的解释为:);label2.setBounds(43, 72, 91, 18);pane2.add(label2);chinJText = new JTextField();chinJText.setBounds(153, 67, 179, 28);pane2.add(chinJText);final JButton btnselect = new JButton(查询);btnselect.setBounds(258, 114, 71, 28);pane2.add(btnselect);btnselect.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) selectJButtonActionPerformed(e);); updateJButton = new JButton(修改);updateJButton.setEnabled(false);updateJButton.setBounds(180, 114, 71, 28);pane2.add(updateJButton);updateJButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) updateJButtonActionPerformed(e);); /*查询事件处理*private void selectJButtonActionPerformed(ActionEvent evt) if (engJText.getText().equals() / 如果用户没有指定要翻译的单词JOptionPane.showMessageDialog(this, 没有输入要翻译的单词, 警告,JOptionPane.WARNING_MESSAGE);return; else String english = engJText.getText();FindDictionary findDictionary = new FindDictionary();Dictionary dictionary = findDictionary.findDictionary(english);if (dictionary = null) / 如果数据表中不包含该单词JOptionPane.showMessageDialog(this, 字典中没有包含该单词, 警告,JOptionPane.WARNING_MESSAGE);return; else / 数据表中包含有要查询的单词chinJText.setText(dictionary.getChinese();updateJButton.setEnabled(true);/ 将该单词的中文解释在文本框中显示 /*添加事件处理*private void insertJButtonActionPerformed(ActionEvent evt) if (inEngJText.getText().equals() / 如果用户没有输入要添加的英文单词JOptionPane.showMessageDialog(this, 请输入要添加的英文内容, 警告,JOptionPane.WARNING_MESSAGE);return; else if (inChinJText.getText().equals() / 如果用户没有输入该单词的中文翻译JOptionPane.showMessageDialog(this, 请输入要添加的中文翻译, 警告,JOptionPane.WARNING_MESSAGE);return; else String english = inEngJText.getText();FindDictionary findDictionary = new FindDictionary();Dictionary dictionaryEnglish = findDictionary.findDictionary(english);if (dictionaryEnglish = null) / 如果数据库中没有该单词Dictionary dictionary = new Dictionary();dictionary.setChinese(inChinJText.getText();dictionary.setEnglish(english);InsertDictionary insertDictionary = new InsertDictionary();insertDictionary.insertDictionary(dictionary); / 调用添加单词的方法JOptionPane.showMessageDialog(this, 单词添加成功, 信息提示框,JOptionPane.WARNING_MESSAGE); else / 如果表中有该单词JOptionPane.showMessageDialog(this, 该单词已经存在, 警告提示框,JOptionPane.WARNING_MESSAGE); /*修改事件处理*private void updateJButtonActionPerformed(ActionEvent evt) if (engJText.getText().equals() / 如果用户没有输入要添加的英文单词JOptionPane.showMessageDialog(this, 没有需要修改的英文内容!, 警告,JOptionPane.WARNING_MESSAGE);return; else if (chinJText.getText().equals() / 如果用户没有输入该单词的中文翻译JOptionPane.showMessageDialog(this, 请输入要修改的中文翻译!, 警告,JOptionPane.WARNING_MESSAGE);return; else String english = engJText.getText();FindDictionary findDictionary = new FindDictionary();Dictionary dictionaryEnglish = findDictionary.findDictionary(english);if (dictionaryEnglish!= null) / 如果数据库中有该单词Dictionary dictionary = new Dictionary();dictionary.setChinese(chinJText.getText();dictionary.setEnglish(english);UpdateDictionary insertDictionary = new UpdateDictionary();insertDictionary.updateDictionary(dictionary); / 调用更新数据表方法JOptionPane.showMessageDialog(this, 单词修改成功!, 信息,JOptionPane.INFORMATION_MESSAGE); (3).GetConn类代码:package com.xd.jdbc;import java.sql.*;public class GetConn public Connection conn = null; / 获取数据库连接方法public Connection getConnection() try Class.forName(oracle.jdbc.OracleDriver); / 加载数据库驱动String url = jdbc:oracle:thin:pc-xiangdong:1521:orcl;String user = system;String passWord = xd;conn = DriverManager.getConnection(url, user, passWord);/连接数据库if (conn != null) System.out.println(数据库连接成功!); catch (Exception e) System.out.println(数据库连接失败!);e.printStackTrace();return conn;public static void main(String args) GetConn getConn = new GetConn(); / 创建GetConn对象getConn.getConnection(); / 调用连接数据库方法(4),FindDictionary类代码:package com.xd.jdbc;import java.sql.*;import com.xd.bean.Dictionary;public class FindDictionary /查询单词信息GetConn getConn = new GetConn(); Connection connection = getConn.getConnection(); / 获取数据库连接/ 按英文名称,查询信息public Dictionary findDictionary(String english) Dictionary dictionary = null; String sql = select * from dictionary where english= + english+ ; / 定义数据查询SQL语句try PreparedStatement statement = connection.prepareStatement(sql); ResultSet rest = statement.executeQuery(); while (rest.next() dictionary = new Dictionary(); dictionary.setChinese(rest.getString(chinese); / 获取结果集中数据 catch (SQLException e) e.printStackTrace();return dictionary;(5),InsertDictionary类代码:package com.xd.jdbc;import java.sql.*;import com.xd.bean.Dictionary;public class InsertDictionary / 添加单词信息GetConn getConn = new GetConn();Connection conn = getConn.getConnection(); / 获取数据库连接/ 向字典表中添加信息public void insertDictionary(Dictionary dictionary) String sql1 = select max(id) from dictionary;String sql = insert into dictionary values (?,?,?); / 定义数据添加的SQL语句PreparedStatement statement1,statement;try statement1 = conn.prepareStatement(sql1);System.out.println(1);ResultSet res=statement1.executeQuery(); System.out.println(2); int id=0; while(res.next() id=res.getInt(1);System.out.println(id);statement = conn.prepareStatement(sql);statement.setString(1, (+id)+);statement.setString(2, dictionary.getEnglish();statement.setString(3, dictionary.getChinese();statement.executeUpdate(); / 执行SQL语句 catch (SQLException e) Syste

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论