1、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 objects. user=sa;password=wz951107 private sta
2、tic 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.println(连接成功 */ / Handle any errors that may have occurred. catch (Exception e) e.
3、printStackTrace(); /关闭连接 public static void close(ResultSet rs,PreparedStatement pstmt,Connection conn)throws Exception /释放资源 if(rs!=null) rs.close(); if(pstmt! pstmt.close(); if(conn! conn.close(); / 获取连接 public static Connection getConnection() return conn; public static void main(String args) Sys
4、tem.out.println(实体类:package bean;public class Zone private String number; private String size; private String address; private String status; private String work; public String getWork() return work; public void setWork(String work) this.work = work; public Zone() super(); public Zone(String number,
5、 String size, String address, String status, String work) this.number = number; this.size = size; this.address = address; this.status = status; public String getNumber() return number; public void setNumber(String number) public String getSize() return size; public void setSize(String size) public S
6、tring getAddress() return address; public void setAddress(String address) public String getStatus() return status; public void setStatus(String status) Override public String toString() return Zone number= + number + , size= + size + , address= + address + , status= + status + , work= + work + Dao层类
7、(对数据库的操作):package dao;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; try /连接 conn = ConnectionFactory.getConnection(); /预处理sql String sq
8、l = 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.setString(5, zone.getWork(); /执行sql int num = pstmt.executeUpdate();Zone插入一条数
9、据 finally /*ConnectionFactory.close(null, pstmt, conn);*/ catch (Exception e) /删除 public void deleteByWork(String work) delete from zone where work = ? pstmt.setString(1, work);删除+num+条数据 finally public void deleteByNumber(String number) Connection conn = null; PreparedStatement pstmt = null; try co
10、nn = ConnectionFactory.getConnection(); String sql = delete from zone where number = ? pstmt = conn.prepareStatement(sql); pstmt.setString(1, number); int num = pstmt.executeUpdate(); System.out.println( finally /*ConnectionFactory.close(null, pstmt, conn); catch (Exception e) e.printStackTrace(); /
11、查询 public List FindAll() List list = new ArrayList String sql = select * from zone ORDER BY address ASC视图层:package view;import java.util.Scanner;import dao.ZoneDao;public class Zoneview public static void main(String args) System.out.println(欢迎进入动态分区:1.首次适应算法2.最佳适应算法 Scanner in = new Scanner(System.
12、in); String one = in.nextLine(); if(one.equals(1) ZoneDao dao = new ZoneDao(); List list = dao.FindAll(); for(Zone zone : list) System.out.println(zone); System.out.println(1.分配2.回收 String two = in.nextLine(); if(two.equals(请输入作业号: String work = in.nextLine(); boolean flag = false; for(int j = 0;jlist.size();j+) Zone fzone = list.get(j); if(fzone.getWork().equals(work) flag = true; if(flag)该作业号已经存在 else请输入作业大小: String size = in.nextLine(); String address = null; String status = null; String number = work; String size2 = null;/修改分配的表 Zone zone1 = new Zone(); boolean f = false;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1