Advertisement
elsemTim

JDBC

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