版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
./實驗由本人當時親自完成僅供參考,希望可以幫助大家本科實驗報告課程名稱:Java程序設(shè)計B實驗項目:Java語言基礎(chǔ)編程和面向?qū)ο笤O(shè)計實驗地點:逸夫樓501專業(yè)班級:0000學號:00000000學生:氣宇軒昂指導教師:20XX13月13日實驗名稱實驗一Java環(huán)境搭建和Java語言基礎(chǔ)編程實驗?zāi)康暮鸵笳莆認DK、Eclipse的下載和安裝,并熟練使用API幫助文檔;學會JavaApplication程序和JavaApplet程序的編制、編譯和運行;熟練掌握Java程序流程控制語句的使用;熟練掌握Java語言數(shù)組的使用、字符串以及字符串處理方法的使用。實驗容下載并安裝最新版本的JDK和Eclipse,設(shè)置環(huán)境變量,下載API幫助文檔。設(shè)計JavaApplication程序,輸出"我叫×××,我編寫的JavaApplication程序運行通過了",分別在命令提示符和Eclipse環(huán)境中編譯、運行。完成習題2.7的編程、調(diào)試與運行。使用for循環(huán)語句輸出習題3.5中的圖案。完成習題3.9的編程、調(diào)試與運行。完成習題4.5和4.7的編程、調(diào)試與運行。主要儀器設(shè)備臺式或筆記本計算機實驗記錄<寫出實驗容中3,5,6的程序代碼和運行結(jié)果><可分欄或加頁>2.7程序代碼importnner;publicclassDataType{publicstaticvoidmain<String[]args> { Scannerin=newScanner<System.in>; System.out.print<"請輸入一個long型數(shù)據(jù)〔輸完按enter結(jié)束:">;longa=in.nextLong<>; System.out.print<"請輸入一個float型數(shù)據(jù)〔輸完按enter結(jié)束:">;floatb=in.nextFloat<>; System.out.print<"請輸入一個double型數(shù)據(jù)〔輸完按enter結(jié)束:">;doublec=in.nextDouble<>; System.out.println<"long:"+a>; System.out.println<"float:"+b>; System.out.println<"double:"+c>; }}運行結(jié)果3.9程序代碼importjava.util.Scanner;publicclassValueFore{intn; ValueFore<> {this.n=calculateN<>; }doublestackBy<intn> {inti;doubles=1;for<i=1;i<=n;i++> { s*=i; }returns; }doubleoverlap<> {inti;doublesum=1;for<i=1;i<=this.n;i++> { sum+=1/stackBy<i>; }returnsum; }intcalculateN<> { Scannerin=newScanner<System.in>;inti=1;doublea=in.nextDouble<>;while<1/stackBy<i>>a> { i++; }returni; }publicstaticvoidmain<String[]args> { Scannerin=newScanner<System.in>;booleanb=true;while<b> { System.out.println<"請選擇菜單:">; System.out.println<"1.計算不同精度的e值">; System.out.println<"2.退出">;switch<in.nextByte<>> {case1: { System.out.println<"請輸入精度〔例如:1e-3,表示0.001:">; ValueForea=newValueFore<>; System.out.println<"e="+a.overlap<>>; };break;case2:b=false;break; } } }}運行結(jié)果說明:沒有按書中的要求去求,但是這個程序功能更強,運用了菜單的方式,使用程序的人能求出不同精度的e值。4.5程序代碼publicclassStringText{publicstaticvoidmain<String[]args> { Stringstr1="Iam"; Stringstr2="astudent"; Stringstr3="我是中國人"; StringBufferstr=newStringBuffer<>; str.append<str1>.append<str2>.append<str3>; System.out.println<str>; System.out.println<str.charAt<8>>; System.out.println<str.charAt<16>>; }}運行結(jié)果4.7程序代碼importjava.util.Scanner;publicclassTriangle{voidprintTriangle<introw> {inti,j;int[][]a=newint[row][];for<i=0;i<row;i++> a[i]=newint[i+1];for<i=0;i<row;i++> { a[i][0]=1; a[i][i]=1; }for<i=2;i<row;i++> {for<j=1;j<i;j++> { a[i][j]=a[i-1][j-1]+a[i-1][j]; } } System.out.println<"輸出輝三角為:">;for<i=0;i<row;i++> {for<j=row-1;j>i;j--> System.out.print<"\t">;for<j=0;j<=i;j++> { System.out.print<a[i][j]>;if<i!=0> System.out.print<"\t\t">; } System.out.print<"\n">; } }publicstaticvoidmain<String[]args> { Scannerin=newScanner<System.in>; Trianglenum=newTriangle<>; System.out.print<"請輸入輝三角的行數(shù):">; num.printTriangle<in.nextByte<>>; }}運行結(jié)果說明:這個程序同樣也具有一定的靈活行,隨意輸出輝三角的行數(shù)就就可以得到不同的輝三角,比如輸入6,得到如下結(jié)果:遇到的問題和解決方法編寫求e值的過程中題中只給出了最后一項的精度,這就要求我們用這一精度去先求出n來,求n就是要解決的問題之一,我用了一個循環(huán),先判斷符合不符合精度,如果大于所給精度讓初值為1的i自加1;最后小于精度的那一項停止循環(huán),隨之計算出n。編寫輝三角的過程中主要是一個排布的過程,因為按題中要求,得輸出題中的格式,所以必須在輸出的格式中加空格或退格,第二個空格是第一個的二倍,為了看起來比較流暢,我是用加退格,得到運行結(jié)果。心得體會編寫程序是一件令人欣悅的事,當你為解決某個問題絞盡腦汁最后又得到問題的解,那種心情只有自己才知道,我最大的心得就是通過編寫簡單的程序讓我對編寫程序有了更高的認識,邏輯上更清晰,增加了個人編程的小技巧,同時更有信心去編寫更復雜的程序。實驗名稱實驗二Java面向?qū)ο蟪绦蛟O(shè)計、異常處理和Applet小程序?qū)嶒災(zāi)康暮鸵笳莆認ava面向?qū)ο蟪绦蛟O(shè)計中類、繼承、包和接口的概念和應(yīng)用;掌握類變量和實例變量、類方法和實例方法的特點、使用;掌握異常的概念和Java異常處理機制,學會異常處理5個關(guān)鍵字的使用;熟悉Applet小程序的特點和運行過程,掌握Applet程序中常用的方法。實驗容完成教材實驗4中實驗容<1>完成教材實驗4中實驗容<3>。完成教材實驗5中實驗容<2>,指出拋出異常的語句,屬于何類異常。完成教材實驗5中實驗容<3>。完成教材實驗6中實驗容<2>主要儀器設(shè)備臺式或筆記本計算機實驗記錄<寫出實驗容中1,4,5的程序代碼和運行結(jié)果><可分欄或加頁>實驗四容〔1的程序代碼:importjava.io.*;importjava.util.Scanner;publicclassComplex{doublereal,imag; Complex<> { } Complex<doublereal,doubleimag> {this.real=real;this.imag=imag; }voidaddComplex<Complexc1,Complexc2> {this.real=c1.real+c2.real;this.imag=c1.imag+c2.imag; System.out.println<"<"+this.real+","+this.imag+">">; }voidminusComplex<Complexc1,Complexc2> {this.real=c1.real-c2.real;this.imag=c1.imag-c2.imag; System.out.println<"<"+this.real+","+this.imag+">">; }voidmultiplyComplex<Complexc1,Complexc2> {this.real=c1.real*c2.real-c1.imag*c2.imag;this.imag=c1.real*c2.imag+c1.imag*c2.real; System.out.println<"<"+this.real+","+this.imag+">">; }publicstaticvoidmain<String[]args>throwsIOException { Scannerin=newScanner<System.in>; System.out.print<"請輸入第一個復數(shù)的實部和虛部并用空格隔開:">; Complexc1=newComplex<in.nextDouble<>,in.nextDouble<>>; System.out.print<"\n請輸入第二個復數(shù)的實部和虛部并用空格隔開:">; Complexc2=newComplex<in.nextDouble<>,in.nextDouble<>>; Complexc=newComplex<>;booleana=true;while<a> { System.out.println<"請選擇菜單:">; System.out.println<"1.復數(shù)加法">; System.out.println<"2.復數(shù)減法">; System.out.println<"3.復數(shù)乘法">; System.out.println<"4.退出">;switch<in.nextByte<>> {case1:c.addComplex<c1,c2>;break;case2:c.minusComplex<c1,c2>;break;case3:c.multiplyComplex<c1,c2>;break;case4:a=false;break; } } }}運行結(jié)果說明:輸入的數(shù)可以是在精度圍的任何實數(shù),先輸入兩個復數(shù)后然后就可以通過菜單提示的功能去選擇是要做加,減,乘,還是退出程序,比書中的要求的相對靈活一些。實驗四容〔3的程序代碼:packagemymaths;publicinterfaceMaxMinValue{doubleseekMax<doublea,doubleb,doublec>;doubleseekMin<doublea,doubleb,doublec>;}packagemymaths;publicclassSeekMaxMinValueimplementsMaxMinValue{doublemax,min;publicdoubleseekMax<doublea,doubleb,doublec> {this.max=a>b?a:<b>c?b:c>;returnthis.max; }publicdoubleseekMin<doublea,doubleb,doublec> {this.min=a<b?a:<b<c?b:c>;returnthis.min; }}packagemymaths;importjava.util.Scanner;publicclassExample{publicstaticvoidmain<String[]args> { Scannerin=newScanner<System.in>; MaxMinValuenum=newSeekMaxMinValue<>;booleana=true;while<a> { System.out.println<"請選擇菜單:">; System.out.println<"1.取最大值">; System.out.println<"2.取最小值">; System.out.println<"3.退出">;switch<in.nextByte<>> {case1: System.out.print<"輸入要比較的三個數(shù)并用空格隔開:">; System.out.println<"Max="+num.seekMax<in.nextDouble<>,in.nextDouble<>,in.nextDouble<>>>;break;case2: System.out.print<"輸入要比較的三個數(shù)并用空格隔開:">; System.out.println<"Min="+num.seekMin<in.nextDouble<>,in.nextDouble<>,in.nextDouble<>>>;break;case3:a=false;break; } } }}運行結(jié)果說明:有些繁瑣,但根據(jù)提示進行就可以了實驗五容〔三程序:importjava.util.Scanner;publicclassBank{ Stringname;longremainder; Bank<Stringname,longremainder> {=name;this.remainder=remainder; }longwithdraeal<longgetMoney>throwsInsufficientFundsException {try {this.remainder=this.remainder-getMoney;if<this.remainder<0> {thrownewInsufficientFundsException<"尊敬的"++"您的余額不足">; }returnthis.remainder; }catch<InsufficientFundsExceptione> { System.out.println<e.getMessage<>>;returnthis.remainder=this.remainder+getMoney; } }publicstaticvoidmain<String[]args>throwsInsufficientFundsException { Scannerin=newScanner<System.in>; System.out.print<"請輸入您的和存款金額并用空格或回車隔開:">; Bankuser=newBank<in.next<>,in.nextLong<>>;booleana=true;while<a> { System.out.println<"請選擇菜單:">; System.out.println<"1.取款">; System.out.println<"2.退出">;switch<in.nextByte<>> {case1: { System.out.print<"請輸入您要取款的金額〔輸完之后按enter結(jié)束:">; System.out.println<"您的卡上余額:"+user.withdraeal<in.nextLong<>>>; };break;case2:a=false;break; } } }}classInsufficientFundsExceptionextendsException{publicInsufficientFundsException<Stringmsg> {super<msg>; }}運行結(jié)果說明:書中要求的也沒有這么多功能,為了熟悉而加了這些必要的功能,從上述輸入的數(shù)據(jù)中可以看書程序可以解決取錢以后卡上余額的顯示,并且繼續(xù)取還是取余額的錢,當余額不足是會提示信息,并且自己的余額不會變,還可以繼續(xù)取錢。實驗六容〔二程序:importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassTest1extendsAppletimplementsActionListener{ Buttonb=newButton<"change">; Buttonb1=newButton<"change1">;publicvoidinit<> { add<b>; add<b1>;b.addActionListener<this>;b1.addActionListener<this>; }publicvoidpaint<Graphicsg> { Fontf=newFont<"宋體",Font.PLAIN,18>; g.setFont<f>; g.setColor<Color.blue>; g.drawString<"Pleaselookatme.",0,30>; }publicvoidactionPerformed<ActionEvente> {if<e.getSource<>==b> { Graphicsg=getGraphics<>; Fontf=newFont<"楷體_GB2312",Font.PLAIN,18>; g.setFont<f>; g.setColor<Color.black>; g.drawString<"Pleaselookatme.",0,60>; }if<e.getSource<>==b1> { Graphicsg=getGraphics<>; Fontf=newFont<"宋體",Font.PLAIN+Font.ITALIC,18>; g.setFont<f>; g.setColor<Color.green>; g.drawString<"Pleaselookatme.",0,90>; } }}運行結(jié)果說明:按鈕change按下出來黑字體,按鈕change1按下出來綠色字體。遇到的問題和解決方法編程過程中遇到結(jié)果不一致的情況比較多,比如說銀行系統(tǒng)中取走錢后余額是正確的,在取錢時余額又回到了剛開始存的錢,解決這些問題中用的最多的是this的變化,在編寫復數(shù)是this用的相對也是比較多的,顯示了其較高的靈活性!心得體會在編完銀行系統(tǒng)時感覺很有成就感,在調(diào)試過程中都能按自己的要求去執(zhí)行,再次提升了我個人對編程的興趣,小程序我感覺編寫的還不夠好,只是做到了簡單的要求,我想再給我點兒時間會編的更好!實驗名稱實驗三圖形用戶界面、多線程和多媒體編程實驗?zāi)康暮鸵笳莆瘴谋窘M件、按鈕和單、復選按鈕組件的使用;掌握列表的使用,鼠標、鍵盤事件的處理;掌握布局控制的方法;掌握線程的概念、線程的生命周期;掌握采用擴展Thread類和實現(xiàn)Runnable接口的多線程編程方法;掌握Java中聲音播放、圖形繪制和顯示圖像的程序設(shè)計方法;實驗容編寫一個Applet程序響應(yīng)鼠標事件,當鼠標事件發(fā)生時,在狀態(tài)欄顯示相應(yīng)的狀態(tài)信息<使用showStatus<字符串>方法>。完成教材實驗7中實驗容<3>。完成教材實驗8中實驗容<1><2>。完成教材實驗8中實驗容<3>。編程繪制直線、各種矩形、各種橢圓及多邊形。編程顯示各種字體、字型、字號和不同顏色的文字。主要儀器設(shè)備臺式或筆記本計算機實驗記錄<寫出實驗容中1,2,5的程序代碼和運行結(jié)果><可分欄或加頁>實驗容一程序:importjava.applet.*;importjava.awt.*;importpter;importjava.awt.event.MouseEvent;publicclassMouseTextextendsApplet{ Panelp1,p2; TextAreaf;publicvoidinit<> { setSize<800,600>; setLayout<newGridLayout<1,2>>;p1=newPanel<>;p2=newPanel<>;p2.setLayout<newBorderLayout<>>;f=newTextArea<>;p2.add<f>; add<p1>; add<p2>;p1.addMouseListener<newMouseAdapter<> {publicvoidmouseClicked<MouseEvente> {intx=e.getX<>;inty=e.getY<>;f.append<"Pushat<"+x+","+y+">\n">; } }>; }}運行結(jié)果說明:當鼠標指向左邊區(qū)域時,相應(yīng)的坐標位置打印在文本區(qū)域。實驗七實驗容〔三程序:importjava.awt.*;importjava.awt.event.*;publicclassClickMe{ Framef; Buttonbtn;inta=1;publicvoidchushihua<> {f=newFrame<>;f.setSize<400,300>;f.setLayout<null>;btn=newButton<"ClickMe">;btn.setLocation<150,120>;btn.setSize<110,30>;f.add<btn>;f.setVisible<true>;btn.addActionListener<newActionListener<> {publicvoidactionPerformed<ActionEvente> {a++;if<e.getSource<>==btn> {if<a%2==0> {btn.setLabel<"ClickMeAgain">; }else {btn.setLabel<"ClickMe">; } } } }>; }publicstaticvoidmain<String[]args> { ClickMea=newClickMe<>; a.chushihua<>; }}運行結(jié)果:點擊后:再點擊后:說明:此程序是通過修改標簽來達到書中的目的的,點擊后將會不停地再變。實驗容五程序:importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassGraphicsTextextendsJFrameimplementsActionListener{ JPanelp1,p2; JLabellab1,lab2,lab3; JTextFieldf1,f2,f3; Stringstr1,str2,str3;intx,y;publicvoidinitJ<> {str1="直線";str2="圓";str3="矩形"; setSize<600,300>; GridLayoutg2=newGridLayout<1,2>; setLayout<g2>;p1=newJPanel<>;p2=newJPanel<>; add<p1>; add<p2>; setp1<>; setVisible<true>; }publicvoidsetp1<> { GridLayoutg1=newGridLayout<6,1>;p1.setLayout<g1>; Fontfn1=newFont<"宋體",Font.BOLD,25>;lab1=newJLabel<"請輸入要畫的幾何圖形:">;lab1.setFont<fn1>;lab2=newJLabel<"提示:只能輸入直線,圓,矩形">;p1.add<lab1>;p1.add<lab2>;f1=newJTextField<>;f1.setFont<fn1>;f1.addActionListener<this>;p1.add<f1>; }publicvoidactionPerformed<ActionEvente> {if<e.getSource<>==f1> {if<str1.equals<f1.getText<>>> {p2.addMouseListener<newMouseAdapter<> {publicvoidmousePressed<MouseEvente> {x=e.getX<>;y=e.getY<>; }publicvoidmouseReleased<MouseEvente> { Graphicsg=p2.getGraphics<>; g.drawLine<x,y,e.getX<>,e.getY<>>; } }>; }if<str2.equals<f1.getText<>>> {p2.addMouseListener<newMouseAdapter<> {publicvoidmousePressed<MouseEvente> {x=e.getX<>;y=e.getY<>; }publicvoidmouseReleased<MouseEvente> { Graphicsg=p2.getGraphics<>; g.drawOval<x,x,e.getX<>-x,e.getX<>-x>; } }>; }if<str3.equals<f1.getText<>>> {p2.addMouseListener<newMouseAdapter<> {publicvoidmousePressed<MouseEvente> {x=e.getX<>;y=e.getY<>; }publicvoidmouseReleased<MouseEvente> { Graphicsg=p2.getGraphics<>; g.drawRect<x,x,e.getX<>-x,e.getX<>-x>; } }>; } } }publicstaticvoidmain<String[]args> { GraphicsTextgt=newGraphicsText<>; gt.initJ<>; }}運行結(jié)果說明:此程序由鼠標事件完成,有一定局限性。遇到的問題和解決方法遇到的問題還是挺多,監(jiān)聽器使用不夠熟練,思路不夠清晰,總是出現(xiàn)錯誤。心得體會總的來說這次實驗不像前兩次那么有成就感,總感覺少了點什么,總覺得自己想的沒能夠按要求達到目的,或者說總感覺變得程序漏洞百出,好好加油吧,我覺得還是對圖形界面的編程不夠熟悉,才學了一個月,我還有很大發(fā)展空間,好好記住Java中常用的東西,練得多了才能更加熟練!實驗名稱實驗四網(wǎng)絡(luò)、數(shù)據(jù)庫和輸入/輸出流編程實驗?zāi)康暮鸵笳莆誙RL的概念和編程方法,掌握Socket的概念和編程方法、了解JDBC的概念和工作原理,掌握使用JDBC實現(xiàn)簡單的數(shù)據(jù)庫管理;了解流的概念和分類,掌握兩種基本輸入/輸出流類的使用;掌握Java文件類的使用。實驗容改寫教材例11.10的程序,使用圖形界面實現(xiàn)多用戶聊天程序。完成教材實驗11中的實驗容<1>~<4>。利用Java文件類File的方法,獲取磁盤文件的文件名、長度、大小等特性。編寫程序,利用Java的字符流文件輸入/輸出類實現(xiàn)將源文件復制到另一文件。主要儀器設(shè)備臺式或筆記本計算機實驗記錄<寫出實驗容中1,2,4的程序代碼和運行結(jié)果><可分欄或加頁>實驗1的程序及運行結(jié)果程序如下:importjava.io.*;import.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;publicclassZCX4_Server{publicstaticvoidmain<String[]args> { Framef=newFrame<"Server">; f.setSize<600,400>; f.setLayout<newGridLayout<3,1>>; Panelp1=newPanel<>; p1.setLayout<newBorderLayout<>>; Panelp2=newPanel<>; p2.setLayout<newBorderLayout<>>; Panelp3=newPanel<>; p3.setLayout<null>; f.add<p1>; f.add<p2>; f.add<p3>;finalTextAreatxt1=newTextArea<>; p1.add<txt1,"Center">;finalTextAreatxt2=newTextArea<>; p2.add<txt2,"Center">;finalButtonbtn=newButton<"發(fā)送">; btn.setSize<80,30>; btn.setLocation<260,60>; p3.add<btn>; f.setVisible<true>;try { ServerSocketserver=newServerSocket<4444>; Socketclient=server.accept<>; BufferedReaderis=newBufferedReader<newInputStreamReader<client.getInputStream<>>>;finalPrintWriteros=newPrintWriter<client.getOutputStream<>>; txt1.append<"客戶:"+is.readLine<>+"\n">; txt2.addKeyListener<newKeyAdapter<> {publicvoidkeyPressed<KeyEvente> {if<e.getKeyCode<>==KeyEvent.VK_ENTER> { txt1.append<"服務(wù)器:"+txt2.getText<>+"\n">; os.println<txt2.getText<>>; os.flush<>; txt2.setText<"">; } } }>; btn.addActionListener<newActionListener<> {publicvoidactionPerformed<ActionEvente> {if<e.getSource<>==btn> { txt1.append<"服務(wù)器::"+txt2.getText<>+"\n">; os.println<txt2.getText<>>; os.flush<>; txt2.setText<"">; } } }>;while<!txt2.getText<>.equals<"bye">> { txt1.append<"客戶:"+is.readLine<>+"\n">; } os.close<>; is.close<>; client.close<>; }catch<Exceptione> { System.out.println<"Error:"+e>; } }}importjava.io.*;import.*;importjava.awt.*;importjava.awt.event.*;publicclassZCX4_Client{publicstaticvoidmain<String[]args> { Framef=newFrame<"Client">; f.setSize<600,400>; f.setLayout<newGridLayout<3,1>>; Panelp1=newPanel<>; p1.setLayout<newBorderLayout<>>; Panelp2=newPanel<>; p2.setLayout<newBorderLayout<>>; Panelp3=newPanel<>; p3.setLayout<null>; f.add<p1>; f.add<p2>; f.add<p3>;finalTextAreatxt1=newTextArea<>; p1.add<txt1,"Center">;finalTextAreatxt2=newTextArea<>; p2.add<txt2,"Center">;finalButtonbtn=newButton<"發(fā)送">; btn.setSize<80,30>; btn.setLocation<260,60>; p3.add<btn>; f.setVisible<true>;try { Socketclient=newSocket<"",4444>; BufferedReaderis=newBufferedReader<newInputStreamReader<client.getInputStream<>>>;finalPrintWriteros=newPrintWriter<client.getOutputStream<>>; txt2.addKeyListener<newKeyAdapter<> {publicvoidkeyPressed<KeyEvente> {if<e.getKeyCode<>==KeyEvent.VK_ENTER> { txt1.append<"我自己:"+txt2.getText<>+"\n">; os.println<txt2.getText<>>; os.flush<>; txt2.setText<"">; } } }>; btn.addActionListener<newActionListener<> {publicvoidactionPerformed<ActionEvente> {if<e.getSource<>==btn> { txt1.append<"我自己:"+txt2.getText<>+"\n">; os.println<txt2.getText<>>; os.flush<>; txt2.setText<"">; } } }>;while<!txt2.getText<>.equals<"bye">> { txt1.append<"服務(wù)器:"+is.readLine<>+"\n">; } os.close<>; is.close<>; client.close<>; }catch<Exceptione> { System.out.println<"Error:"+e>; } }}運行結(jié)果:說明:先運行Server程序再運行client程序,然后在下邊的文本域中輸入要輸入的信息,按下enter或發(fā)送發(fā)送消息。實驗2:源代碼:importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.sql.*;publicclassStudentSql{publicstaticvoidmain<String[]args>{newfwin<>;}}classfwinextendsJFrameimplementsActionListener{ JTabletable; JButtonbt1,bt2,bt3,bt4; Objecta[][]; Connectionconn; Statementstm; ResultSetrs; ObjectcolumnName[]={"學號","","性別","年齡","班級"};inti=0; fwin<>{super<"學生數(shù)據(jù)庫查詢">;a=newObject[50][5];table=newJTable<a,columnName>; setSize<550,550>;bt1=newJButton<"瀏覽表">;bt2=newJButton<"添加學生信息">;bt3=newJButton<"年齡<>18>">;bt4=newJButton<"退出">;bt1.addActionListener<this>;bt2.addActionListener<this>;bt3.addActionListener<this>;bt4.addActionListener<this>; Containercon=getContentPane<>; con.setLayout<newFlowLayout<>>; con.add<bt1>; con.add<bt2>; con.add<bt3>; con.add<bt4>; Containercontainer=getContentPane<>; container.add<newJScrollPane<table>,BorderLayout.CENTER>; setVisible<true>; validate<>; addWindowListener<newWindowAdapter<>{publicvoidclosing<WindowEvente>{ System.exit<0>; } }>;}OverridepublicvoidactionPerformed<ActionEvente>{//TODOAuto-generatedmethodstubif<e.getSource<>==bt1>{while<i>=0>{//清空上次查詢結(jié)果a[i][0]="";a[i][1]="";a[i][2]="";a[i][3]="";a[i][4]="";i--; }i=0;try{ Class.forName<"sun.jdbc.odbc.JdbcOdbcDriver">; }catch<ClassNotFoundExceptione1>{//TODOAuto-generatedcatchblock e1.printStackTrace<>; }try{conn=DriverManager.getConnection<"jdbc:odbc:sqlserver">;stm=conn.createStatement<>;rs=stm.executeQuery<"Select*fromdbo.student">;while<rs.next<>>{ Stringid=rs.getString<"學號">; Stringname=rs.getString<"">; Stringsex=rs.getString<"性別">; Stringage=rs.getString<"年齡">; Stringiclass=rs.getString<"班級">;a[i][0]=id;a[i][1]=name;a[i][2]=sex;a[i][3]=age;a[i][4]=iclass;i++; }conn.close<>; repaint<>; }catch<SQLExceptione2>{} }elseif<e.getSource<>==bt2>{newtianjia<>;//新生成一個添加的窗口 }elseif<e.getSource<>==bt3>{while<i>=0>{//清空上次查詢結(jié)果a[i][0]="";a[i][1]="";a[i][2]="";a[i][3]="";a[i][4]="";i--; }i=0;try{ Class.forName<"sun.jdbc.odbc.JdbcOdbcDriver">; }catch<ClassNotFoundExceptione1>{//TODOAuto-generatedcatchblock e1.printStackTrace<>; }try{conn=DriverManager.getConnection<"jdbc:odbc:sqlserver">;stm=conn.createStatement<>;rs=stm.executeQuery<"Select*fromdbo.studentWHERE年齡>18">;while<rs.next<>>{ Stringid=rs.getString<"學號">; Stringname=rs.getString<"">; Stringsex=rs.getString<"性別">;intage=rs.getInt<"年齡">; Stringiclass=rs.getString<"班級">;a[i][0]=id;a[i][1]=name;a[i][2]=sex;a[i][3]=String.valueOf<age>;a[i][4]=iclass;i++; }conn.close<>; repaint<>; }catch<SQLExceptione2>{} }elseif<e.getSource<>==bt4>{ dispose<>; }}}classtianjiaextendsJFrameimplementsActionListener{TextFieldtid,tname,tsex,tage,tclass;Labellid,lname,lsex,lage,lclass;Buttonbtn;tianjia<>{ setLayout<null>;btn=newButton<"添加">;tid=newTextField<>;tname=newTextField<>;tsex=newTextField<>;tage=newTextField<>;tclass=newTextField<>;lid=newLabel<"學號">;lname=newLabel<"">;lsex=newLabel<"性別">;lage=newLabel<"年齡">;lclass=newLabel<"班級">; add<lid>; add<tid>; add<lname>; add<tname>; add<lsex>; add<tsex>; add<lage>; add<tage>; add<lclass>; add<tclass>; add<btn>;lid.setBounds<10,10,70,25>;tid.setBounds<90,10,220,25>;lname.setBounds<10,40,70,25>;tname.setBounds<90,40,220,25>;lsex.setBounds<10,70,70,25>;tsex.setBounds<90,70,220,25>;lage.setBounds<10,100,70,25>;tage.setBounds<90,100,220,25>;lclass.setBounds<10,130,70,25>;tclass.setBounds<90,130,220,25>;btn.setBounds<130,160,70,25>;btn.addActionListener<this>; setSize<340,250>; setBackground<Color.white>; setVisible<true>; validate<>; addWindowListener<newWindowAdapter<>{publicvoidclosing<WindowEvente>{ System.exit<0>; } }>;}OverridepublicvoidactionPerformed<ActionEvente>{//TODOAuto-generatedmethodstub Stringsid=tid.getText<>; Stringsname=tname.getText<>; Stringssex=tsex.getText<>; Stringsage=tage.getText<>; Stringsclass=tclass.g
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 郵政快遞實行安全查驗制度
- 薛城美術(shù)面試題目及答案
- 技術(shù)規(guī)范制定指南
- 設(shè)備更新建設(shè)實施制度
- 2025年鎮(zhèn)海中學音樂筆試題目及答案
- 2025年龍州衛(wèi)生健康招聘免筆試及答案
- 2025年1比80的事業(yè)單位考試及答案
- 2025年工會招錄工作人員筆試及答案
- 2025年華工綜評筆試題目及答案
- 2025年北京 人事考試及答案
- 尋脈山河:中國主要河流與湖泊的空間認知與生態(tài)理解-八年級地理教學設(shè)計
- 達人精準運營方案
- 四川省涼山州2025-2026學年上學期期末考試七年級數(shù)學試題(含答案)
- 語文試題-汕頭市2025-2026學年度普通高中畢業(yè)班教學質(zhì)量監(jiān)測(含解析)
- 水利水電工程單元工程施工質(zhì)量驗收標準(2025版)解讀課件
- 水利工程項目設(shè)計審批流程與管理要點
- 湖北省2026屆高三上學期元月調(diào)考政治+答案
- 2026年浙江高考英語考試真題及答案
- 垃圾填埋場排水施工方案
- 辦公室頸椎保養(yǎng)課件
- (16)普通高中體育與健康課程標準日常修訂版(2017年版2025年修訂)
評論
0/150
提交評論