package worddictionary;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import java.io.*;
import javax.swing.table.*;
import java.util.*;
public class WordDictionary extends JFrame
implements ActionListener
{
private JTextArea display;
private JButton delete,find,add;
private JLabel mean,word;
private JTextField meantext,wordtext;
private JPanel jp;
private JScrollPane jsp;
private JSplitPane jst;
private Connection conn;
private Statement stmt;
private JTable jt=new JTable();;
private Vector row;
private Vector headName=new Vector();
private DefaultTableModel dtm=new DefaultTableModel();;
private ResultSetMetaData rsmd;
public WordDictionary()
{
super("电子词典");
jp=new JPanel();
jp.setLayout(null);
row=new Vector();
String[] a={"单词","解释"};
for(int i=0;i<a.length;i++)
{
headName.add(a[i]);
}
dtm.setDataVector(row,headName);
jt.setModel(dtm);
word=new JLabel("单词");
word.setBounds(10,10,60,20);
mean=new JLabel("解释");
mean.setBounds(10,40,60,20);
meantext=new JTextField();
meantext.setBounds(80,40,60,20);
wordtext=new JTextField();
wordtext.setBounds(80,10,60,20);
delete=new JButton("删除");
delete.setBounds(150,80,60,20);
delete.addActionListener(this);
find=new JButton("查找");
find.setBounds(10,80,60,20);
find.addActionListener(this);
add=new JButton("添加");
add.setBounds(80,80,60,20);
add.addActionListener(this);
jp.add(mean);jp.add(word);
jp.add(meantext);jp.add(wordtext);
jp.add(delete);jp.add(find);jp.add(add);
jsp=new JScrollPane(jt);
jst=new JSplitPane(JSplitPane.VERTICAL_SPLIT,jp,jsp);
jst.setDividerLocation(120);
jst.setDividerSize(5);
this.add(jst);
this.setSize(600,400);
this.setVisible(true);
this.setResizable(true);
//服务器端界面居中显示
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if(frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
if(frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
setLocation((screenSize.width - frameSize.width)/2,
(screenSize.height - frameSize.height)/2);
this.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
try
{
conn = getConnection();
stmt = conn.createStatement();
System.out.println("数据库连接成功");
}
catch(SQLException e)
{
System.out.println("1111");
}
catch(IOException e)
{
System.out.println("2555");
}
catch(ClassNotFoundException e)
{
System.out.println("666");
}
//execuiteQuery();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==add)
{
String g=wordtext.getText().trim();
String h=meantext.getText().trim();
ResultSet rs=null;
if(g.equals("")||h.equals(""))
{
JOptionPane.showMessageDialog(null, "请输入单词或解释", "提示",
JOptionPane.WARNING_MESSAGE);
}
else
{
try
{
rs=stmt.executeQuery("SELECT * FROM test where word="+"'"+g+"'");
if(rs.next())
{
JOptionPane.showMessageDialog(null, "此编号已经被注册.", "警告",
JOptionPane.WARNING_MESSAGE);
return;
}
else
{
stmt.execute(
"insert into test(word,mean) values('"
+g+"','"+h+"')");
JOptionPane.showMessageDialog(null, "增加信息成功!");
}
}
catch(Exception eg)
{
System.out.println("添加按钮错误");
}
}
}
if(e.getSource()==delete)
{
String t=wordtext.getText().trim();
ResultSet rs=null;
if(t.equals(""))
{
JOptionPane.showMessageDialog(null, "未输入单词", "提示",
JOptionPane.WARNING_MESSAGE);
}
try
{
rs=stmt.executeQuery("SELECT * FROM test where word="+"'"+t+"'");
if(!rs.next())
{
JOptionPane.showMessageDialog(null, "未找到该单词", "提示",
JOptionPane.WARNING_MESSAGE);
}
if (JOptionPane.showConfirmDialog(this,
"确实要删除该信息吗?/n删除的信息将不能恢复,继续?",
"删除确定", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE) == 0)
{
stmt.execute("delete from test where word="+"'"+t+"'");
JOptionPane.showMessageDialog(null, "删除成功!");
}
}
catch(Exception ee)
{
System.out.println("删除按钮错误");
}
}
if(e.getSource()==find)
{
try
{
String s=wordtext.getText().trim();
String t=meantext.getText().trim();
ResultSet rs=null;
if(s.equals("")&&t.equals(""))
{
JOptionPane.showMessageDialog(null, "请输入单词", "提示",
JOptionPane.WARNING_MESSAGE);
}
else
{
if(!s.equals(""))
{
rs=stmt.executeQuery("select * from test where word="+"'"+s+"'");
if(rs.next())
{
Vector v=new Vector();
v.add(rs.getString(1));
v.add(rs.getString(2));
row.add(v);
rs.close();
dtm.setDataVector(row,headName);
jt.setModel(dtm);
}
else
{
JOptionPane.showMessageDialog(null, "没有找到!!", "提示",
JOptionPane.WARNING_MESSAGE);
}
}
if(!t.equals(""))
{
rs=stmt.executeQuery("select * from test where mean="+"'"+t+"'");
Vector v=new Vector();
if(rs.next())
{
v.add(rs.getString(1));
v.add(rs.getString(2));
row.add(v);
rs.close();
dtm.setDataVector(row,headName);
jt.setModel(dtm);
}
else
{
JOptionPane.showMessageDialog(null, "没有找到!!", "提示",
JOptionPane.WARNING_MESSAGE);
}
}
}
}
catch(SQLException ee)
{
System.out.println("查找按钮错误");
}
}
}
public static Connection getConnection()
throws IOException,SQLException,ClassNotFoundException
{
String url="jdbc:odbc:word";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
return DriverManager.getConnection(url,"","");
}
public void execuiteQuery()
{
try
{
ResultSet rs=stmt.executeQuery("select * from test");
while(rs.next())
{
Vector cols=new Vector();
cols.add(rs.getString(1));
cols.add(rs.getString(2));
row.add(cols);
}
rs.close();
}
catch(SQLException e)
{
System.out.println("表格添加错误");
}
dtm.setDataVector(row,headName);
jt.setModel(dtm);
}
public static void main(String[] args)
{
new WordDictionary();
}
}