已完善英語詞典詞典所有功能,酌情使用
Java連接并管理Access數(shù)據(jù)庫詞典
酌情參考
本單元的作業(yè),意在檢測(cè)學(xué)生是否達(dá)到以下學(xué)習(xí)目標(biāo):
(1)掌握J(rèn)DBC數(shù)據(jù)庫訪問的基本步驟;
(2)掌握利用JDBC建立數(shù)據(jù)庫連接的方法;
(3)掌握利用JDBC對(duì)數(shù)據(jù)庫進(jìn)行查詢的方法;
(4)掌握利用JDBC對(duì)數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行增、刪、]改的方法。
依照學(xué)術(shù)誠(chéng)信條款,我保證此回答為本人原創(chuàng),所有回答中引用的外部材料已經(jīng)做了出處標(biāo)記。
( 20分 )
用Microsoft Access作為DBMS,建立數(shù)據(jù)庫和數(shù)據(jù)表來實(shí)現(xiàn)對(duì)一個(gè)簡(jiǎn)易的英漢電子詞典進(jìn)行存儲(chǔ)和管理,這個(gè)簡(jiǎn)易電子詞典中的內(nèi)容至少包括:英文單詞名、詞性、漢語釋義、例句(其它信息可自行擴(kuò)充)。請(qǐng)編寫圖形界面的Java Application,完成對(duì)電子詞典中英文單詞的查找、新詞的添加、單詞的修改、刪除等功能。
酌情參考
一、功能列表
1.創(chuàng)建數(shù)據(jù)庫EnglishDictionary.accdb,添加單詞屬性
2.使用Java連接Access數(shù)據(jù)庫
--------數(shù)據(jù)庫的增刪改查功能--------
(1).打印全部單詞列表
(2).查詢單詞
(3).修改單詞(單詞、詞義、詞性、例句、例句翻譯)
(4).刪除單詞(通過單詞主鍵,刪除其所有屬性)
(6).退出系統(tǒng)(System.exit(0);
)
包含的成員方法有:
public class main()//主方法,程序入口
public void printMenu(){}//打印功能菜單
public void ConnectionAccess(){}//連接數(shù)據(jù)庫模塊
public void printList(){}//格式化輸出模塊
public void Switch(){}//功能選擇模塊
public void CheckWords(){}//查詢單個(gè)單詞屬性
public void PrintAllwords(){}//打印整個(gè)單詞列表
public void addWords(){}//添加單詞(單詞、詞義、詞性、例句、例句翻譯)
public void ChangeWord(){}//修改單詞屬性(單詞、詞義、詞性、例句、例句翻譯)
public void deleteWord(){}//刪除某個(gè)單詞(全部屬性)
可以根據(jù)自己需求另外增加功能模塊。
二、代碼塊
1.代碼
代碼如下:
import java.sql.*;
import java.util.Scanner;
public class Dictionary {
Connection con;
Statement stmt; //創(chuàng)建實(shí)例,要執(zhí)行SQL語句,必須獲取實(shí)例
ResultSet rs;
int res;
public static void main(String[] args) {//main()方法主體,盡量簡(jiǎn)潔
Dictionary dt = new Dictionary(); //創(chuàng)建對(duì)象
dt.Switch();//菜單嵌套在stitch開頭了,調(diào)用選擇語塊,會(huì)自動(dòng)打印菜單
}
/*打印的菜單語塊*/
public void printMenu() {
System.out.println("-------------------------------");
System.out.println("請(qǐng)輸入您要執(zhí)行的功能:");
System.out.println("1.打印數(shù)據(jù)庫中所有的單詞。");
System.out.println("2.查到某個(gè)單詞及其屬性。");
System.out.println("3.增加一個(gè)單詞及其屬性");
System.out.println("4.修改某個(gè)單詞的屬性(包括修改單詞、漢意、詞性、例句、例句翻譯)。");
System.out.println("5.刪除某個(gè)單詞(以及其屬性)");
System.out.println("6.退出系統(tǒng)");
}
/*加載驅(qū)動(dòng)連接數(shù)據(jù)庫語塊*/
public void ConnectionAccess(){
try{
//加載Access驅(qū)動(dòng)包
Class.forName("com.hxtt.sql.access.AccessDriver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
try {
//連接Access數(shù)據(jù)庫,jdbc:Access:///后面跟著你的數(shù)據(jù)庫路徑,可以仿造我下面的路徑格式
//Access的數(shù)據(jù)庫連接不需要賬號(hào)密碼,都為"",""不用填寫。
con = DriverManager.getConnection("jdbc:Access:///G:/JavaProjectFile/JavaJDBC/EnglishDictionary.accdb", "", "");
//連接并讀English表
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("select * from English");
System.out.println("已連接到數(shù)據(jù)庫的English表!");
}catch (Exception e){
e.printStackTrace();
}
}
/*格式化輸出語塊,減少代碼重復(fù)。復(fù)用的好處*/
public void printList(){
try {
while (rs.next()) {
String Words = rs.getString(1);
String ChineseMeaning = rs.getString(2);
String partSpeech = rs.getString(3);
String Example = rs.getString(4);
String ExampleMeaning = rs.getString(5);
//格式化輸出單詞的5個(gè)屬性。
//注意,這里不是println(),是print(輸出格式,輸出對(duì)象)
System.out.printf("%-10s", Words); //單詞
System.out.printf("%-30s", ChineseMeaning); //單詞漢意思
System.out.printf("%-12s\n", partSpeech); //單詞詞性
System.out.printf("%-60s\n", Example); //單詞例句
System.out.printf("%-30s\n\n", ExampleMeaning); //例句翻譯
//小提示,在這里一個(gè)單詞的信息已經(jīng)打印完成了,最后結(jié)尾那個(gè)屬性使用\n換行。
}
}catch (SQLException e){
throw new RuntimeException(e);
}
}
/*選擇語塊*/
public void Switch(){
Scanner sc=new Scanner(System.in);
while(true)
{
printMenu();//打印進(jìn)入系統(tǒng)時(shí)的菜單
int i=sc.nextInt();
switch (i) {
case 1 -> {
PrintAllwords();
System.out.println("按下Enter以繼續(xù)!");
//這里是停頓作用,等用戶按下回車再繼續(xù)循壞,直到選擇6
new Scanner(System.in).nextLine();
}
case 2 -> {
CheckWords();
System.out.println("按下Enter以繼續(xù)!");
//這里是停頓作用,等用戶按下回車再繼續(xù)循壞,直到選擇6
new Scanner(System.in).nextLine();
}
case 3 -> {
addWords();
System.out.println("按下Enter以繼續(xù)!");
//這里是停頓作用,等用戶按下回車再繼續(xù)循壞,直到選擇6
new Scanner(System.in).nextLine();
}
case 4 -> {
ChangeWord();
System.out.println("按下Enter以繼續(xù)!");
//這里是停頓作用,等用戶按下回車再繼續(xù)循壞,直到選擇6
new Scanner(System.in).nextLine();
}
case 5 -> {
deleteWord();
System.out.println("按下Enter以繼續(xù)!");
//這里是停頓作用,等用戶按下回車再繼續(xù)循壞,直到選擇6
new Scanner(System.in).nextLine();
}
case 6 -> {
try {
con.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
System.exit(0);
}
default -> System.out.println("請(qǐng)輸入正確的數(shù)字選項(xiàng)!");
}
}
}
/*查詢某個(gè)單詞語塊*/
public void CheckWords(){
ConnectionAccess();
System.out.println("請(qǐng)輸入要查詢的單詞(如果輸出為空則詞庫暫時(shí)沒有該單詞):");
Scanner sc=new Scanner(System.in);
String deptName=sc.nextLine();
try {
rs=stmt.executeQuery("select * from English where Words="+"'"+deptName+"'");
printList();
con.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
System.out.println("查詢完畢!如果輸出為空則列表還沒有該單詞qwq!");
}
//打印全部單詞信息方法
public void PrintAllwords(){
ConnectionAccess();
try{
rs.last();
int rows=rs.getRow();
System.out.println("English表共有"+rows+"條記錄");
//順序打印,需要再移動(dòng)到最前面來,將游標(biāo)移到第一行前
rs.beforeFirst();
System.out.println("順序輸出English表中的記錄:");
printList();
con.close();
}catch(
SQLException e){
e.printStackTrace();
}
}
public void addWords(){
String[] str=new String[5];str[0]="請(qǐng)輸入單詞:";str[1]="請(qǐng)輸入漢意:";str[2]="請(qǐng)輸入詞性:";str[3]="請(qǐng)輸入例句:";str[4]="請(qǐng)輸入例句翻譯:";
//先獲取用戶輸入的,單詞、漢意、詞性、例句、例句翻譯,再一次性插入數(shù)據(jù)表
String[] strings=new String[5];
for(int i=0;i<5;i++){
System.out.println(str[i]);
Scanner scanner=new Scanner(System.in);
strings[i]=scanner.nextLine();
}
ConnectionAccess();
try {
res= stmt.executeUpdate("insert into English values('"+strings[0]+"'"+","+"'"+strings[1]+"'"+","+"'"+strings[2]+"'"+","+"'"+strings[3]+"'"+","+"'"+strings[4]+"'"+")");
con.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
System.out.println("插入完畢!可通過功能2查詢?cè)搯卧~。");
}
/*修改單詞語塊, 可供修改單詞/漢意/詞性/例句/例句翻譯*/
public void ChangeWord(){
System.out.println("請(qǐng)輸入你要修改的單詞:");
Scanner scanner=new Scanner(System.in);
String word=scanner.nextLine();
System.out.println("請(qǐng)輸入要修改該單詞的什么屬性(1,單詞、2.漢意、3.詞性、4.例句、5.例句翻譯)");
int i=scanner.nextInt();
ConnectionAccess();
try{
switch (i) {
case 1 -> {
System.out.println("請(qǐng)輸入修改成什么單詞:");
Scanner scanner1=new Scanner(System.in);
String changeovers = scanner1.nextLine();
res = stmt.executeUpdate("update English set Words=" + "'" + changeovers + "'" + "where Words=" + "'" + word + "'");
if(res==1){
System.out.println("修改成功!請(qǐng)?jiān)诠δ?查看修改結(jié)果。");
}else System.out.println("刪除失敗,可能沒有該單詞!");
con.close();
}
case 2 -> {
System.out.println("請(qǐng)輸入修改成什么漢意:");
Scanner scanner1=new Scanner(System.in);
String changeovers = scanner1.nextLine();
res = stmt.executeUpdate("update English set ChineseM=" + "'" + changeovers + "'" + "where Words=" + "'" + word + "'");
if(res==1){
System.out.println("修改成功!請(qǐng)?jiān)诠δ?查看修改結(jié)果。");
}else System.out.println("刪除失敗,可能沒有該單詞!");
con.close();
}
case 3 ->{
System.out.println("請(qǐng)輸入修改成什么詞性:");
Scanner scanner1=new Scanner(System.in);
String changeovers = scanner1.nextLine();
res = stmt.executeUpdate("update English set PartSpeech=" + "'" + changeovers + "'" + "where Words=" + "'" + word + "'");
if(res==1){
System.out.println("修改成功!請(qǐng)?jiān)诠δ?查看修改結(jié)果。");
}else System.out.println("刪除失敗,可能沒有該單詞!");
con.close();
}
case 4 ->{
System.out.println("請(qǐng)輸入修改成什么例句:");
Scanner scanner1=new Scanner(System.in);
String changeovers = scanner1.nextLine();
res = stmt.executeUpdate("update English set Example=" + "'" + changeovers + "'" + "where Words=" + "'" + word + "'");
if(res==1){
System.out.println("修改成功!請(qǐng)?jiān)诠δ?查看修改結(jié)果。");
}else System.out.println("刪除失敗,可能沒有該單詞!");
con.close();
}
case 5 ->{
System.out.println("請(qǐng)輸入修改成什么例句翻譯:");
Scanner scanner1=new Scanner(System.in);
String changeovers = scanner1.nextLine();
res = stmt.executeUpdate("update English set ExampleM=" + "'" + changeovers + "'" + "where Words=" + "'" + word + "'");
if(res==1){
System.out.println("修改成功!請(qǐng)?jiān)诠δ?查看修改結(jié)果。");
}else System.out.println("刪除失敗,可能沒有該單詞!");
con.close();
}
default -> System.out.println("不要胡亂輸入功能數(shù)字呀??!");
}
}catch(SQLException e){
throw new RuntimeException();
}
}
//刪除單詞語塊
public void deleteWord(){
System.out.println("請(qǐng)輸入要?jiǎng)h除的單詞(請(qǐng)謹(jǐn)慎,將不可撤銷)!:");
Scanner scanner=new Scanner(System.in);
String str=scanner.nextLine();
ConnectionAccess();
try{
res=stmt.executeUpdate("delete from English where Words="+"'"+str+"'");
if(res==1){
System.out.println("刪除成功!");
}else System.out.println("刪除失敗,可能沒有該單詞!");
con.close();
}catch (SQLException e){
throw new RuntimeException();
}
}
}
運(yùn)行如圖:(已嵌套循壞語句,可一直不停的查詢,直到選擇6系統(tǒng)性退出)
Select(2)—>abandon
功能完善,下面的就不演示了,都沒問題的。
2.注意“食用”方法(不要直接粘貼就運(yùn)行了)
如果你直接粘貼代碼運(yùn)行的話,那我可以肯定的告訴你,一定會(huì)報(bào)錯(cuò)的QwQ,如果只是為了提交作業(yè)倒也不用自己修改。。。
1.數(shù)據(jù)庫的連接需要加載驅(qū)動(dòng)包。
2.修改代碼中的數(shù)據(jù)庫路徑為你存放EnglishDictionary的路徑
1.加載驅(qū)動(dòng)包
驅(qū)動(dòng)包就是這個(gè):這里推薦用JDBC30那個(gè)(有次數(shù)限制,40無限制)
我的是Java17,在C:\jdk-17.0.3.1_windows-x64_bin\jdk-17.0.3.1\lib
下
一般的Java應(yīng)該是在C:\Program Files\Java\jdk-11.0.13\
下
放lib
目錄或者bin
目錄在中
這里以IDEA
為例:外部庫這里可以看見驅(qū)動(dòng)包即調(diào)用了(即一些額外的包)
如果沒有識(shí)別到驅(qū)動(dòng)包需要右鍵手動(dòng)添加
2.修改EnglishDictionary文件路徑成你保存的文件路徑
然后把代碼中的這個(gè)路徑修改成你存放的路徑(代碼這一塊有提示)
//jdbc:Access:///后面跟著你的數(shù)據(jù)庫路徑,可以仿造我下面的路徑格式
//Access的數(shù)據(jù)庫連接不需要賬號(hào)密碼,都為"",""不用填寫。
con = DriverManager.getConnection("jdbc:Access:///G:/JavaProjectFile/JavaJDBC/EnglishDictionary.accdb", "", "");
如例///后面是文件路徑G:/JavaProjectFile/JavaJDBC/EnglishDictionary.accdb
以上是我的存放位置以及書寫路徑格式,改成你的即可,不要落下后綴名。
如果還有什么問題歡迎在評(píng)論區(qū)留言,快速回復(fù),精準(zhǔn)解答!文章來源:http://www.zghlxwxcb.cn/news/detail-441067.html
總結(jié)
最近時(shí)間比較緊,暫時(shí)不能更新出GUI界面了,只能在軟件里面實(shí)現(xiàn)英語詞典數(shù)據(jù)庫的增刪改查功能,等時(shí)間充裕再補(bǔ)上結(jié)合GUI界面的代碼!
如果對(duì)您有幫助,希望得到一個(gè)免費(fèi)的贊??!
如果還有什么問題歡迎在評(píng)論區(qū)留言,快速回復(fù),精準(zhǔn)解答!文章來源地址http://www.zghlxwxcb.cn/news/detail-441067.html
到了這里,關(guān)于用Microsoft Access作為DBMS,建立數(shù)據(jù)庫和數(shù)據(jù)表來實(shí)現(xiàn)對(duì)一個(gè)簡(jiǎn)易的英漢電子詞典進(jìn)行存儲(chǔ)和管理,這個(gè)簡(jiǎn)易電子詞典中的內(nèi)容至少包括:英文單詞名、詞性、漢語釋義的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!