版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、太原工業(yè)學院計算機工程系操作系統(tǒng) 實驗報告()實驗名稱動態(tài)分區(qū)分配方式的模擬實驗日期2016/12/3成績姓 名班級學號實驗目的了解動態(tài)分區(qū)分配方式中使用的數(shù)據結構和分配算法,進一步加深對動態(tài)分區(qū)存儲管理方式及其實現(xiàn)過程的理解實驗環(huán)境Eclipse,SQL Server實驗內容1. 在數(shù)據庫中創(chuàng)建分區(qū)表;2. JAVA實現(xiàn)采用首次適應算法和最佳適應算法的動態(tài)分區(qū)分配過程和回收過程。3. 設置初始狀態(tài),每次分配和回收后顯示出空閑內存分區(qū)表的情況。實驗步驟數(shù)據庫的創(chuàng)建:實驗代碼:連庫類:package common;import java.sql.Connection;import java.sq
2、l.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;public class ConnectionFactory / Create a variable for the connection string. private static final String connectionUrl = "jdbc:sqlserver:/localhost:1433;" + "DatabaseName=czxt;" / Declare the JDBC object
3、s. user=sa;password=wz951107 private static Connection conn = null;static try / Establish the connection. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); conn = DriverManager.getConnection(connectionUrl,"sa","wz951107"); /* if(conn!=null) System.out.print
4、ln("連接成功"); */ / Handle any errors that may have occurred. catch (Exception e) e.printStackTrace(); /關閉連接public static void close(ResultSet rs,PreparedStatement pstmt,Connection conn)throws Exception/釋放資源if(rs!=null)rs.close();if(pstmt!=null)pstmt.close();if(conn!=null)conn.close(); / 獲取連接
5、public static Connection getConnection() return conn;public static void main(String args)if(conn!=null)System.out.println("連接成功");實體類:package bean;public class Zone private String number;private String size;private String address;private String status;private String work;public String getW
6、ork() return work;public void setWork(String work) this.work = work;public Zone() super();public Zone(String number, String size, String address, String status, String work) super();this.number = number;this.size = size;this.address = address;this.status = status;this.work = work;public String getNu
7、mber() return number;public void setNumber(String number) this.number = number;public String getSize() return size;public void setSize(String size) this.size = size;public String getAddress() return address;public void setAddress(String address) this.address = address;public String getStatus() retur
8、n status;public void setStatus(String status) this.status = status;Overridepublic String toString() return "Zone number=" + number + ", size=" + size + ", address=" + address + ", status=" + status + ", work="+ work + ""Dao層類(對數(shù)據庫的操作):packa
9、ge dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import bean.Zone;import common.ConnectionFactory;public class ZoneDao /插入public void save(Zone zone)try Connection conn = null;PreparedStatement pstmt = null
10、;try /連接conn = ConnectionFactory.getConnection();/預處理sqlString sql = " insert into zone values(?,?,?,?,?)"pstmt = conn.prepareStatement(sql);pstmt.setString(1,zone.getNumber() );pstmt.setString(2, zone.getSize();pstmt.setString(3, zone.getAddress();pstmt.setString(4, zone.getStatus();pstmt
11、.setString(5, zone.getWork();/執(zhí)行sqlint num = pstmt.executeUpdate();System.out.println("Zone插入一條數(shù)據"); finally /*ConnectionFactory.close(null, pstmt, conn);*/ catch (Exception e) e.printStackTrace();/刪除public void deleteByWork(String work)try Connection conn = null;PreparedStatement pstmt =
12、null;try conn = ConnectionFactory.getConnection();String sql = " delete from zone where work = ?"pstmt = conn.prepareStatement(sql);pstmt.setString(1, work);int num = pstmt.executeUpdate();System.out.println("刪除"+num+"條數(shù)據"); finally /*ConnectionFactory.close(null, pstmt
13、, conn);*/ catch (Exception e) e.printStackTrace();/刪除public void deleteByNumber(String number)try Connection conn = null;PreparedStatement pstmt = null;try conn = ConnectionFactory.getConnection();String sql = " delete from zone where number = ?"pstmt = conn.prepareStatement(sql);pstmt.se
14、tString(1, number);int num = pstmt.executeUpdate();System.out.println("刪除"+num+"條數(shù)據"); finally /*ConnectionFactory.close(null, pstmt, conn);*/ catch (Exception e) e.printStackTrace();/查詢public List<Zone> FindAll()List<Zone> list = new ArrayList<Zone>();try Conne
15、ction conn = null;PreparedStatement pstmt = null;ResultSet rs = null;try /連接conn = ConnectionFactory.getConnection();/預處理sqlString sql = " select * from zone ORDER BY number"pstmt = conn.prepareStatement(sql);/執(zhí)行sqlrs = pstmt.executeQuery();while(rs.next()String number = rs.getString("
16、;number");String size = rs.getString("size");String address = rs.getString("address");String status = rs.getString("status");String work = rs.getString("work");Zone zone = new Zone(number, size, address, status,work);list.add(zone); finally /*ConnectionFa
17、ctory.close(rs, pstmt, conn);*/ catch (Exception e) e.printStackTrace();return list;/單個查詢public Zone queryByNumber(String number,String size,String address,String status,String work)Zone zone = new Zone();try Connection conn = null;PreparedStatement pstmt = null;ResultSet rs = null;try conn = Connec
18、tionFactory.getConnection();String sql = " select *from zone Where1 = 1"if(number != null)sql += " and number = '"+number+"'"if(size != null)sql += " and size = '"+size+"'"if(address != null)sql += " and address = '"+add
19、ress+"'"if(status != null)sql += " and status = '"+status+"'"if(work != null)sql += " and work = '"+work+"'"pstmt = conn.prepareStatement(sql);rs = pstmt.executeQuery();while(rs.next()zone.setNumber(rs.getString("number"
20、);zone.setAddress(rs.getString("address");zone.setSize(rs.getString("size");zone.setStatus(rs.getString("status");zone.setWork(rs.getString("work"); finally /*ConnectionFactory.close(rs, pstmt, conn);*/ catch (Exception e) e.printStackTrace();return zone;/修改pu
21、blic void update(Zone zone)try Connection conn = null;PreparedStatement pstmt = null;try conn = ConnectionFactory.getConnection();String sql = " update zone set"+" number = ?,size = ?,address = ?,status = ?,work = ?"+" where number = ?"pstmt = conn.prepareStatement(sql)
22、;pstmt.setString(1,zone.getNumber() );pstmt.setString(2, zone.getSize();pstmt.setString(3, zone.getAddress();pstmt.setString(4, zone.getStatus();pstmt.setString(5, zone.getWork();pstmt.setString(6,zone.getNumber() );int num = pstmt.executeUpdate();System.out.println("修改了"+num+"條數(shù)據&quo
23、t;); finally /*ConnectionFactory.close(null, pstmt, conn);*/ catch (Exception e) e.printStackTrace();/排序public List<Zone> asc()List<Zone> list = new ArrayList<>();try Connection conn = null;PreparedStatement pstmt = null;ResultSet rs = null;try conn = ConnectionFactory.getConnectio
24、n();String sql =" select * from zone ORDER BY address ASC"pstmt = conn.prepareStatement(sql);rs = pstmt.executeQuery();while(rs.next()String number = rs.getString("number");String size = rs.getString("size");String address = rs.getString("address");String stat
25、us = rs.getString("status");String work = rs.getString("work");Zone zone = new Zone(number, size, address, status,work);list.add(zone); finally catch (Exception e) e.printStackTrace();return list;視圖層:package view;import java.util.List;import java.util.Scanner;import bean.Zone;imp
26、ort dao.ZoneDao;public class Zoneview public static void main(String args) System.out.println("歡迎進入動態(tài)分區(qū):");System.out.println("1.首次適應算法");System.out.println("2.最佳適應算法");Scanner in = new Scanner(System.in);String one = in.nextLine();if(one.equals("1")ZoneDao da
27、o = new ZoneDao();List<Zone> list = dao.FindAll();for(Zone zone : list)System.out.println(zone);System.out.println("1.分配");System.out.println("2.回收");String two = in.nextLine();if(two.equals("1")System.out.println("請輸入作業(yè)號:");String work = in.nextLine();b
28、oolean flag = false;for(int j = 0;j<list.size();j+)Zone fzone = list.get(j);if(fzone.getWork().equals(work)flag = true;if(flag)System.out.println("該作業(yè)號已經存在");elseSystem.out.println("請輸入作業(yè)大小:");String size = in.nextLine();String address = null;String status = null;String number
29、 = work;String size2 = null;/修改分配的表Zone zone1 = new Zone();boolean f = false;for(int i = 0;i<list.size();i+)Zone zone = list.get(1);zone1 = zone;if(zone.getStatus().equals("空閑")if(Integer.valueOf(zone.getSize()>=Integer.valueOf(size)address = zone.getAddress();status = "占用"
30、size2 =String.valueOf(Integer.valueOf(zone.getSize().intValue()-Integer.valueOf(size).intValue() ;System.out.println(size2);zone1.setSize(String.valueOf(size2); zone1.setAddress(String.valueOf(Integer.valueOf(zone1.getAddress().intValue()+Integer.valueOf(size).intValue();Zone zone2 = new Zone(number
31、, size, address, status,work);dao.save(zone2);dao.update(zone1);f = true;break;if(!f)Zone mzone = list.get(0);zone1 = mzone;if(mzone.getStatus().equals("空閑")if(Integer.valueOf(mzone.getSize()>=Integer.valueOf(size)address = mzone.getAddress();status = "占用"size2 =String.valueOf
32、(Integer.valueOf(mzone.getSize().intValue()-Integer.valueOf(size).intValue() ;System.out.println(size2);zone1.setSize(String.valueOf(size2); zone1.setAddress(String.valueOf(Integer.valueOf(zone1.getAddress().intValue()+Integer.valueOf(size).intValue();Zone zone2 = new Zone(number, size, address, sta
33、tus,work);dao.save(zone2);dao.update(zone1);f = true;break;elseSystem.out.println("請輸入回收的作業(yè)號:");String work = in.nextLine();int i = 0;for(Zone zone : list)if(zone.getWork().equals(work)zone.setStatus("空閑");zone.setWork("空間");for(;i<list.size();)if(i!=0)if(list.get(i-
34、1).getStatus().equals("空閑")zone.setSize(String.valueOf(Integer.valueOf(zone.getSize()+Integer.valueOf(list.get(i-1).getSize();dao.deleteByNumber(list.get(i-1).getNumber();i=i-1;if(i+1<list.size()if(list.get(i+1).getStatus().equals("空閑")zone.setSize(String.valueOf(Integer.value
35、Of(zone.getSize()+Integer.valueOf(list.get(i+1).getSize();elsebreak;dao.update(zone);break;i+;elseSystem.out.println("1.分配");System.out.println("2.回收");String two = in.nextLine();ZoneDao dao = new ZoneDao();List<Zone> list = dao.FindAll();if(two.equals("1")System.
36、out.println("請輸入作業(yè)號:");String work = in.nextLine();boolean flag = false;for(int j = 0;j<list.size();j+)Zone fzone = list.get(j);if(fzone.getWork().equals(work)flag = true;if(flag)System.out.println("該作業(yè)號已經存在");elseSystem.out.println("請輸入作業(yè)大小:");String size = in.nextLine();String address = null
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年開發(fā)區(qū)綜合招商代理合作合同版
- 繪本故事托班課程設計
- 英語初中閱讀課課程設計
- 稅收籌劃課程設計進度
- 主治醫(yī)師資格(全科醫(yī)學301)考試題庫(全真題庫)
- 美麗小蠻腰雕刻課程設計
- 職業(yè)課程設計中的問題
- 游戲美術課程設計
- 職工培訓課程設計
- 汽車行業(yè)維修技能培訓總結
- 2024新一代變電站集中監(jiān)控系統(tǒng)系列規(guī)范第2部分:設計規(guī)范
- 財富管理課程設計
- 快樂寒假安全先行寒假安全教育主題班會課件
- 燃燒仿真.燃燒仿真軟件:OpenFOAM:湍流燃燒仿真原理
- 2024-2025學年七年級語文上冊第一學期 期末綜合模擬測試卷(人教版)
- 浙江省臺金七校2023-2024學年高一下學期4月期中考試英語試題
- 藍色卡通風胃腸減壓護理
- 小學單位換算-體積
- 叉車自行檢查記錄表
- 2024新安全生產法知識考試題庫及答案大全
- 專題5 書面表達-2023-2024學年譯林版五年級上冊英語期末專題復習
評論
0/150
提交評論