首页资源分类嵌入式系统其他 > java电子词典

java电子词典

已有 445125个资源

下载专区

上传者其他资源

    文档信息举报收藏

    标    签:javamysql

    分    享:

    文档简介

    java代码编写界面,连接mysql数据库,实现电子词典功能,可以查询,修改,添加等功能

    文档预览

    程序代码: import java.awt.BorderLayout; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Label; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.Panel; import java.awt.TextArea; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; import sun.audio.AudioPlayer; class Ex extends Frame implements ActionListener { MenuBar menubar = new MenuBar();// 菜单 Menu fileMenu, editMenu, helpMenu; MenuItem fileenglish, filechinese, exit, editAdd, editmod, editDel; TextField inputtext; TextArea txt; Label label1, label2; Button btn1, btnsound; Panel p, p1, p2, p3; Ex() { super("电子词典"); setBounds(200, 200, 350, 300); setMenuBar(menubar); fileMenu = new Menu("文件"); editMenu = new Menu("编辑"); helpMenu = new Menu("帮助"); fileenglish = new MenuItem("英汉词典"); filechinese = new MenuItem("汉英词典"); exit = new MenuItem("退出"); editAdd = new MenuItem("添加词汇"); editmod = new MenuItem("修改词汇"); editDel = new MenuItem("删除词汇"); menubar.add(fileMenu); menubar.add(editMenu); menubar.add(helpMenu); fileMenu.add(fileenglish); fileMenu.add(filechinese); fileMenu.addSeparator(); fileMenu.add(exit); editMenu.add(editAdd); editMenu.add(editmod); editMenu.add(editDel); inputtext = new TextField("", 10); txt = new TextArea(10, 10); label1 = new Label("输入要查询的english单词:"); label2 = new Label("查询结果:"); btn1 = new Button("查询"); btnsound = new Button("发音"); p = new Panel(new BorderLayout()); p2 = new Panel(new FlowLayout(FlowLayout.LEFT, 5, 0)); p2.add(label1); p2.add(inputtext); p2.add(btn1); p2.add(btnsound); add(p2, "North"); p.add(label2, "North"); p.add(txt, "Center"); add(p, "Center"); setVisible(true); setResizable(false); validate(); fileenglish.addActionListener(this); filechinese.addActionListener(this); exit.addActionListener(this); editAdd.addActionListener(this); editmod.addActionListener(this); editDel.addActionListener(this); btn1.addActionListener(this); btnsound.addActionListener(this); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent e) { if (e.getSource() == fileenglish)// 英汉(外观变化) { label1.setText("输入要查询的english单词:"); label2.setText("查询结果:"); txt.setText(""); btn1.setLabel("查询"); btnsound.setVisible(true); } else if (e.getSource() == filechinese)// 汉英(外观变化) { label1.setText("输入要查询的chinese词语:"); label2.setText("查询结果:"); txt.setText(""); btn1.setLabel("查询"); btnsound.setVisible(true); } else if (e.getSource() == exit)// 退出 { System.exit(0); } else if (e.getSource() == btn1) { if (btn1.getLabel().equals("查询"))// 实现查询功能(包括英汉或汉英) { txt.setText(null); try { Listwords(); } catch (Exception ee) { ee.printStackTrace(); } } else if (btn1.getLabel().equals("提交"))// 实现添加功能 { try { addwords(); } catch (Exception ee) { } } else if (btn1.getLabel().equals("更新"))// 实现修改功能 { try { modwords(); } catch (SQLException ee) { ee.printStackTrace(); } } else if (btn1.getLabel().equals("删除"))// 实现删除功能 { try { delwords(); } catch (SQLException ee) { } } } else if (e.getSource() == editAdd)// 添加(外观变化) { label1.setText("输入新单词:"); label2.setText("输入中文解释:"); btn1.setLabel("提交"); btnsound.setVisible(false); } else if (e.getSource() == editmod)// 修改(外观变化) { label1.setText("输入要修改的单词:"); label2.setText("输入更新后的解释:"); btn1.setLabel("更新"); btnsound.setVisible(false); } else if (e.getSource() == editDel)// 删除(外观变化) { label1.setText("输入要删除的单词:"); label2.setText(""); btn1.setLabel("删除"); btnsound.setVisible(false); } else if (e.getSource() == btnsound)// 发音 { if (inputtext.getText() != null) { try { InputStream is = getClass().getResource( "sound//" + inputtext.getText().trim() + ".wav") .openStream(); AudioPlayer.player.start(is); } catch (IOException e1) { } } } } public void Listwords() // 查询实现过程 { System.out.println("asdfadfa"); String cname, ename; try { Class.forName("org.gjt.mm.mysql.Driver"); String url = "jdbc:mysql://localhost/test?user=root&password=root"; Connection Ex1Con = DriverManager.getConnection(url); Statement Ex1Stmt = Ex1Con.createStatement(); ResultSet rs = Ex1Stmt.executeQuery("SELECT * FROM words"); boolean boo = false; while ((boo = rs.next()) == true) { ename = rs.getString("english"); cname = rs.getString("chinese"); if (ename.equals(inputtext.getText()) && label1.getText().equals("输入要查询的english单词:")) { txt.append(cname); break; } else if (cname.equals(inputtext.getText()) && label1.getText().equals("输入要查询的chinese词语:")) { txt.append(ename); break; } } System.out.println("boo =" + boo); Ex1Con.close(); if (boo == false) { JOptionPane.showMessageDialog(this, "查无此单词!", "警告", JOptionPane.WARNING_MESSAGE); } } catch (Exception e) { e.printStackTrace(); } } public void addwords()// 向数据库添加新词汇 { try { String cname, ename; Class.forName("org.gjt.mm.mysql.Driver"); String url = "jdbc:mysql://localhost/test?user=root&password=root"; Connection Ex1Con = DriverManager.getConnection(url); Statement Ex1Stmt = Ex1Con.createStatement(); ResultSet rs = Ex1Stmt.executeQuery("SELECT * FROM words"); boolean boo = false; while ((boo = rs.next()) == true) { ename = rs.getString("english"); cname = rs.getString("chinese"); if (ename.equals(inputtext.getText()) && cname.equals(txt.getText())) { JOptionPane.showMessageDialog(this, "此词汇已存在!", "警告", JOptionPane.WARNING_MESSAGE); break; } } if (boo == false) { Ex1Stmt.executeUpdate("INSERT INTO words VALUES ('" + inputtext.getText().trim() + "','" + txt.getText().trim() + "')"); JOptionPane.showMessageDialog(this, "添加成功!", "恭喜", JOptionPane.WARNING_MESSAGE); } Ex1Con.close(); } catch (Exception e) { e.printStackTrace(); } } public void modwords() throws SQLException// 修改词库中记录 { String cname, ename; try { Class.forName("org.gjt.mm.mysql.Driver"); String url = "jdbc:mysql://localhost/test?user=root&password=root"; Connection Ex1Con = DriverManager.getConnection(url); Statement Ex1Stmt = Ex1Con.createStatement(); ResultSet rs = Ex1Stmt.executeQuery("SELECT * FROM words"); boolean boo = false; while ((boo = rs.next()) == true) { ename = rs.getString("english"); cname = rs.getString("chinese"); if (ename.equals(inputtext.getText())) { Ex1Stmt.executeUpdate("UPDATE words SET chinese='" + txt.getText().trim() + "' WHERE english='" + inputtext.getText().trim() + "'"); JOptionPane.showMessageDialog(this, "记录修改成功!", "恭喜", JOptionPane.WARNING_MESSAGE); break; } } Ex1Con.close(); if (boo == false) { JOptionPane.showMessageDialog(this, "不存在此单词!", "警告", JOptionPane.WARNING_MESSAGE); } } catch (ClassNotFoundException e) { } } public void delwords() throws SQLException// 删除词库中记录 { String cname, ename; try { Class.forName("org.gjt.mm.mysql.Driver"); String url = "jdbc:mysql://localhost/test?user=root&password=root"; Connection Ex1Con = DriverManager.getConnection(url); Statement Ex1Stmt = Ex1Con.createStatement(); ResultSet rs = Ex1Stmt.executeQuery("SELECT * FROM words"); boolean boo = false; while ((boo = rs.next()) == true) { ename = rs.getString("english"); cname = rs.getString("chinese"); if (ename.equals(inputtext.getText())) { Ex1Stmt.executeUpdate("DELETE FROM words WHERE english='" + inputtext.getText().trim() + "'"); JOptionPane.showMessageDialog(this, "成功删除记录!", "恭喜", JOptionPane.WARNING_MESSAGE); break; } } Ex1Con.close(); if (boo == false) { JOptionPane.showMessageDialog(this, "不存在此单词!", "警告", JOptionPane.WARNING_MESSAGE); } } catch (ClassNotFoundException e) { } } } public class Test { public static void main(String[] args) { new Ex(); } }

    Top_arrow
    回到顶部
    EEWORLD下载中心所有资源均来自网友分享,如有侵权,请发送举报邮件到客服邮箱bbs_service@eeworld.com.cn 或通过站内短信息或QQ:273568022联系管理员 高进,我们会尽快处理。