一、問答題
1.(1)添加數(shù)據(jù)源,(2)選擇驅(qū)動(dòng)程序,(3)命名數(shù)據(jù)源名稱。
2.不必使用數(shù)據(jù)名稱。
3.減輕數(shù)據(jù)庫內(nèi)部SQL語句解釋器的負(fù)擔(dān)。
4.事務(wù)由一組SQL語句組成,所謂事務(wù)處理是指:應(yīng)用程序保證事務(wù)中的SQL語句要么全部都執(zhí)行,要么一個(gè)都不執(zhí)行。
5.(1)用setAutoCommit()方法關(guān)閉自動(dòng)提交模式,(2)用commit()方法處理事務(wù),(3)用rollback()方法處理事務(wù)失敗。
四、編程題
1.??import java.sql.*;
import java.util.*;
public class E {
???public static void main(String args[]) {
?????Query query=new Query();
?????String dataSource="myData";
?????String tableName="goods";
?????Scanner read=new Scanner(System.in);
?????System.out.print("輸入數(shù)據(jù)源名:");
?????dataSource = read.nextLine();
?????System.out.print("輸入表名:");
?????tableName = read.nextLine();
?????query.setDatasourceName(dataSource);
?????query.setTableName(tableName);
?????query.setSQL("SELECT * FROM "+tableName);
?????query.inputQueryResult();
???}
}
class Query {
???String datasourceName=""; ???????//數(shù)據(jù)源名
???String tableName=""; ???????????//表名
???String SQL; ???????????????????//SQL語句
???public Query() {
??????try{ ?Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
??????}
??????catch(ClassNotFoundException e) {
?????????System.out.print(e);
??????}
???}
???public void setDatasourceName(String s) {
??????datasourceName = s.trim();
???}
???public void setTableName(String s) {
??????tableName = s.trim();
???}
???public void setSQL(String SQL) {
??????this.SQL = SQL.trim();
???}
???public void inputQueryResult() {
??????Connection con;
??????Statement sql;
??????ResultSet rs;
??????try {
???????????String uri = "jdbc:odbc:"+datasourceName;
???????????String id = "";
???????????String password = "";
???????????con = DriverManager.getConnection(uri,id,password);
???????????DatabaseMetaData metadata = con.getMetaData();
???????????ResultSet rs1 = metadata.getColumns(null,null,tableName,null);
???????????int 字段個(gè)數(shù) = 0;
???????????while(rs1.next()) {
??????????????字段個(gè)數(shù)++;
???????????}
???????????sql = con.createStatement();
???????????rs = sql.executeQuery(SQL);
???????????while(rs.next()) {
?????????????for(int k=1;k<=字段個(gè)數(shù);k++) {
?????????????????System.out.print(" "+rs.getString(k)+" ");
?????????????}
?????????????System.out.println("");
???????????}
???????????con.close();
???????}
???????catch(SQLException e) {
???????????System.out.println("請(qǐng)輸入正確的表名"+e);
???????}
???} ???
}
2.???import java.sql.*;
import java.util.*;
public class E {
???public static void main(String args[]) {
?????Query query = new Query();
?????String dataSource = "myData";
?????String tableName = "goods";
?????query.setDatasourceName(dataSource);
?????query.setTableName(tableName);
?????String name = "";
?????Scanner read=new Scanner(System.in);
?????System.out.print("商品名:");
?????name = read.nextLine();
?????String str="'%["+name+"]%'";
?????String SQL = "SELECT * FROM "+tableName+" WHERE name LIKE "+str;
?????query.setSQL(SQL);
?????System.out.println(tableName+"表中商品名是"+name+"的記錄");
?????query.inputQueryResult();
???}
}
class Query {
???String datasourceName=""; ???????//數(shù)據(jù)源名
???String tableName=""; ???????????//表名
???String SQL; ???????????????????//SQL語句
???public Query() {
??????try{ ?Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
??????}
??????catch(ClassNotFoundException e) {
?????????System.out.print(e);
??????}
???}
???public void setDatasourceName(String s) {
??????datasourceName = s.trim();
???}
???public void setTableName(String s) {
??????tableName = s.trim();
???}
???public void setSQL(String SQL) {
??????this.SQL = SQL.trim();
???}
???public void inputQueryResult() {
??????Connection con;
??????Statement sql;
??????ResultSet rs;
??????try {
???????????String uri = "jdbc:odbc:"+datasourceName;
???????????String id = "";
???????????String password = "";
???????????con = DriverManager.getConnection(uri,id,password);
???????????DatabaseMetaData metadata = con.getMetaData();
???????????ResultSet rs1 = metadata.getColumns(null,null,tableName,null);
???????????int 字段個(gè)數(shù) = 0;
???????????while(rs1.next()) {
??????????????字段個(gè)數(shù)++;
???????????}
???????????sql = con.createStatement();
???????????rs = sql.executeQuery(SQL);
???????????while(rs.next()) {
?????????????for(int k=1;k<=字段個(gè)數(shù);k++) {
?????????????????System.out.print(" "+rs.getString(k)+" ");
?????????????}
?????????????System.out.println("");
???????????}
???????????con.close();
???????}
???????catch(SQLException e) {
???????????System.out.println("請(qǐng)輸入正確的表名"+e);
???????}
???} ???
}
3.將例子5中的代碼:
String SQL = "SELECT * FROM "+tableName+" ORDER BY name";
更改為:
String SQL = "SELECT * FROM "+tableName+" ORDER BY madeTime";
可達(dá)題目要求。
一、問答題
1.4種狀態(tài):新建、運(yùn)行、中斷和死亡。
2.有4種原因的中斷:(1)JVM將CPU資源從當(dāng)前線程切換給其他線程,使本線程讓出CPU的使用權(quán)處于中斷狀態(tài)。(2)線程使用CPU資源期間,執(zhí)行了sleep(int millsecond)方法,使當(dāng)前線程進(jìn)入休眠狀態(tài)。(3)線程使用CPU資源期間,執(zhí)行了wait()方法,使得當(dāng)前線程進(jìn)入等待狀態(tài)。(4)線程使用CPU資源期間,執(zhí)行某個(gè)操作進(jìn)入阻塞狀態(tài),比如執(zhí)行讀/寫操作引起阻塞。
3.死亡狀態(tài),不能再調(diào)用start()方法。
4.新建和死亡狀態(tài)。
5.兩種方法:用Thread類或其子類。
6.使用 setPrority(int grade)方法。
7.Java使我們可以創(chuàng)建多個(gè)線程,在處理多線程問題時(shí),我們必須注意這樣一個(gè)問題:當(dāng)兩個(gè)或多個(gè)線程同時(shí)訪問同一個(gè)變量,并且一個(gè)線程需要修改這個(gè)變量。我們應(yīng)對(duì)這樣的問題作出處理,否則可能發(fā)生混亂。
8.當(dāng)一個(gè)線程使用的同步方法中用到某個(gè)變量,而此變量又需要其它線程修改后才能符合本線程的需要,那么可以在同步方法中使用wait()方法。使用wait方法可以中斷方法的執(zhí)行,使本線程等待,暫時(shí)讓出CPU的使用權(quán),并允許其它線程使用這個(gè)同步方法。其它線程如果在使用這個(gè)同步方法時(shí)不需要等待,那么它使用完這個(gè)同步方法的同時(shí),應(yīng)當(dāng)用notifyAll()方法通知所有的由于使用這個(gè)同步方法而處于等待的線程結(jié)束等待。
9.不合理。
10.“吵醒”休眠的線程。一個(gè)占有CPU資源的線程可以讓休眠的線程調(diào)用interrupt 方法“吵醒”自己,即導(dǎo)致休眠的線程發(fā)生InterruptedException異常,從而結(jié)束休眠,重新排隊(duì)等待CPU資源。
二、選擇題
1.A。2.A。3.B。
三、閱讀程序
1.屬于上機(jī)調(diào)試題目,解答略。
2.屬于上機(jī)調(diào)試題目,解答略。
3.屬于上機(jī)調(diào)試題目,解答略。
4.屬于上機(jī)調(diào)試題目,解答略。
5.屬于上機(jī)調(diào)試題目,解答略。
6.屬于上機(jī)調(diào)試題目,解答略
7.【代碼】:BA。
8.屬于上機(jī)調(diào)試題目,解答略
四、編寫程序
1. ?public class E?{
?public static void main(String args[])?{
???????Cinema a=new Cinema();
????????a.zhang.start();
????????a.sun.start();
????????a.zhao.start();
???}
}
class TicketSeller ???//負(fù)責(zé)賣票的類。
{ ?int fiveNumber=3,tenNumber=0,twentyNumber=0;
???public synchronized void ?sellTicket(int receiveMoney)
???{ ?if(receiveMoney==5)
???????{ ?fiveNumber=fiveNumber+1;
??????????System.out.println(Thread.currentThread().getName()+
"給我5元錢,這是您的1張入場(chǎng)卷");
???????}
???????else if(receiveMoney==10) ??????????
????????{ while(fiveNumber<1)
????????????{ ??try { ?System.out.println(Thread.currentThread().getName()+"靠邊等");
???????????????????????wait(); ??
???????????????????????System.out.println(Thread.currentThread().getName()+"結(jié)束等待");
????????????????????}
???????????????catch(InterruptedException e) {}
????????????}
???????????fiveNumber=fiveNumber-1;
???????????tenNumber=tenNumber+1;
???????????System.out.println(Thread.currentThread().getName()+
"給我10元錢,找您5元,這是您的1張入場(chǎng)卷"); ?
????????}
???????else if(receiveMoney==20) ??????????
????????{ ?while(fiveNumber<1||tenNumber<1)
????????????{ ??try { ?System.out.println(Thread.currentThread().getName()+"靠邊等");
???????????????????????wait(); ??
???????????????????????System.out.println(Thread.currentThread().getName()+"結(jié)束等待");
????????????????????}
???????????????catch(InterruptedException e) {}
????????????}
???????????fiveNumber=fiveNumber-1;
???????????tenNumber=tenNumber-1;
???????????twentyNumber=twentyNumber+1; ??
???????????System.out.println(Thread.currentThread().getName()+
"給20元錢,找您一張5元和一張10元,這是您的1張入場(chǎng)卷");
?????????????????????????????
????????}
???????notifyAll();
???}
}
class Cinema implements Runnable ?????????
{ ?Thread zhang,sun,zhao;
???TicketSeller seller;
???Cinema()
???{ ?zhang=new Thread(this);
??????sun=new Thread(this);
??????zhao=new Thread(this);
??????zhang.setName("張小有");
??????sun.setName("孫大名");
??????zhao.setName("趙中堂");
??????seller=new TicketSeller();
???}
???public void run()
???{ ?if(Thread.currentThread()==zhang)
???????{ ?seller.sellTicket(20);
???????}
???????else if(Thread.currentThread()==sun)
???????{ ?seller.sellTicket(10);
???????}
???????else if(Thread.currentThread()==zhao)
???????{ seller.sellTicket(5);
???????}
???}
}
2. ?參照本章例子6
3.參照本章例子9