Advertisement
elsemTim

JDBC1

Nov 15th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.10 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 void UpdateEmp () throws SQLException
  49.     {
  50.         PreparedStatement stmt = null;
  51.         try{
  52.             stmt = _ourCon.prepareStatement(
  53.                     "UPDATE employee SET "
  54.                             +"DEP_ID=?, POS_ID=?  "
  55.                             +"WHERE DEP_ID=? AND POS_ID=?;");
  56.             stmt.setInt(1,...);
  57.             stmt.setInt(2,...);
  58.             stmt.setInt(3,...);
  59.             stmt.setInt(4,...);
  60.             stmt.executeUpdate();
  61.         }
  62.         finally {
  63.             if (stmt!=null){
  64.                 stmt.close();
  65.             }
  66.         }
  67.     }
  68.  
  69.     public void RemoveEmp () throws  SQLException
  70.     {
  71.         PreparedStatement stmt = null;
  72.         try{
  73.             stmt = _ourCon.prepareStatement(
  74.                     "DELETE FROM employee WHERE "
  75.                             +"DEP_ID=? AND POS_ID=?;");
  76.             stmt.setInt(1,...);
  77.             stmt.setInt(2,...);
  78.             stmt.executeUpdate();
  79.         }
  80.         finally {
  81.             if (stmt!=null){
  82.                 stmt.close();
  83.             }
  84.         }
  85.     }
  86.  
  87.     public void InsertEmp () throws SQLException
  88.     {
  89.         PreparedStatement stmt = null;
  90.         try{
  91.             stmt = _ourCon.prepareStatement(
  92.                     "INSERT INTO employee "
  93.                             +"(DEP_ID, POS_ID, FIRST_NAME, LAST_NAME) "
  94.                             +"VALUES (?, ?, ?, ?);");
  95.             stmt.setInt(1,...);
  96.             stmt.setInt(2,...);
  97.             stmt.setString(3,...);
  98.             stmt.setString(4,...);
  99.             stmt.executeUpdate();
  100.         }
  101.         finally {
  102.             if (stmt!=null){
  103.                 stmt.close();
  104.             }
  105.         }
  106.     }
  107.  
  108.     //Position
  109.     //Поправить вх. данные, реализовать
  110.     //методы для получения значений
  111.     public void UpdatePos() throws SQLException
  112.     {
  113.         PreparedStatement stmt = null;
  114.         try{
  115.             stmt = _ourCon.prepareStatement(
  116.                     "UPDATE positionn SET "
  117.                     +"SALARY=?, NAME=?  "
  118.                     +"WHERE ID=?;");
  119.             stmt.setInt(1,...);
  120.             stmt.setString(2,...);
  121.             stmt.setInt(3,..);
  122.             stmt.executeUpdate();
  123.         }
  124.         finally {
  125.             if (stmt!=null){
  126.                 stmt.close();
  127.             }
  128.         }
  129.  
  130.     }
  131.  
  132.     public void RemovePos() throws SQLException
  133.     {
  134.         PreparedStatement stmt = null;
  135.         try{
  136.             stmt = _ourCon.prepareStatement(
  137.                     "DELETE FROM positionn WHERE "
  138.                     +"ID=?;");
  139.             stmt.setInt(1,...);
  140.             stmt.executeUpdate();
  141.         }
  142.         finally {
  143.             if (stmt!=null){
  144.                 stmt.close();
  145.             }
  146.         }
  147.     }
  148.  
  149.     //
  150.     public void InsertPos() throws SQLException
  151.     {
  152.         PreparedStatement stmt = null;
  153.         try{
  154.             stmt = _ourCon.prepareStatement(
  155.                     "INSERT INTO positionn "
  156.                     +"(ID, SALARY, NAME) "
  157.                     +"VALUES (NULL, ?, ?);");
  158.             stmt.setInt(1,...);
  159.             stmt.setString(2,...);
  160.             stmt.executeUpdate();
  161.         }
  162.         finally {
  163.             if (stmt!=null){
  164.                 stmt.close();
  165.             }
  166.         }
  167.     }
  168.  
  169.  
  170.     //Department
  171.     public List<Department> SelectDep() throws SQLException{
  172.         List<Department> depart = new ArrayList<Department>();
  173.         Statement stmt = null;
  174.         ResultSet rs = null;
  175.         try {
  176.             stmt = _ourCon.createStatement();
  177.             //тут нужен запрос, который свяжент все таблицы и кинет их сюда
  178.             rs = stmt.executeQuery( "SELECT * FROM employee");
  179.             while (rs.next()) {
  180.                 Department dep = new Department(rs);
  181.                 dep.SetID((rs.getInt(0)));
  182.                 dep.SetNameDepartment(rs.getString(1));
  183.                 dep.SetMail(rs.getString(2));
  184.                 dep.SetPhone(rs.getString(3));
  185.                 depart.add(dep);
  186.             }
  187.         }
  188.         finally {
  189.             if (rs != null) {
  190.                 rs.close();
  191.             }
  192.             if (stmt != null) {
  193.                 stmt.close();
  194.             }
  195.         }
  196.         return depart;
  197.     }
  198.  
  199.     public void UpdateDep() throws SQLException
  200.     {
  201.         Department dep = new Department();
  202.  
  203.         PreparedStatement stmt = null;
  204.         try{
  205.             stmt = _ourCon.prepareStatement(
  206.                     "UPDATE department SET "
  207.                             +"DEPARTMENT=?, MAIL=?, PHONE=? "
  208.                             +"WHERE ID=?;");
  209.             stmt.setString(1,dep);
  210.             stmt.setString(2,...);
  211.             stmt.setString(3,...);
  212.             stmt.setInt(4,...);
  213.             stmt.executeUpdate();
  214.         }
  215.         finally {
  216.             if (stmt!=null){
  217.                 stmt.close();
  218.             }
  219.         }
  220.     }
  221.  
  222.     public void RemoveDep() throws SQLException
  223.     {
  224.         PreparedStatement stmt = null;
  225.         try{
  226.             stmt = _ourCon.prepareStatement(
  227.                     "DELETE FROM department WHERE "
  228.                             +"ID=?;");
  229.             stmt.setInt(1,...);
  230.             stmt.executeUpdate();
  231.         }
  232.         finally {
  233.             if (stmt!=null){
  234.                 stmt.close();
  235.             }
  236.         }
  237.     }
  238.  
  239.     public void InsertDep() throws SQLException
  240.     {
  241.         PreparedStatement stmt = null;
  242.         try{
  243.             stmt = _ourCon.prepareStatement(
  244.                     "INSERT INTO department "
  245.                             +"(ID, DEPARTMENT, MAIL, PHONE) "
  246.                             +"VALUES (NULL, ?, ?, ?);");
  247.             stmt.setString(1,...);
  248.             stmt.setString(2,...);
  249.             stmt.setString(3,...);
  250.             stmt.executeUpdate();
  251.         }
  252.         finally {
  253.             if (stmt!=null){
  254.                 stmt.close();
  255.             }
  256.         }
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement