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

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();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值