加载中…
个人资料
艳儿
艳儿
  • 博客等级:
  • 博客积分:0
  • 博客访问:5,578
  • 关注人气:0
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

java实现简单的英汉翻译功能

(2012-06-15 17:28:00)
标签:

杂谈

分类: 学习乐园

package BDO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Dao {
  private static final String DRIVER_CLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  private static final String DATABASE_URL = "jdbc:sqlserver://localhost:1433;DatabaseName=fanyi";
  private static final String DATABASE_USRE = "sa";
  private static final String DATABASE_PASSWORD = "sa";
  
  public static Connection getConnection() {
   Connection dbConnection = null;
   try {
    Class.forName(DRIVER_CLASS);
    dbConnection = DriverManager.getConnection(DATABASE_URL,
      DATABASE_USRE, DATABASE_PASSWORD);
   } catch (Exception e) {
    e.printStackTrace();
   }

   return dbConnection;
  }

  
  public static void closeConnection(Connection dbConnection) {
   try {
    if (dbConnection != null && (!dbConnection.isClosed())) {
     dbConnection.close();
    }
   } catch (SQLException sqlEx) {
    sqlEx.printStackTrace();
   }

  }

  
  public static void closeResultSet(ResultSet res) {
   try {
    if (res != null) {
     res.close();
     res = null;
    }
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }

  

  public static void closeStatement(PreparedStatement pStatement) {
   try {
    if (pStatement != null) {
     pStatement.close();
     pStatement = null;
    }
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }

package BDO;

public class huyi {
private String china;
private String english;
public String getChina() {
 return china;
}
public void setChina(String china) {
 this.china = china;
}
public String getEnglish() {
 return english;
}
public void setEnglish(String english) {
 this.english = english;
}

}

package java1;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.*;

import BDO.Dao;
import BDO.huyi;

public class Huyi implements ActionListener {
 JFrame jf = new JFrame("单词翻译");
 JLabel lb1 = new JLabel("英译汉");
 JLabel lb2 = new JLabel("请输入英语");
 JTextField text = new JTextField();
 JButton b = new JButton("翻译");

 public Huyi() {
  jf.setLayout(null);
  lb1.setFont((new Font("楷体", Font.PLAIN, 35)));
  lb1.setBounds(160, 40, 150, 60);
  jf.add(lb1);
  lb2.setFont((new Font("宋体", Font.PLAIN, 25)));
  lb2.setBounds(50, 150, 160, 30);
  jf.add(lb2);
  text.setFont((new Font("宋体", Font.PLAIN, 25)));
  text.setBounds(200, 150, 130, 30);
  jf.add(text);
  b.setFont((new Font("宋体", Font.PLAIN, 25)));
  b.setBounds(140, 240, 140, 30);
  jf.add(b);
  b.addActionListener(this);
  jf.setSize(400, 400);
  jf.setVisible(true);

 }

 public static void main(String[] args) {
  new Huyi();
 }

 public void actionPerformed(ActionEvent e) {
  if (e.getActionCommand().equals("翻译")) {
   huyi china = new huyi();
   Connection conn = null;
   PreparedStatement pstmt = null;
   ResultSet rs = null;
   try {
    conn = Dao.getConnection();
    String str = "select china from huyi where english=?";
    pstmt = conn.prepareStatement(str);
    pstmt.setString(1, text.getText());
    rs = pstmt.executeQuery();
    if (rs.next()) {
     String mess = rs.getString(1);
     JOptionPane.showMessageDialog(null, mess, "翻译结果",
       JOptionPane.INFORMATION_MESSAGE);
    } else {
     JOptionPane.showMessageDialog(null, "这个英语单词我不知道!", "错误信息",
       JOptionPane.ERROR_MESSAGE);
     text.setText("");
    }
   } catch (Exception ee) {
    ee.printStackTrace();
   }
  }
 }

}

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有