第一篇:2008081042實(shí)驗(yàn)四 多線(xiàn)程
廣州中醫(yī)藥大學(xué)信息技術(shù)學(xué)院
課程名稱(chēng):專(zhuān)業(yè)班級(jí):學(xué)生學(xué)號(hào):學(xué)生姓名:實(shí)驗(yàn)名稱(chēng):實(shí)驗(yàn)成績(jī):課程類(lèi)別:
驗(yàn) 報(bào) 告
Java程序設(shè)計(jì)
計(jì)算機(jī)科學(xué)與技術(shù)2008級(jí)
2008081042 王湛澤
JAVA程序設(shè)計(jì)
必修□限選? 公選□ 其它□
實(shí)
實(shí)驗(yàn)四
多線(xiàn)程
[實(shí)驗(yàn)?zāi)康腯 1.練習(xí)線(xiàn)程的使用以深入理解線(xiàn)程狀態(tài)與生命周期。2.了解線(xiàn)程調(diào)度機(jī)制、理解線(xiàn)程同步機(jī)制。
[實(shí)驗(yàn)內(nèi)容] 1.編寫(xiě)一個(gè)多線(xiàn)程的Java應(yīng)用程序,經(jīng)歷線(xiàn)程不同的狀態(tài)與生命周期。
2.編寫(xiě)一個(gè)多線(xiàn)程的Java應(yīng)用程序,在程序中進(jìn)行線(xiàn)程同步的處理。[實(shí)驗(yàn)步驟與要求] 第1題 線(xiàn)程的狀態(tài)
編寫(xiě)一個(gè)Java應(yīng)用程序,在主線(xiàn)程中再創(chuàng)建2個(gè)線(xiàn)程,要求線(xiàn)程經(jīng)歷4種狀態(tài):新建、運(yùn)行、中斷和死亡 第2題 排隊(duì)買(mǎi)票
編寫(xiě)一個(gè)Java應(yīng)用程序,模擬5個(gè)人排隊(duì)買(mǎi)票。售票員只有1張五元的錢(qián),電影票五元一張。假設(shè)5個(gè)人的名字及排隊(duì)順序:趙、錢(qián)、孫、李、周?!摆w”拿一張二十元的人民幣買(mǎi)2張票,“錢(qián)”拿1張二十元的人民幣1張票,“孫”拿1張十元的人民幣買(mǎi)1張票,“李”拿1張十元的人民幣買(mǎi)2張票,“周”拿1張五元的人民幣買(mǎi)1張票,要求售票員按如下規(guī)則找贖:
二十元買(mǎi)2張票,找零:找1張十元;不許找2張五元 二十元買(mǎi)1張票,找零:找1張十元,1張五元;不許找3張五元
十元買(mǎi)1張票,找零:找1張五元 [作業(yè)提交] 第一題:
將代碼貼在下面:
public class Example8_8{ public static void main(String args[]){ ClassRoom room=new ClassRoom();room.zhangHua.start();
room.teacher.start();} } class ClassRoom implements Runnable{ Thread zhangHua,teacher;ClassRoom(){ teacher=new Thread(this);zhangHua=new Thread(this);zhangHua.setName(“張華”);
teacher.setName(“劉老師”);} public void run(){ Thread thread=Thread.currentThread();if(thread==zhangHua){ try{ System.out.println(thread.getName()+“休息10秒后再說(shuō)問(wèn)候”);Thread.sleep(10000);} catch(InterruptedException e){ System.out.println(thread.getName()+“被吵醒了”);} System.out.println(thread.getName()+“說(shuō):早上好!”);} else if(thread==teacher){ for(int i=1;i<=2;i++){ System.out.println(thread.getName()+“說(shuō):t上課!”);try{ Thread.sleep(500);} 3 catch(InterruptedException e){} }
zhangHua.interrupt();//吵醒zhangXiao } } } 將結(jié)果運(yùn)行截屏貼在下面:
第二題: 代碼:
public class Example8_10{ public static void main(String args[]){ String s1=“張”,s2=“錢(qián)”,s3=“孫”,s4=“李”,s5=“周”;Cinema canema=new Cinema(s1,s2,s3,s4,s5);Thread zhang,qian,sun,li,zhou;zhang=new Thread(canema);qian=new Thread(canema);sun=new Thread(canema);li=new Thread(canema);zhou=new Thread(canema);zhang.setName(s1);qian.setName(s2);4 sun.setName(s3);li.setName(s4);zhou.setName(s5);zhang.start();qian.start();sun.start();li.start();zhou.start();} } class Cinema implements Runnable{ //實(shí)現(xiàn)Runnable接口的類(lèi)(電影院)TicketSeller seller;//電影院的售票員
String name1,name2,name3,name4,name5;//買(mǎi)票人的名字(線(xiàn)程的名字)Cinema(String s1,String s2,String s3,String s4,String s5){ seller=new TicketSeller();name1=s1;name2=s2;name3=s3;name4=s4;name5=s5;} public void run(){ if(Thread.currentThread().getName().equals(name1)){ seller.sellTicket(20);} else if(Thread.currentThread().getName().equals(name2)){ seller.sellTicket(20);} else if(Thread.currentThread().getName().equals(name3)){ seller.sellTicket(10);} else if(Thread.currentThread().getName().equals(name4)){ seller.sellTicket(10);} else if(Thread.currentThread().getName().equals(name5)){ seller.sellTicket(5);} } } class TicketSeller{ //負(fù)責(zé)賣(mài)票的類(lèi)
int fiveNumber=1,tenNumber=0,twentyNumber=0;5 public synchronized void sellTicket(int receiveMoney){ String s=Thread.currentThread().getName();if(receiveMoney==5){ fiveNumber=fiveNumber+1;System.out.println(s+“給售票員5元錢(qián),售票員賣(mài)給”+s+“一張票,不必找贖”);} else if(receiveMoney==10&&s==“李”){
tenNumber=tenNumber+1;
System.out.println(s+“給售票員10元錢(qián),售票員賣(mài)給”+s+“兩張票,不必找贖”);
} else if(receiveMoney==10&&s==“孫”){
while(fiveNumber<1){ try{ System.out.println(s+“給售票員10元錢(qián)”);System.out.println(“售票員請(qǐng)”+s+“靠邊等一會(huì)”);wait();//如果線(xiàn)程占有CPU期間執(zhí)行了wait(),就進(jìn)入中斷狀態(tài) System.out.println(s+“結(jié)束等待,繼續(xù)買(mǎi)票”);} catch(InterruptedException e){} } fiveNumber=fiveNumber-1;tenNumber=tenNumber+1;System.out.println(s+“給售票員10元錢(qián),售票員賣(mài)給”+s+“一張票,找贖5元”);} else if(receiveMoney==20&&s==“錢(qián)”){
while(fiveNumber<1||tenNumber<1){ try{ System.out.println(s+“給售票員20元錢(qián)”);System.out.println(“售票員請(qǐng)”+s+“靠邊等一會(huì)”);wait();//如果線(xiàn)程占有CPU期間執(zhí)行了wait(),就進(jìn)入中斷狀態(tài) System.out.println(s+“結(jié)束等待,繼續(xù)買(mǎi)票”);} catch(InterruptedException e){} } fiveNumber=fiveNumber-1;tenNumber=tenNumber-1;twentyNumber=twentyNumber+1;System.out.println(s+“給售票員20元錢(qián),售票員賣(mài)給”+s+“一張票,找贖15 6 元”);} else if(receiveMoney==20&&s==“趙”){
while(tenNumber<1){ try{ System.out.println(s+“給售票員20元錢(qián)”);System.out.println(“售票員請(qǐng)”+s+“靠邊等一會(huì)”);wait();//如果線(xiàn)程占有CPU期間執(zhí)行了wait(),就進(jìn)入中斷狀態(tài) System.out.println(s+“結(jié)束等待,繼續(xù)買(mǎi)票”);} catch(InterruptedException e){} }
tenNumber=tenNumber-1;twentyNumber=twentyNumber+1;System.out.println(s+“給售票員20元錢(qián),售票員賣(mài)給”+s+“兩張票,找贖15元”);}
截屏:
(作業(yè)提交說(shuō)明:實(shí)驗(yàn)完成后,將此文檔和相關(guān)的程序源程序代碼一并壓縮后提交上來(lái),文件名為自己的學(xué)號(hào)+實(shí)驗(yàn)四,如2008000001+實(shí)驗(yàn)四.RAR)
第二篇:多線(xiàn)程實(shí)驗(yàn)報(bào)告
寧波工程學(xué)院電信學(xué)院計(jì)算機(jī)教研室
實(shí)驗(yàn)報(bào)告
課程名稱(chēng): Java 2 姓 名: *** 實(shí)驗(yàn)項(xiàng)目: 多線(xiàn)程實(shí)驗(yàn) 學(xué) 號(hào): **** 指導(dǎo)教師: **** 班 級(jí): **** 實(shí)驗(yàn)位置: 電信樓機(jī)房 日 期:
一、實(shí)驗(yàn)?zāi)康?/p>
1、掌握多線(xiàn)程編程的特點(diǎn)和工作原理;
2、掌握編寫(xiě)線(xiàn)程程序的方法
3、了解線(xiàn)程的調(diào)度和執(zhí)行過(guò)程
4、掌握線(xiàn)程同步機(jī)理
二、實(shí)驗(yàn)環(huán)境
windows記事本,java jdk 1.60版本,cmd命令運(yùn)行窗口
三、實(shí)驗(yàn)內(nèi)容 實(shí)驗(yàn)一:
應(yīng)用Java中線(xiàn)程的概念寫(xiě)一個(gè)Java程序(包括一個(gè)測(cè)試線(xiàn)程程序類(lèi)TestThread,一個(gè)Thread類(lèi)的子類(lèi)PrintThread)。在測(cè)試程序中用子類(lèi)PrintThread創(chuàng)建2個(gè)線(xiàn)程,使得其中一個(gè)線(xiàn)程運(yùn)行時(shí)打印10次“線(xiàn)程1正在運(yùn)行”,另一個(gè)線(xiàn)程運(yùn)行時(shí)打印5次“線(xiàn)程2正在運(yùn)行
源程序:
public class A { public static void main(String args[]){
Test1 A1;
Test2 A2;
A1=new Test1();
A2=new Test2();
A1.start();
A2.start();} } class PrintThread extends Thread { } class Test1 extends PrintThread { public void run(){
for(int i=1;i<=10;i++)
{
System.out.println(“線(xiàn)程1正在運(yùn)行!”);
} } } class Test2 extends PrintThread { public void run(){
for(int i=1;i<=5;i++)
{
System.out.println(“線(xiàn)程2正在運(yùn)行!”);
} } } 運(yùn)行結(jié)果:
實(shí)驗(yàn)二:
將上述程序用Runnable接口改寫(xiě),并上機(jī)驗(yàn)證源程序 public class D { public static void main(String args[]){
Move move=new Move();
move.test1.start();
move.test2.start();} } class Move implements Runnable { Thread test1,test2;Move(){
test1=new Thread(this);
test1.setName(“線(xiàn)程1正在運(yùn)行!”);
test2=new Thread(this);
test2.setName(“線(xiàn)程2正在運(yùn)行!”);} public void run(){
if(Thread.currentThread()==test1)
{
for(int i=1;i<=10;i++)
{
System.out.println(test1.getName());
} } } else { for(int i=1;i<=5;i++){
System.out.println(test2.getName());} } 運(yùn)行結(jié)果:
實(shí)驗(yàn)三:
import java.awt.*;import java.awt.event.*;public class E
{ public static void main(String args[])
{ new FrameMoney();
} } class FrameMoney extends Frame implements Runnable,ActionListener { int money=100;
TextArea text1,text2;
Thread 會(huì)計(jì),出納;
int weekDay;
Button start=new Button(“開(kāi)始演示”);
FrameMoney()
{ 會(huì)計(jì)=new Thread(this);
出納=new Thread(this);
text1=new TextArea(12,15);
text2=new TextArea(12,15);
setLayout(new FlowLayout());
add(start);
add(text1);
add(text2);
setVisible(true);
setSize(360,300);
validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
start.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{ if(!(出納.isAlive()))
{ 會(huì)計(jì)=new Thread(this);
出納=new Thread(this);
}
try
{ 會(huì)計(jì).start();
出納.start();
}
catch(Exception exp){}
}
public synchronized void 存取(int number)//存取方法
{ if(Thread.currentThread()==會(huì)計(jì))
{ text1.append(“今天是星期”+weekDay+“n”);
for(int i=1;i<=3;i++)//會(huì)計(jì)使用存取方法存入90元,存入30元,稍歇一下
{ money=money+number;
//這時(shí)出納仍不能使用存取方法
try { Thread.sleep(1000);//因?yàn)闀?huì)計(jì)還沒(méi)使用完存取方法
}
catch(InterruptedException e){}
text1.append(“帳上有”+money+“萬(wàn)n”);
}
}
else if(Thread.currentThread()==出納)
{ text2.append(“今天是星期 ”+weekDay+“n”);
for(int i=1;i<=2;i++)//出納使用存取方法取出30元,取出15元,稍歇一下
{ money=money-number/2;
//這時(shí)會(huì)計(jì)仍不能使用存取方法
try { Thread.sleep(1000);//因?yàn)槌黾{還沒(méi)使用完存取方法
}
catch(InterruptedException e){}
text2.append(“帳上有”+money+“萬(wàn)n”);
}
}
}
public void run()
{ if(Thread.currentThread()==會(huì)計(jì)||Thread.currentThread()==出納)
{ for(int i=1;i<=3;i++)//從周一到周三會(huì)計(jì)和出納都要使用帳本
{ weekDay=i;
存取(30);
}
}
} }
運(yùn)行結(jié)果:
}
四、實(shí)驗(yàn)心得與小結(jié)
通過(guò)本次實(shí)驗(yàn),基本了解了線(xiàn)程的概念,作用,方法以及使用規(guī)則。1.首先:java 程序是建立在線(xiàn)程之上的。.2.創(chuàng)建線(xiàn)程必須繼承 Thread class 它已經(jīng)為線(xiàn)程的創(chuàng)建和運(yùn)行做了必要的配置。run是線(xiàn)程就重要的方法。你必須覆寫(xiě)這個(gè)方法達(dá)到你想要的目的。3.run方法所包含的代碼就是和其他線(xiàn)程同時(shí)運(yùn)行的代碼以達(dá)到同一時(shí)刻運(yùn)行多段代碼的目的。當(dāng)終止了 run以后。這個(gè)線(xiàn)程也就結(jié)束了。調(diào)用線(xiàn)程的 start方法才會(huì)執(zhí)行 run方法。
4.線(xiàn)程的生命周期:新建——Thread.State.NEW:當(dāng)一個(gè) Thread 類(lèi)或者其子類(lèi)的對(duì)象被聲明并創(chuàng)建時(shí),新的線(xiàn)程對(duì)象處于新建狀態(tài),此時(shí)它已經(jīng)有了相應(yīng)的內(nèi)存空間和其他資源start方法尚未被調(diào)整用就緒可執(zhí)行狀態(tài)——Thread.State.RUNNABLE:處于新建狀態(tài)的線(xiàn)程被啟動(dòng)后,將進(jìn)入線(xiàn)程隊(duì)列排隊(duì),這個(gè)時(shí)候具備了運(yùn)行的條件,一旦輪到 CPU 的時(shí)候,就可以脫離創(chuàng)建它的主線(xiàn)程獨(dú)立開(kāi)始自己的生命周期運(yùn)行:就緒的線(xiàn)程被調(diào)度進(jìn)入運(yùn)行狀態(tài),每一個(gè) Thread 類(lèi)及其子類(lèi)的對(duì)象都有一個(gè)重要的run方法,當(dāng)線(xiàn)程對(duì)象被調(diào)度執(zhí)行的時(shí)候,它將自動(dòng)調(diào)用本對(duì)象的 run方法,從第一句代碼開(kāi)始執(zhí)行。
第三篇:實(shí)驗(yàn)四
電 子 科 技 大 學(xué)
實(shí)
驗(yàn)
報(bào)
告
學(xué)生姓名:
學(xué) 號(hào):
指導(dǎo)教師: 實(shí)驗(yàn)地點(diǎn):
實(shí)驗(yàn)時(shí)間:
一、實(shí)驗(yàn)室名稱(chēng):
Linux環(huán)境高級(jí)編程實(shí)驗(yàn)室
二、實(shí)驗(yàn)項(xiàng)目名稱(chēng):
插件框架實(shí)驗(yàn)
三、實(shí)驗(yàn)學(xué)時(shí):
4學(xué)時(shí)
四、實(shí)驗(yàn)?zāi)康模?/p>
需要說(shuō)明為什么要進(jìn)行本次實(shí)驗(yàn)
五、實(shí)驗(yàn)內(nèi)容:
PPT上的4個(gè)版本程序,以及綜合練習(xí)
六、實(shí)驗(yàn)步驟:
PPT上的4個(gè)版本程序,以及綜合練習(xí)
七、總結(jié)及心得體會(huì):
八、對(duì)本實(shí)驗(yàn)過(guò)程及方法、手段的改進(jìn)建議:
報(bào)告評(píng)分:
指導(dǎo)教師簽字:
第四篇:實(shí)驗(yàn)四
實(shí)習(xí)四 圖書(shū)館利用基礎(chǔ)及中文全文數(shù)據(jù)庫(kù)
實(shí)習(xí)目的:
一、通過(guò)實(shí)習(xí),了解館藏書(shū)目數(shù)據(jù)庫(kù)的基本原理和常用檢索途徑,熟練掌握查詢(xún)本館、相關(guān)高校及科研院所圖書(shū)館檢索書(shū)刊信息的方法;樹(shù)立信息資源共享意識(shí),重點(diǎn)了解國(guó)內(nèi)學(xué)術(shù)資源分布情況,掌握外文期刊聯(lián)合目錄的使用方法,提升獨(dú)立獲取外文期刊原文的信息能力。
二、了解國(guó)內(nèi)中文全文數(shù)據(jù)庫(kù)的收錄特點(diǎn)及檢索功能,包括電子期刊和電子圖書(shū)全文數(shù)據(jù)庫(kù),重點(diǎn)掌握清華同方的“中文期刊全文數(shù)據(jù)庫(kù)”的使用方法;了解重慶維普的“中文科技期刊數(shù)據(jù)庫(kù)(全文版)”和萬(wàn)方數(shù)據(jù)資源系統(tǒng)的“數(shù)字化期刊”等全文數(shù)據(jù)庫(kù)的收錄范圍和使用方法;了解超星數(shù)字圖書(shū)館等目前國(guó)內(nèi)較常見(jiàn)的電子書(shū)刊資源及其常用檢索途徑和方法。實(shí)習(xí)題:
一、圖書(shū)館利用基礎(chǔ)
1、查找廈門(mén)理工學(xué)院圖書(shū)館(http://lib.xmut.edu.cn/index.asp)是否收藏商業(yè)模式方面的叢書(shū),若有,請(qǐng)記錄你感興趣的其中一本的書(shū)名、編者、出版地、出版社、出版年、分類(lèi)號(hào)、收藏單位、索取號(hào)以及出借狀態(tài)等書(shū)目信息。
2、廈門(mén)理工學(xué)院圖書(shū)館是否收藏外文的中國(guó)軍事百科全書(shū)?若有,請(qǐng)問(wèn)目前收藏有多少分冊(cè)?可以在廈門(mén)理工學(xué)院圖書(shū)館幾樓的哪個(gè)書(shū)庫(kù)獲取呢?
3、請(qǐng)查找與你所學(xué)專(zhuān)業(yè)相關(guān)的一種期刊,中外文均可,并請(qǐng)記錄刊名、有無(wú)曾用名、出版地、創(chuàng)刊年、分類(lèi)號(hào)、收藏單位等書(shū)目信息。
4、利用搜索引擎查找并登錄以下網(wǎng)站,試將每個(gè)網(wǎng)站的主頁(yè)加入“收藏夾”中,以便調(diào)用。
(1)登錄“廈門(mén)理工學(xué)院圖書(shū)館”主頁(yè),瀏覽其館藏書(shū)目查詢(xún)功能頁(yè)面,并自命題查找與你專(zhuān)業(yè)密切相關(guān)的圖書(shū)或期刊;
(2)登錄并瀏覽“中國(guó)高等教育數(shù)字圖書(shū)館(CALIS/eduChina)”主頁(yè),從主頁(yè)的“查找全國(guó)高校圖書(shū)館資料”欄目練習(xí)檢索有關(guān)的圖書(shū)或期刊;
(3)登錄并瀏覽“國(guó)家科技圖書(shū)文獻(xiàn)中心(NSTL)”主頁(yè),自命題練習(xí)檢索相關(guān)的圖書(shū)或期刊,并嘗試注冊(cè)新用戶(hù)和密碼,模擬外文原文訂購(gòu)過(guò)程。
二、中文期刊全文數(shù)據(jù)庫(kù)
(1)通過(guò)校園網(wǎng)進(jìn)入“萬(wàn)方數(shù)據(jù)資源”的主頁(yè),可以按照“學(xué)術(shù)期刊”的學(xué)科分類(lèi)或者論文檢索途徑,找出一種與你所學(xué)專(zhuān)業(yè)密切相關(guān)的期刊,請(qǐng)嘗試查閱最新一期刊載的論文全文內(nèi)容。
(2)通過(guò)校園網(wǎng)進(jìn)入“維普資訊”的主頁(yè),練習(xí)通過(guò)“快速檢索”、“高級(jí)檢索”、“分類(lèi)檢索”、“期刊導(dǎo)航”等途徑查找自己感興趣的學(xué)術(shù)論文,并瀏覽文獻(xiàn)題錄及全文內(nèi)容(練習(xí)題目可自選,或參考前面題目)。在使用過(guò)程中,請(qǐng)思考以上三個(gè)資源站點(diǎn)之間有何異同。
(3)通過(guò)校園網(wǎng)分別登錄“超星數(shù)字圖書(shū)館”、“讀秀學(xué)術(shù)搜索”或“書(shū)生之家”等電子圖書(shū)閱讀網(wǎng),瀏覽各網(wǎng)站的欄目信息,嘗試查找和閱讀相關(guān)專(zhuān)業(yè)的圖書(shū)全文。
【實(shí)驗(yàn)報(bào)告提交】
1、作業(yè)以WORD格式完成,在同一個(gè)文檔中無(wú)需分開(kāi),標(biāo)明題目即可??砂l(fā)送到郵箱jihuish@126.com(郵件標(biāo)題請(qǐng)注明學(xué)號(hào)和姓名),或者提交打印稿均可。
2、作業(yè)須由自己完成,如發(fā)現(xiàn)有copy行為,取消實(shí)驗(yàn)成績(jī);
3、本次實(shí)驗(yàn)成績(jī)記入平時(shí)成績(jī)的10%。
第五篇:實(shí)驗(yàn)四總結(jié)報(bào)告
《數(shù)據(jù)庫(kù)原理與應(yīng)用》實(shí)驗(yàn)報(bào)告
實(shí)驗(yàn)名稱(chēng): 實(shí)驗(yàn)四
學(xué)號(hào): 班級(jí):
姓名: 軟件工程
一、實(shí)驗(yàn)?zāi)康?/p>
(1)了解Oracle數(shù)據(jù)庫(kù)中的用戶(hù)管理,模式,權(quán)限管理和角色管理。
(2)掌握為用戶(hù)分配權(quán)限的方法。
(3)了解為不同用戶(hù)分配不同權(quán)限的目的及原因。
二、實(shí)驗(yàn)過(guò)程
1.用系統(tǒng)帳戶(hù)sys登錄數(shù)據(jù)庫(kù),分別創(chuàng)建數(shù)據(jù)庫(kù)內(nèi)部用戶(hù)user_one和user_two,創(chuàng)建時(shí)自己為用戶(hù)分配帳戶(hù)口令。
create user user_one
identified by 980916
default tablespace users
temporary tablespace temp
quota unlimited on users;create user user_two
identified by 980916
default tablespace users
temporary tablespace temp
quota unlimited on users;/ 14
2.為了使兩位用戶(hù)登錄數(shù)據(jù)庫(kù)請(qǐng)為其授予相應(yīng)的權(quán)限。
grant create session to user_one,user_two;
3.授予用戶(hù)user_one在自己模式下創(chuàng)建表的權(quán)限,在任何模式下刪除表的權(quán)限,授予用戶(hù)user_two可以在任何模式下創(chuàng)建表的權(quán)限,查詢(xún)?nèi)魏文J较卤碇袛?shù)據(jù)的權(quán)限和在任何模式下創(chuàng)建視圖的權(quán)限。
grant create table,drop any table to user_one;grant create any table,select any table,create any view to user_two;
/ 14 4.分別用user_one和user_two登錄,寫(xiě)出相應(yīng)的SQL語(yǔ)句驗(yàn)證為其授予的權(quán)限。(如果建立的表中有主鍵約束,需要預(yù)先授予user_one和user_two用戶(hù)create any index的權(quán)限。)
grant create any index to user_one,user_two;在user_one中建表A create table a(x number,y date);
在user_two中建表B create table b(x number,y date);
在user_two中查詢(xún)表A select * from user_one.a;3 / 14
從user_one中刪除表B drop table user_two.b;在user_two中查詢(xún)表B Select * fromb;
在user_two中建立視圖VIEW_A create view view_a(x,y)
as select x,y
from b;/ 14
5.用系統(tǒng)帳戶(hù)sys登錄數(shù)據(jù)庫(kù),創(chuàng)建用戶(hù)user_three,將角色權(quán)限D(zhuǎn)BA授予用戶(hù)user_three,并將S、P、J、SPJ四張表導(dǎo)入到user_three模式下。
create user user_three
identified by 980916
default tablespace users
temporary tablespace temp
quota unlimited on users;grant dba to user_three;/ 14
6.使用user_three登錄,完成如下授權(quán),在user_one和user_two用戶(hù)下執(zhí)行相應(yīng)的SQL語(yǔ)句驗(yàn)證授權(quán)是否成功。
(1)把對(duì)表S的INSERT權(quán)力授予用戶(hù)user_one,并允許他再將此權(quán)限授予其他用戶(hù)。
grant insert on s to user_one with grant option;
在user_one中插入數(shù)據(jù)
insert into user_three.s(sno,sname,city,sphone)
values('1','a','湖北',null);
commit;
grant insert on user_three.s to user_two;在user_two中插入數(shù)據(jù)
insert into user_three.s(sno,sname,city,sphone)6 / 14
values('2','b','湖北',null);
commit;
(2)用戶(hù)user_two對(duì)S,P,J三個(gè)表有SELECT和INSERT權(quán)力
grant select,insert on s to user_two;grant select,insert on p to user_two;grant select,insert on j to user_two;
從user_two中查詢(xún)表S select * from user_three.s;/ 14
從user_two中給表S插入數(shù)據(jù)
insert into user_three.s(sno,sname,city,sphone)
values('3','c','湖北',null);
commit;
(3)用戶(hù)user_one對(duì)SPJ表有DELETE權(quán)力,對(duì)QTY字段具有UPDATE權(quán)力。
grant delete,update(qty)on spj to user_one;
在user_one中刪除sno為s1的數(shù)據(jù)
delete from user_three.spj where sno='S1';
commit;/ 14
在user_one中將sno為s2的數(shù)據(jù)的qty改為0 update user_three.spj set qty=0 where sno='S2';
commit;
(4)收回user_one對(duì)S表的插入權(quán)限。
revoke insert on s from user_one;
嘗試在user_one中插入數(shù)據(jù)
insert into user_three.s(sno,sname,city,sphone)
values('3','d','湖北',null);
commit;9 / 14
7.把對(duì)用戶(hù)user_two授予的所有權(quán)限收回,只保留登錄權(quán)限。(系統(tǒng)權(quán)限和對(duì)象權(quán)限應(yīng)該分別收回)
revoke select,insert on s from user_two;revoke select,insert on p from user_two;revoke select,insert on j from user_two;revoke create any index,create any table,create any view,select any table from user_two;
嘗試在user_two中創(chuàng)建表C create table c(x number,y date);10 / 14
8.用系統(tǒng)帳戶(hù)sys登錄數(shù)據(jù)庫(kù),創(chuàng)建用戶(hù)user_four,將角色權(quán)限D(zhuǎn)BA授予此用戶(hù),在user_four的模式下導(dǎo)入Sudent、Course和SC表。
create user user_four
identified by 980916
default tablespace users
temporary tablespace temp
quota unlimited on users;grant dba to user_four;/ 14
9.使用user_four登錄,創(chuàng)建角色STUDBA,將修改Student、Course、SC表結(jié)構(gòu)的權(quán)限,插入、刪除、修改和查詢(xún)?nèi)龔埍碇袛?shù)據(jù)的權(quán)限授予角色STUDBA,將角色的權(quán)限授予user_one和user_two。
create role studba;grant alter,insert,delete,update,select on s to studba;grant alter,insert,delete,update,select on c to studba;grant alter,insert,delete,update,select on sc to studba;grant studba to user_one,user_two;commit;/ 14
10.對(duì)于通過(guò)STUDBA角色授予的權(quán)限,在user_one和user_two用戶(hù)下執(zhí)行相應(yīng)的SQL語(yǔ)句對(duì)權(quán)限進(jìn)行驗(yàn)證。
修改表權(quán)限驗(yàn)證
alter table user_four.c add collage varchar2(40);commit;select * from user_four.c;
更新數(shù)據(jù)驗(yàn)證
update user_four.s set major='軟件工程' where major='植物保護(hù)';select * from user_four.s where major='植物保護(hù)';
插入數(shù)據(jù)驗(yàn)證 / 14
insert into user_four.sc(sno,cno,grade)values(103000,300,100);commit;select * from user_four.sc where sno=103000 and cno=300;
刪除數(shù)據(jù)驗(yàn)證
delete from user_four.sc where sno=103000 and cno=300;commit;select * from user_four.sc where sno=103000 and cno=300;
三、實(shí)驗(yàn)總結(jié) / 14