Advertisement
elsemTim

DBconnect

Nov 16th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.23 KB | None | 0 0
  1. /**
  2.  * Created by elsemTim on 14.11.2016.
  3.  */
  4.  
  5. import java.security.acl.Group;
  6. import java.sql.Connection;
  7. import java.sql.Date;
  8. import java.sql.DriverManager;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.ArrayList;
  14. import java.util.Collection;
  15. import java.util.List;
  16.  
  17.  
  18.  
  19. public class DBConnect {
  20.  
  21.     private static DBConnect _ourInstance;
  22.     private static Connection _ourCon;
  23.  
  24.  
  25.     public static DBConnect getInstance() throws Exception {
  26.         if (_ourInstance ==null)
  27.         {
  28.             _ourInstance = new DBConnect();
  29.         }
  30.         return _ourInstance;
  31.     }
  32.  
  33.     private DBConnect() throws Exception {
  34.         try
  35.         {
  36.             Class.forName("org.firebirdsql.jdbc.FBDriver");
  37.             String strDatabasePath ="d:\\DBWork\\OURDB.FDB";
  38.             String strUrl="jdbc:firebirdsql://localhost:3050/"+strDatabasePath;
  39.             _ourCon=DriverManager.getConnection(strUrl,"SYSDBA","123");
  40.         }
  41.         catch (Exception e)
  42.         {
  43.             throw new Exception(e);
  44.         }
  45.     }
  46.  
  47.     //Employee
  48.     public List<Employee> SelectEmp() throws SQLException{
  49.         List<Employee> employ = new ArrayList<Employee>();
  50.         Statement stmt = null;
  51.         ResultSet rs = null;
  52.         try {
  53.             stmt = _ourCon.createStatement();
  54.             //тут нужен запрос, который свяжент все таблицы и кинет их сюда
  55.             rs = stmt.executeQuery( "SELECT * FROM employee;");
  56.             while (rs.next()) {
  57.                 Employee emp = new Employee(rs);
  58.                 emp.SetDepID(rs.getInt(0));
  59.                 emp.SetPosID(rs.getInt(1));
  60.                 emp.SetFirstName(rs.getString(2));
  61.                 emp.SetLastName(rs.getString(3));
  62.                 employ.add(emp);
  63.             }
  64.         }
  65.         finally {
  66.             if (rs != null) {
  67.                 rs.close();
  68.             }
  69.             if (stmt != null) {
  70.                 stmt.close();
  71.             }
  72.         }
  73.         return employ;
  74.     }
  75.  
  76.     public void UpdateEmp (Employee emp) throws SQLException
  77.     {
  78.         PreparedStatement stmt = null;
  79.         try{
  80.             stmt = _ourCon.prepareStatement(
  81.                     "UPDATE employee SET "
  82.                             +"DEP_ID=?, POS_ID=?  "
  83.                             +"WHERE DEP_ID=? AND POS_ID=?;");
  84.             stmt.setInt(1, emp.GetDepID());
  85.             stmt.setInt(2, emp.GetPosID());
  86.             stmt.setInt(3, emp.GetDepID());
  87.             stmt.setInt(4, emp.GetPosID());
  88.             stmt.executeUpdate();
  89.         }
  90.         finally {
  91.             if (stmt!=null){
  92.                 stmt.close();
  93.             }
  94.         }
  95.     }
  96.  
  97.     public void RemoveEmp (Employee emp) throws  SQLException
  98.     {
  99.         PreparedStatement stmt = null;
  100.         try{
  101.             stmt = _ourCon.prepareStatement(
  102.                     "DELETE FROM employee WHERE "
  103.                             +"DEP_ID=? AND POS_ID=?;");
  104.             stmt.setInt(1, emp.GetDepID());
  105.             stmt.setInt(2, emp.GetPosID());
  106.             stmt.executeUpdate();
  107.         }
  108.         finally {
  109.             if (stmt!=null){
  110.                 stmt.close();
  111.             }
  112.         }
  113.     }
  114.  
  115.     public void InsertEmp (Employee emp) throws SQLException
  116.     {
  117.         PreparedStatement stmt = null;
  118.         try{
  119.             stmt = _ourCon.prepareStatement(
  120.                     "INSERT INTO employee "
  121.                             +"(DEP_ID, POS_ID, FIRST_NAME, LAST_NAME) "
  122.                             +"VALUES (?, ?, ?, ?);");
  123.             stmt.setInt(1, emp.GetDepID());
  124.             stmt.setInt(2, emp.GetPosID());
  125.             stmt.setString(3, emp.GetFirstName());
  126.             stmt.setString(4, emp.GetLastName());
  127.             stmt.executeUpdate();
  128.         }
  129.         finally {
  130.             if (stmt!=null){
  131.                 stmt.close();
  132.             }
  133.         }
  134.     }
  135.  
  136.     //Position OK
  137.     public List<Position> SelectPos() throws SQLException{
  138.         List<Position> posit = new ArrayList<Position>();
  139.         Statement stmt = null;
  140.         ResultSet rs = null;
  141.         try {
  142.             stmt = _ourCon.createStatement();
  143.             //тут нужен запрос, который свяжент все таблицы и кинет их сюда
  144.             rs = stmt.executeQuery( "SELECT * FROM positionn;");
  145.             while (rs.next()) {
  146.                 Position pos = new Position(rs);
  147.                 pos.SetID(rs.getInt(0));
  148.                 pos.SetNamePosition(rs.getString(1));
  149.                 pos.SetSalary(rs.getString(2));
  150.                 posit.add(pos);
  151.             }
  152.         }
  153.         finally {
  154.             if (rs != null) {
  155.                 rs.close();
  156.             }
  157.             if (stmt != null) {
  158.                 stmt.close();
  159.             }
  160.         }
  161.         return posit;
  162.     }
  163.  
  164.     public void UpdatePos(Position pos) throws SQLException
  165.     {
  166.         PreparedStatement stmt = null;
  167.         try{
  168.             stmt = _ourCon.prepareStatement(
  169.                     "UPDATE positionn SET "
  170.                     +"SALARY=?, NAME=?  "
  171.                     +"WHERE ID=?;");
  172.             stmt.setInt(1, pos.GetID());
  173.             stmt.setString(2,pos.GetNamePosition());
  174.             stmt.setString(3,pos.GetSalary());
  175.             stmt.executeUpdate();
  176.         }
  177.         finally {
  178.             if (stmt!=null){
  179.                 stmt.close();
  180.             }
  181.         }
  182.  
  183.     }
  184.  
  185.     public void RemovePos(Position pos) throws SQLException
  186.     {
  187.         PreparedStatement stmt = null;
  188.         try{
  189.             stmt = _ourCon.prepareStatement(
  190.                     "DELETE FROM positionn WHERE "
  191.                     +"ID=?;");
  192.             stmt.setInt(1,pos.GetID());
  193.             stmt.executeUpdate();
  194.         }
  195.         finally {
  196.             if (stmt!=null){
  197.                 stmt.close();
  198.             }
  199.         }
  200.     }
  201.  
  202.     //
  203.     public void InsertPos(Position pos) throws SQLException
  204.     {
  205.         PreparedStatement stmt = null;
  206.         try{
  207.             stmt = _ourCon.prepareStatement(
  208.                     "INSERT INTO positionn "
  209.                     +"(ID, SALARY, NAME) "
  210.                     +"VALUES (NULL, ?, ?);");
  211.             stmt.setString(1,pos.GetSalary());
  212.             stmt.setString(2,pos.GetNamePosition());
  213.             stmt.executeUpdate();
  214.         }
  215.         finally {
  216.             if (stmt!=null){
  217.                 stmt.close();
  218.             }
  219.         }
  220.     }
  221.  
  222.  
  223.     //Department OK
  224.     public List<Department> SelectDep() throws SQLException{
  225.         List<Department> depart = new ArrayList<Department>();
  226.         Statement stmt = null;
  227.         ResultSet rs = null;
  228.         try {
  229.             stmt = _ourCon.createStatement();
  230.             //тут нужен запрос, который свяжент все таблицы и кинет их сюда
  231.             rs = stmt.executeQuery( "SELECT * FROM department;");
  232.             while (rs.next()) {
  233.                 Department dep = new Department(rs);
  234.                 dep.SetID(rs.getInt(0));
  235.                 dep.SetNameDepartment(rs.getString(1));
  236.                 dep.SetMail(rs.getString(2));
  237.                 dep.SetPhone(rs.getString(3));
  238.                 depart.add(dep);
  239.             }
  240.         }
  241.         finally {
  242.             if (rs != null) {
  243.                 rs.close();
  244.             }
  245.             if (stmt != null) {
  246.                 stmt.close();
  247.             }
  248.         }
  249.         return depart;
  250.     }
  251.  
  252.     public void UpdateDep(Department dep) throws SQLException
  253.     {
  254.         PreparedStatement stmt = null;
  255.         try{
  256.             stmt = _ourCon.prepareStatement(
  257.                     "UPDATE department SET "
  258.                             +"DEPARTMENT=?, MAIL=?, PHONE=? "
  259.                             +"WHERE ID=?;");
  260.             stmt.setString(1,dep.GetNameDepartment());
  261.             stmt.setString(2,dep.GetMail());
  262.             stmt.setString(3,dep.GetPhone());
  263.             stmt.setInt(4,dep.GetID());
  264.             stmt.executeUpdate();
  265.         }
  266.         finally {
  267.             if (stmt!=null){
  268.                 stmt.close();
  269.             }
  270.         }
  271.     }
  272.  
  273.     public void RemoveDep(Department dep) throws SQLException
  274.     {
  275.         PreparedStatement stmt = null;
  276.         try{
  277.             stmt = _ourCon.prepareStatement(
  278.                     "DELETE FROM department WHERE "
  279.                             +"ID=?;");
  280.             stmt.setInt(1,dep.GetID());
  281.             stmt.executeUpdate();
  282.         }
  283.         finally {
  284.             if (stmt!=null){
  285.                 stmt.close();
  286.             }
  287.         }
  288.     }
  289.  
  290.     public void InsertDep(Department dep) throws SQLException
  291.     {
  292.         PreparedStatement stmt = null;
  293.         try{
  294.             stmt = _ourCon.prepareStatement(
  295.                     "INSERT INTO department "
  296.                             +"(ID, DEPARTMENT, MAIL, PHONE) "
  297.                             +"VALUES (NULL, ?, ?, ?);");
  298.             stmt.setString(0,dep.GetNameDepartment());
  299.             stmt.setString(1,dep.GetMail());
  300.             stmt.setString(2,dep.GetPhone());
  301.             stmt.executeUpdate();
  302.         }
  303.         finally {
  304.             if (stmt!=null){
  305.                 stmt.close();
  306.             }
  307.         }
  308.     }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement