《Java面向?qū)ο蟪绦蛟O(shè)計案例教程》課件第811章習(xí)題答案_第1頁
《Java面向?qū)ο蟪绦蛟O(shè)計案例教程》課件第811章習(xí)題答案_第2頁
《Java面向?qū)ο蟪绦蛟O(shè)計案例教程》課件第811章習(xí)題答案_第3頁
《Java面向?qū)ο蟪绦蛟O(shè)計案例教程》課件第811章習(xí)題答案_第4頁
《Java面向?qū)ο蟪绦蛟O(shè)計案例教程》課件第811章習(xí)題答案_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第8章習(xí)題答案一、選擇題1.C2.A3.B4.BC5.D二。、簡答題1.JDBC操作步驟(1)導(dǎo)入驅(qū)動程序(2)創(chuàng)建連接(3)創(chuàng)建Statement(4)書寫SQL語句(5)執(zhí)行SQL語句獲得結(jié)果(6)處理結(jié)果(7)關(guān)閉連接2.列表對于數(shù)據(jù)類型Java數(shù)據(jù)類型SQL數(shù)據(jù)類型bytetinyintSshortsmallintintintlongbigintfloatnumberDoublenumberDatedata/time/datatimeStringvarchar3.next方法工作流程,首先跳轉(zhuǎn)進入結(jié)果集對象的下一行記錄,如果記錄不為空,返回值為true,否則返回值為false。三、編程題1.固定SQL語句方式:publicintadminReg(Stringuname,Stringpswd){ intresult=-1; StringdriverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"; StringdbUrl="jdbc:sqlserver://localhost:1433;databaseName=TestDB"; Connectionconnection=null; Statementstmt=null; try{ Class.forName(driverClass); connection=DriverManager.getConnection(dbUrl,"sa","sasa"); stmt=connection.createStatement(); Stringsql="insertintotb_admin(admin_name,admin_pswd)values('"+uname+"','"+pswd+"')"; result=stmt.executeUpdate(sql); }catch(ClassNotFoundExceptione){ e.printStackTrace(); }catch(SQLExceptione){ e.printStackTrace(); } finally{ if(stmt!=null) try{ stmt.close(); }catch(Exceptione){ } if(connection!=null) try{ connection.close(); }catch(Exceptione){ } } returnresult; }預(yù)編譯SQL語句方式:publicintadminReg(Stringuname,Stringpswd){ intresult=-1; StringdriverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"; StringdbUrl="jdbc:sqlserver://localhost:1433;databaseName=TestDB"; Connectionconnection=null; PreparedStatementpstmt=null; try{ Class.forName(driverClass); connection=DriverManager.getConnection(dbUrl,"sa","sasa"); Stringsql="insertintotb_admin(admin_name,admin_pswd)values(?,?)"; pstmt=connection.prepareStatement(sql); pstmt.setString(1,uname); pstmt.setString(2,pswd); result=pstmt.executeUpdate(); }catch(ClassNotFoundExceptione){ e.printStackTrace(); }catch(SQLExceptione){ e.printStackTrace(); } finally{ if(pstmt!=null) try{ pstmt.close(); }catch(Exceptione){ } if(connection!=null) try{ connection.close(); }catch(Exceptione){ } } returnresult; }2.通用性很強的JDBC類importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;publicclassDBConn{ privatefinalStringclassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; privatefinalStringconnectionUrl="jdbc:sqlserver://localhost:1433;databaseName=TestDB"; privatefinalStringusername="sa"; privatefinalStringpassword="sasa"; privateConnectionconnection; privateStatementstmt; publicDBConn(){ try{ Class.forName(className); connection=DriverManager.getConnection(connectionUrl,username,password); System.out.println(connection); stmt=connection.createStatement(); }catch(Exceptione){ e.printStackTrace(); } } publicintupdate(Stringsql){ intr=-1; if(stmt!=null){ try{ r=stmt.executeUpdate(sql); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } returnr; } publicResultSetquery(Stringsql){ ResultSetrs=null; if(stmt!=null){ try{ rs=stmt.executeQuery(sql); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } returnrs; } publicvoidclose(){ if(connection!=null){ try{ connection.close(); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } }}第9章習(xí)題答案一、選擇題1.C2.A3.AB4.AD5.A6.A7.B二、簡答題1.字節(jié)流是以直接的方式傳送數(shù)據(jù),字符流是以字符的方式傳送數(shù)據(jù)。2.讀取操作步驟:(1)打開文件,(2)聲明緩存空間,(3)讀取/寫入,(4)關(guān)閉3.構(gòu)造方法:File(Stringpathname):通過給定路徑創(chuàng)建一個新的File類對象。File(Stringparent,Stringchild):通過給定的文件夾目錄和文件名創(chuàng)建一個新的File類對象。File(Fileparent,Stringchild):通過給定的文件對象和文件名創(chuàng)建一個新的File類對象。常用方法1.publicStringgetName():返回文件對象的XXXXXXX。2.publicStringgetPath():返回文件對象所在路徑名。3.publicStringgetAbsolutePath():返回文件對象的絕對路徑名。4.publicStringgetParent():返回文件對象所在父目錄路徑。如果文件對象沒有父目錄,則返回null。5.publicbooleanexists():判斷文件對象所表示的文件或文件夾是否存在,存在返回true,不存在返回false。6.publicbooleanisDirectory():判斷文件對象所表示的是否為目錄(文件夾),如果是返回true,否則返回false。7.publicbooleanisFile():判斷文件對象所表示的是否為一個標準文件,如果是,返回true,否則返回false。三、編程題1.packagep9_7;importjava.io.FileWriter;importjava.io.IOException;importjava.util.Scanner;publicclassC97_1{ @SuppressWarnings("resource") publicstaticvoidmain(String[]args){ System.out.println("請輸入字符串:"); Scannerscanner=newScanner(System.in); Strings=scanner.nextLine(); Stringss=s.toUpperCase(); try{ FileWriterwriter=newFileWriter("a.txt"); writer.write(ss); writer.close(); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } }}2.packagep9_7;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;importjava.util.Random;publicclassC97_2{ publicstaticvoidmain(String[]args){ Filef=newFile("test.txt"); if(!f.exists()) { try{ f.createNewFile(); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } Randomr=newRandom(); StringBuffersbf=newStringBuffer(); for(inti=1;i<=100;i++) { sbf.append(r.nextInt(100)); sbf.append(""); } try{ FileWriterfw=newFileWriter(f); fw.write(sbf.toString()); fw.close(); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } }}第10章習(xí)題答案一、選擇題1.D2.ABC3.A4.B5.C6.D7.A二、程序填空題1.(1)extends(2).run2.(1)Runnable(2)run1(3)t1三、編程題1.packagep10_7;publicclassC10_7{ publicstaticvoidmain(String[]args){ Objectobj=newObject(); WindowThreadthread01=newWindowThread("窗口一",obj); WindowThreadthread02=newWindowThread("窗口二",obj); //開啟線程 thread01

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論