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