說先說:你的代碼有錯(cuò)誤,在最下面,我已經(jīng)該過了,不是大問題。
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),金平企業(yè)網(wǎng)站建設(shè),金平品牌網(wǎng)站建設(shè),網(wǎng)站定制,金平網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,金平網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
如果你想學(xué)好java的話,可以根據(jù)這個(gè)代碼加上我的注解去理解,但不要學(xué)這個(gè)代碼的變成方式或者說變成習(xí)慣,這個(gè)代碼基本上無誤,但犯了一些常識(shí)性問題,如果養(yǎng)成了這些不好的習(xí)慣對(duì)以后的編程會(huì)有壞的影響。
我是英文學(xué)的java,所以有些注解可能不通順,但我盡力而為了。 有的注解有點(diǎn)長,所以你復(fù)制到編譯器后稍微編輯一下就可以運(yùn)行。代碼是可以運(yùn)行,沒有問題的。
------------------------------------
import javax.swing.*; //用來創(chuàng)建圖形界面,如窗口,表格,按鈕等。
import java.awt.*; //作用同上,但已經(jīng)很少用,能用swing的地方就不要用awt
import java.awt.event.*;//事件管理和控制
import java.sql.*; //數(shù)據(jù)庫語句和操作
import java.lang.System;//這個(gè)不知道
/*下面的這6個(gè)沒有用,純屬寫出來嚇人*/
import javax.swing.tree.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.lang.String.*;
class Mywindow extends JFrame implements ActionListener//這個(gè)類implements actionlistener,意思就是它自己就可以執(zhí)行actionListener的任務(wù)
{
JTextField txf=new JTextField(20); //建一個(gè)文字編輯框,長度20(只可以輸入一行文字)
JTextArea jt=new JTextArea(10,30);//建一個(gè)文字編輯區(qū)域,長10寬30(可以回車然后輸入多行文字)
JButton btn1=new JButton("查詢");//建一個(gè) 查詢 按鈕
Mywindow()//構(gòu)造函數(shù),每個(gè)類必有的,可以為空
{
JFrame frm=new JFrame("Search");//建一個(gè)窗口(讓其他的東西有地方可放,和容器一樣。是3個(gè)最高級(jí)別的容器之一,其他兩個(gè)是applet和window)
frm.setBounds(400,300,450,350);//設(shè)置大小和位置,前兩個(gè)是坐標(biāo),后兩個(gè)是大小
Container con=getContentPane();//建一個(gè)awt容器對(duì)象,用來添加其他元素,最好用這個(gè)添加元素。像:frm.add(all); 可以寫成 con.add(all);
JPanel pnl4=new JPanel();//建一個(gè)面板用來添加其他元素(第二級(jí)別容器,最后需要被添加在frame上)
pnl4.setBorder(BorderFactory.createTitledBorder("Search"));//設(shè)置邊框樣式
pnl4.add(txf);//把文字編輯框添加到面板上
pnl4.add(btn1);//把按鈕添加到面板上
btn1.addActionListener(this);//添加事件行為監(jiān)聽器(this),this意思是當(dāng)前對(duì)象,呼應(yīng) implements ActionListener
JPanel pnl5=new JPanel();//同上
pnl5.setBorder(BorderFactory.createTitledBorder("Result"));//同上
jt.setWrapStyleWord(true);//這個(gè)忘了
jt.setLineWrap(true);//在區(qū)域規(guī)定的寬度下,如果文字的輸入到一行最后則會(huì)自動(dòng)令其一行繼續(xù),如果是(false),文字輸入就會(huì)在這一行繼續(xù)知道回車
pnl5.add(new JScrollPane(jt));//個(gè)這個(gè)面板添加右側(cè)滾動(dòng)條,當(dāng)文字輸入超過 長* 寬后 滾動(dòng)條出現(xiàn)
JPanel all=new JPanel();//同上
all.setLayout(new BorderLayout());//設(shè)置布局,borderlayout()分東西南北(上下左右)中五個(gè)部分 無論窗口多大,中間占得面積最大
all.add(pnl4,BorderLayout.NORTH);//添加一個(gè)面板在上面
all.add(pnl5,BorderLayout.CENTER);//添加一個(gè)在中間
frm.add(all);//把最大的這個(gè)面板添加到窗口上 也可以用con.add(all);
frm.setVisible(true);//設(shè)置窗口顯示屬性 如果false就是不顯示
frm.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});//加一個(gè)窗口監(jiān)聽 如果點(diǎn)小紅叉關(guān)閉窗口則系統(tǒng)推出
}
public void actionPerformed(ActionEvent e)//作為ActionListener類的構(gòu)造函數(shù),如果你的class implements ActionListenser, 那就必須得有這個(gè),也可以單獨(dú)寫一個(gè)class,不過有點(diǎn)麻煩
{
if(e.getSource()==btn1)//當(dāng)按鈕被點(diǎn)擊的時(shí)候
{
String str="";//建一個(gè)字符串
String tmp=this.txf.getText();//同上,這個(gè)字符串的值是當(dāng)前對(duì)象(窗口)中,文本框輸入的值
for(int k=0;ktmp.length();k++)//建一個(gè) 永久循環(huán)
str+=tmp.charAt(k)+"%";//把 % 插入每一個(gè)字符后面, 作用后面說
String sql=null;//同上
Statement stmt=null;//定義一個(gè)stmt,用來建數(shù)據(jù)庫連接的
sql="select * from chinese where charsound like'"+str+"'";//創(chuàng)建一個(gè)sql數(shù)據(jù)庫語句,但它本身還是一個(gè)字符串
System.out.println(sql);//系統(tǒng)顯示創(chuàng)建的語句,通常找錯(cuò)時(shí)候用的
try{//try 和 catch 的作用一句兩句說不清楚 不知道你就自己查查
Class.forName("com.mysql.jdbc.Driver");//或者:Class.forName("org.gjt.mm.mysql.Driver");關(guān)聯(lián)mysql數(shù)據(jù)庫驅(qū)動(dòng)
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/japan?user=rootpassword=sa");//建立連接,數(shù)據(jù)庫名japan(為什么不是chinese?)用戶名root密碼sa
stmt=conn.createStatement();//建立statement對(duì)象,用來發(fā)送sql語句到數(shù)據(jù)庫
ResultSet rs=stmt.executeQuery(sql);//運(yùn)行語句并建立一個(gè)查詢結(jié)果的集合
System.out.println("\n------------------------search :"+str+"-------------------------------");//同上
jt.setText("");//清空文本編輯區(qū)域
while(rs.next())//while循環(huán),當(dāng)還有結(jié)果的時(shí)候,把所有查詢結(jié)果添加加到文本編輯區(qū)域中
{
jt.append(new String(rs.getString("charname").getBytes("iso-8859-1"),"gb2312")+"\t");
System.out.print(new String(rs.getString("charname").getBytes("iso-8859-1"),"gb2312")+"\t");
}
stmt.close();//關(guān)閉關(guān)連,很重要。
}
catch(Exception eq){System.out.println("error");}
//--------------------------------------------------------------end btn1-------
}
}
public static void main(String args[])
{
Mywindow win=new Mywindow();//建立一個(gè) mywindow 對(duì)象
win.pack();//將所有元素整合
win.show();
}
}
public class Test4 {
static MapString, String map = new TreeMapString, String();
static {
map.put("watermelon", "西瓜");
map.put("banana", "香蕉");
map.put("strawberry", "草莓");
map.put("apple", "蘋果");
}
public static void main(String[] args) {
System.out.println("請(qǐng)輸入單詞");
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str1 = sc.nextLine();
if(str1.equals("退出")){
return;
}
else if (map.containsKey(str1)) {
System.out.println(map.get(str1));
} else{
System.out.println("次單詞為新詞,添加意思");
Scanner sc1 = new Scanner(System.in);
String str2=sc1.nextLine();
map.put(str1, str2);
System.out.println("添加成功。");
}
}
}
}
使用java提供的國際化功能就可以了。不過建議你使用框架技術(shù)中的國際化,框架技術(shù)中的國際化都做過封裝,實(shí)現(xiàn)起來相對(duì)簡單的多。只需要簡單的配置就可以實(shí)現(xiàn)中英文或者其他語言的切換了!~
lz ?你好
代碼還是比較簡單 ?就是需要一個(gè)做好的txt英漢詞典文檔
以下是一個(gè)簡單的例子:
import?java.awt.*;
import?javax.swing.*;
import?java.awt.event.*;
import?java.io.*;
import?java.util.*;
public?class?EC_Dictionary?extends?JFrame{
private?JTextField?input;
private?JButton?search;
private?JTextArea?output;
public?EC_Dictionary(){
super("英漢詞典");
input?=?new?JTextField(14);
search?=?new?JButton("查詢");
search.setFont(new?Font("宋體",?Font.PLAIN,?15));
search.addActionListener(new?ActionListener(){
public?void?actionPerformed(ActionEvent?e){
searchWords();
}
});
output?=?new?JTextArea(10,18);
output.setEditable(false);
output.setFont(new?Font("宋體",?Font.PLAIN,?18));
output.setForeground(Color.RED);
setLayout(new?FlowLayout(FlowLayout.CENTER,?5,?20));
getContentPane().add(input);
getContentPane().add(search);
getContentPane().add(output);
setSize(300,320);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(3);
setVisible(true);
}
//查詢單詞
public?void?searchWords(){
try?{
BufferedReader?br?=?new?BufferedReader(new?FileReader("dictionary.txt"));
String?line,?inputWord;
boolean?isFound?=?false;
inputWord?=?input.getText();
if(inputWord.equals("")){
return;
}
while((line?=?br.readLine())?!=?null){
Scanner?in?=?new?Scanner(line);
if(in.next().equals(inputWord)){
int?offset?=?inputWord.length();
output.setText("\n\n\n\n"+line.substring(offset));
isFound?=?true;
break;
}
}
if(!isFound){
output.setText("沒找到相應(yīng)項(xiàng)..");
}
}
catch?(Exception?ex)?{
ex.printStackTrace();
}
}
public?static?void?main?(String[]?args)?{
new?EC_Dictionary();
}
}
運(yùn)行效果:
ps:
本程序要用附件中dictionary.txt文檔 ?lz要把源程序和這個(gè)文檔放在同目錄下 ?才能正常運(yùn)行
希望能幫助你哈
package zyhz;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.io.*;
import com.sun.media.sound.*;
public class Dictionary {
public Dictionary() {
}
public static void main(String[] args)
{
dicFrame frame = new dicFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class dicFrame extends JFrame
{
public dicFrame()
{
setTitle("Dictionary");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
panel = new JPanel();
add(panel);
//菜單欄
JMenu fileMenu = new JMenu("文件");
JMenuItem ECItem = new JMenuItem("英漢字典");
ECItem.addActionListener(new
AbstractAction(){
public void actionPerformed(ActionEvent event)
{
text2.setText("請(qǐng)?jiān)谏厦孑斎胗⑽模。。。?);
}
});
JMenuItem CEItem = new JMenuItem("漢英字典");
CEItem.addActionListener(new
AbstractAction(){
public void actionPerformed(ActionEvent event)
{
text2.setText("請(qǐng)?jiān)谏厦孑斎胫形模。。。?);
}
});
//備份詞庫
JMenuItem BackupItem = new JMenuItem("備份詞庫");
BackupItem.addActionListener(new
AbstractAction(){
public void actionPerformed(ActionEvent event)
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ce) {
System.out.println(ce);
}
url = "jdbc:odbc:test";
String fileName;
chooser = new JFileChooser();
frame = new JFrame();
chooser.setDialogTitle("備份詞庫為");
try {
flag = chooser.showSaveDialog(frame);
} catch (HeadlessException h) {
System.out.println("Save File Dialog ERROR!");
}
if (flag == JFileChooser.APPROVE_OPTION) {
f = chooser.getSelectedFile();
fileName = chooser.getName(f);
fileName = chooser.getSelectedFile().getPath();
try {
File saveFile = new File(fileName);
FileWriter fw = new FileWriter(saveFile);
try
{
con = DriverManager.getConnection(url);
s = con.createStatement();
rs = s.executeQuery("SELECT * from 字典 ");
while (rs.next()) {
String a = rs.getString("單詞");
String b = rs.getString("詞語解釋");
text2.append(a+b+"\n");
}
rs.close();
} catch (Exception ea) {
text2.setText("查詢數(shù)據(jù)失敗");
}
fw.write(text2.getText());
fw.close();
} catch (IOException ec) {
text2.setText(fileName);
}
text2.setText("詞庫備份成功!");
}
}
});
fileMenu.add(ECItem);
fileMenu.add(CEItem);
fileMenu.add(BackupItem);
fileMenu.addSeparator();
//退出監(jiān)聽器
fileMenu.add(new
AbstractAction("退出")
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
JMenu editMenu = new JMenu("編輯");
JMenuItem addItem = new JMenuItem("添加詞匯");
addItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
//菜單欄增加監(jiān)聽器
addItem.addActionListener(new
AbstractAction(){
public void actionPerformed(ActionEvent event)
{
if(text1.getText().equals(""))
text2.setText("沒有輸入內(nèi)容!請(qǐng)重新增加?。?!");
else
{
String s=text1.getText().trim();
String s1=text2.getText().trim();
text2.setText(new add().jadd(text1.getText().trim(),text2.getText().trim()));
text2.append(s+"\n"+s1+";"+" "+"http://"+"該內(nèi)容已增加?。?!");
}
}
});
//菜單欄修改監(jiān)聽器
JMenuItem updateItem = new JMenuItem("修改詞匯");
updateItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,InputEvent.CTRL_MASK));
updateItem.addActionListener(new
AbstractAction(){
public void actionPerformed(ActionEvent event)
{
if(text1.getText().equals(""))
text2.setText("沒輸入所要修改的內(nèi)容!請(qǐng)重新輸入?。。。?);
else{
String s=text1.getText().trim();
text2.setText(new update().jupdate(text1.getText().trim(),
text2.getText().trim()));
text2.append(s+" "+"http://"+"該內(nèi)容已修改?。?!");
}
}
});
//菜單欄刪除監(jiān)聽器
JMenuItem deleteItem = new JMenuItem("刪除詞匯");
deleteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_MASK));
deleteItem.addActionListener(new
AbstractAction(){
public void actionPerformed(ActionEvent event)
{
if(text1.getText().equals(""))
text2.setText("沒輸入所要?jiǎng)h除的內(nèi)容!請(qǐng)重新輸入?。。?!");
else{
String s=text1.getText().trim();
text2.setText(new delete().jdelete(text1.getText().trim()));
text2.append(s+" "+"http://"+"該內(nèi)容已刪除?。。?);
}
}
});
editMenu.add(addItem);
editMenu.add(updateItem);
editMenu.add(deleteItem);
JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic('H');
aboutItem = new JMenuItem("About");
aboutItem.setMnemonic('A');
aboutItem.addActionListener(new
AbstractAction(){
public void actionPerformed(ActionEvent event)
{
new guanyu();
}
});
helpMenu.add(aboutItem);
menubar = new JMenuBar();
setJMenuBar(menubar);
menubar.add(fileMenu);
menubar.add(editMenu);
menubar.add(helpMenu);
//工具欄
label = new JLabel("請(qǐng)輸入:");
text1= new JTextField("");
selectButton = new JButton("查詢");
//工具欄查詢監(jiān)聽器
selectButton.addActionListener(new
AbstractAction(){
public void actionPerformed(ActionEvent event)
{
if(text1.getText().equals(""))
text2.setText("沒有輸入查詢內(nèi)容!請(qǐng)重新輸入!??!");
else text2.setText(new select().jselect(text1.getText().trim()));
}
});
//工具欄增加監(jiān)聽器
addButton = new JButton("增加");
addButton.addActionListener(new
AbstractAction(){
public void actionPerformed(ActionEvent event)
{
if(text1.getText().equals(""))
text2.setText("沒有輸入內(nèi)容!請(qǐng)重新增加?。?!");
else
{
String s=text1.getText().trim();
String s1=text2.getText().trim();
text2.setText(new add().jadd(text1.getText().trim(),text2.getText().trim()));
text2.append(s+"\n"+s1+";"+" "+"http://"+"該內(nèi)容已增加?。?!");
}
}
});
//工具欄修改監(jiān)聽器
updateButton = new JButton("修改");
updateButton.addActionListener(new
AbstractAction(){
public void actionPerformed(ActionEvent event)
{
if(text1.getText().equals(""))
text2.setText("沒輸入所要修改的內(nèi)容!請(qǐng)重新輸入?。。?!");
else{
String s=text1.getText().trim();
text2.setText(new update().jupdate(text1.getText().trim(),
text2.getText().trim()));
text2.append(s+" "+"http://"+"該內(nèi)容已修改!?。?);
}
}
});
//工具欄刪除監(jiān)聽器
deleteButton = new JButton("刪除");
deleteButton.addActionListener(new
AbstractAction(){
public void actionPerformed(ActionEvent event)
{
if(text1.getText().equals(""))
text2.setText("沒輸入所要?jiǎng)h除的內(nèi)容!請(qǐng)重新輸入!?。?!");
else{
String s=text1.getText().trim();
text2.setText(new delete().jdelete(text1.getText().trim()));
text2.append(s+" "+"http://"+"該內(nèi)容已刪除!?。?);
}
}
});
soundButton = new JButton("讀音");
soundButton.addActionListener(new
AbstractAction(){
public void actionPerformed(ActionEvent event)
{
JavaSoundAudioClip player;
try{
strsound = text2.getText().trim();
FileInputStream ff = new FileInputStream("sound//"+strsound+".wav");
player = new JavaSoundAudioClip(ff);
player.play();
}
catch(Exception e) {
System.out.println("error");
e.printStackTrace();
}
}
});
//萬年歷
dateButton = new JButton("萬年歷");
dateButton.addActionListener(new
AbstractAction() {
public void actionPerformed(ActionEvent event)
{
new calendar();
}
});
toolbar = new JToolBar();
add(toolbar,BorderLayout.NORTH);
toolbar.add(label);
toolbar.add(text1);
toolbar.add(selectButton);
toolbar.add(addButton);
toolbar.add(updateButton);
toolbar.add(deleteButton);
toolbar.add(soundButton);
toolbar.add(dateButton);
text2 = new JTextArea(8,40);
text2.setLineWrap(true);
JScrollPane scroll = new JScrollPane(text2);
add(scroll,BorderLayout.CENTER);
//快捷菜單 剪切,復(fù)制,粘貼
JPopupMenu popup= new JPopupMenu();
text2.setComponentPopupMenu(popup);
popup.add(new
AbstractAction("剪切",new ImageIcon("cut.gif"))
{
public void actionPerformed(ActionEvent event)
{
text2.cut();
}
});
popup.add(new
AbstractAction("復(fù)制",new ImageIcon("copy.gif"))
{
public void actionPerformed(ActionEvent event)
{
text2.copy();
}
});
popup.add(new
AbstractAction("粘貼",new ImageIcon("paste.gif"))
{
public void actionPerformed(ActionEvent event)
{
text2.paste();
}
});
text1.setComponentPopupMenu(popup);
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 250;
private JPanel panel;
private JMenuBar menubar;
private JToolBar toolbar;
private JLabel label;
private JTextField text1;
private JTextArea text2;
private JButton selectButton;
private JButton addButton;
private JButton updateButton;
private JButton deleteButton;
private JButton soundButton;
private JButton dateButton;
private JMenuItem aboutItem;
private Connection con;
private String url;
private Statement s;
private ResultSet rs;
public BufferedWriter Buffer;
private JFileChooser chooser;
private File f;
private JFrame frame;
private int flag;
private boolean b1;
private String strsound;
}
//查詢類
class select {
private Connection con;
private String url;
private PreparedStatement pstmt;
private String sql,zz;
private ResultSet rs;
select() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ce) {
System.out.println(ce);
}
url = "jdbc:odbc:test";
}
public String jselect(String str) {
try {
con = DriverManager.getConnection(url);
sql = "select 詞語解釋 from 字典 where 單詞=?";
pstmt= con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
pstmt.setString(1,str);
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.print(rs.getString(1));
zz=rs.getString(1);
System.out.println(" ");
}
rs.close();
pstmt.close();
con.close();
} catch (SQLException ce) {
System.out.println(ce);}
return zz;
}
}
//增加類
class add {
private Connection con;
private String url;
private PreparedStatement pstmt;
private String sql,sql1,zz;
private ResultSet rs;
add() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ce) {
System.out.println(ce);
}
url = "jdbc:odbc:test";
}
public String jadd(String str1,String str2) {
try {
con = DriverManager.getConnection(url);
sql = "insert into 字典 values(?,?)";
pstmt= con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
pstmt.setString(1,str1);
pstmt.setString(2,str2);
pstmt.executeUpdate();
Statement s = con.createStatement();
sql1 = "SELECT * FROM 字典 ";
rs = s.executeQuery(sql1);
while (rs.next()) {
System.out.print(rs.getString(1));
System.out.print(rs.getString(2));
System.out.println(" ");
}
rs.close();
pstmt.close();
con.close();
} catch (SQLException ce) {
System.out.println(ce);}
return zz;
}
}
//修改類
class update {
private Connection con;
private String url;
private PreparedStatement pstmt;
private String sql,sql1,zz;
private ResultSet rs;
update() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ce) {
System.out.println(ce);
}
url = "jdbc:odbc:test";
}
public String jupdate(String str1,String str2) {
try {
con = DriverManager.getConnection(url);
sql = "UPDATE 字典 SET 詞語解釋=? WHERE 單詞=?";
pstmt= con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
pstmt.setString(1,str1);
pstmt.setString(2,str2);
pstmt.executeUpdate();
Statement s = con.createStatement();
sql1 = "SELECT * FROM 字典 ";
rs = s.executeQuery(sql1);
while (rs.next()) {
System.out.print(rs.getString(1));
System.out.print(rs.getString(2));
System.out.println(" ");
}
rs.close();
pstmt.close();
con.close();
} catch (SQLException ce) {
System.out.println(ce);}
return zz;
}
}
//刪除類
class delete {
private Connection con;
private String url;
private PreparedStatement pstmt;
private String sql,sql1,zz;
private ResultSet rs;
delete() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ce) {
System.out.println(ce);
}
url = "jdbc:odbc:test";
}
public String jdelete(String str1) {
try {
con = DriverManager.getConnection(url);
sql = "delete 字典 WHERE 單詞=?";
pstmt= con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
pstmt.setString(1,str1);
pstmt.executeUpdate();
Statement s = con.createStatement();
sql1 = "SELECT * FROM 字典 ";
rs = s.executeQuery(sql1);
while (rs.next()) {
System.out.print(rs.getString(1));
System.out.print(rs.getString(2));
System.out.println(" ");
}
rs.close();
pstmt.close();
con.close();
} catch (SQLException ce) {
System.out.println(ce);}
return zz;
}
}
還有個(gè)數(shù)據(jù)庫,如果你能留個(gè)郵箱,我把整份程序傳給你~希望能幫到你
如果只是課設(shè)的話,你直接26個(gè)字母是26張表,
把用戶傳來的單詞取首字母,判斷屬于 哪張表
然后select * from tableA where value =‘用戶輸入的單詞’
把返回值處理顯示唄~~~~~。
這么做,因?yàn)楸肀容^少,會(huì)導(dǎo)致表中的單詞量比較大,select速度也是問題。
但是因?yàn)槭钦n設(shè),就不考慮那么多了
不然再根據(jù)第二個(gè)字母,把每張表分成兩張也可以 。
不涉及數(shù)據(jù)庫的話,就是把對(duì)應(yīng)的txt里的信息用IO流讀出來,存在map中,key是單詞,value是詞條信息,直接用map.get(“用戶單詞”);