Advertisement
Guest User

Untitled

a guest
Dec 26th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 KB | None | 0 0
  1. package pac;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.sql.ResultSet;
  8.  
  9. public class DataBase {
  10.     private Connection c;
  11.     private Statement stmt;
  12.     public DataBase() {
  13.        
  14.     }
  15.     public Object[][] getTableData(String tableName) throws SQLException {
  16.         c = null;
  17.         stmt = null;
  18.           try {
  19.              Class.forName("org.postgresql.Driver");
  20.              c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/kond", "postgres", "POI098poi");
  21.           } catch (Exception e) {
  22.              e.printStackTrace();
  23.              System.err.println(e.getClass().getName()+": "+e.getMessage());
  24.              System.exit(0);
  25.           }
  26.           //System.out.println("Opened database successfully");
  27.          
  28.         stmt = c.createStatement();
  29.         ResultSet rs1 = stmt.executeQuery("SELECT COUNT(*) FROM kond." + tableName);
  30.         rs1.next();
  31.         int m = rs1.getInt(1);
  32.         ResultSet rs = stmt.executeQuery("SELECT * FROM kond." + tableName);
  33.         int n = rs.getMetaData().getColumnCount();
  34.        
  35.         Object[][] ans = new Object[m + 1][n];
  36.         int i = 0;
  37.         int max = 0;
  38.         while (rs.next()) {
  39.             for (int j = 0; j < n; j++) {
  40.                 ans[i][j] = rs.getString(j + 1);
  41.                 if (j == 0 && Integer.parseInt((String) ans[i][j]) > max) {
  42.                     max = Integer.parseInt((String) ans[i][j]);
  43.                 }
  44.             }
  45.             i++;
  46.         }
  47.         for (int j = 1; j < n; j++) {
  48.             ans[m][j] = "";
  49.         }
  50.         ans[m][0] = max + 1;
  51.         rs1.close();
  52.         rs.close();
  53.         stmt.close();
  54.         c.close();
  55.         return ans;
  56.     }
  57.     public void updateTable(String tableName, int posRow, int posCol, String newVal) throws SQLException {
  58.        
  59.         c = null;
  60.         stmt = null;
  61.           try {
  62.              Class.forName("org.postgresql.Driver");
  63.              c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/kond", "postgres", "POI098poi");
  64.           } catch (Exception e) {
  65.              e.printStackTrace();
  66.              System.err.println(e.getClass().getName()+": "+e.getMessage());
  67.              System.exit(0);
  68.           }
  69.           //System.out.println("Opened for update database successfully");
  70.        
  71.          
  72.         stmt = c.createStatement();
  73.        
  74.         ResultSet rs = stmt.executeQuery("SELECT * FROM kond." + tableName);
  75.        
  76.         int i = 0;
  77.         while (i < posRow + 1) {
  78.             rs.next();
  79.             i++;
  80.         }
  81.        
  82.         String idName = rs.getMetaData().getColumnName(1);
  83.         String thatUpdate = rs.getMetaData().getColumnName(posCol + 1);
  84.         String id = rs.getString(1);
  85.         String query = "UPDATE kond." + tableName + " SET " + thatUpdate + " = " + "'" + newVal + "'" + " WHERE " + idName + " = " + id;
  86.         rs.close();
  87.        
  88.         stmt.executeUpdate(query);
  89.         //c.commit();
  90.         stmt.close();
  91.         c.close();
  92.        
  93.     }
  94.     public void deleteFromTable(String tableName, int posRow, int posCol) throws SQLException {
  95.        
  96.         c = null;
  97.         stmt = null;
  98.           try {
  99.              Class.forName("org.postgresql.Driver");
  100.              c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/kond", "postgres", "POI098poi");
  101.           } catch (Exception e) {
  102.              e.printStackTrace();
  103.              System.err.println(e.getClass().getName()+": "+e.getMessage());
  104.              System.exit(0);
  105.           }
  106.           //System.out.println("Opened for update database successfully");
  107.        
  108.          
  109.         stmt = c.createStatement();
  110.        
  111.         ResultSet rs = stmt.executeQuery("SELECT * FROM kond." + tableName);
  112.        
  113.         int i = 0;
  114.         while (i < posRow + 1) {
  115.             rs.next();
  116.             i++;
  117.         }
  118.        
  119.         String idName = rs.getMetaData().getColumnName(1);
  120.         String id = rs.getString(1);
  121.         String query = "DELETE FROM kond." + tableName + " WHERE " + idName + " = " + id;
  122.         rs.close();
  123.        
  124.         stmt.executeUpdate(query);
  125.         //c.commit();
  126.         stmt.close();
  127.         c.close();
  128.     }
  129.     public void insertData(String tableName, Object args[]) throws SQLException {
  130.         String vals = "";
  131.         for (int i = 0; i < args.length; i++) {
  132.             vals += "'" + args[i] + "'";
  133.             if (i + 1 < args.length) {
  134.                 vals += ", ";
  135.             }
  136.         }
  137.         c = null;
  138.         stmt = null;
  139.           try {
  140.              Class.forName("org.postgresql.Driver");
  141.              c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/kond", "postgres", "POI098poi");
  142.           } catch (Exception e) {
  143.              e.printStackTrace();
  144.              System.err.println(e.getClass().getName()+": "+e.getMessage());
  145.              System.exit(0);
  146.           }
  147.           //System.out.println("Opened for update database successfully");
  148.        
  149.          
  150.         stmt = c.createStatement();
  151.        
  152.         String query = "INSERT INTO kond." + tableName + " VALUES(" + vals + ")";  
  153.        
  154.         stmt.executeUpdate(query);
  155.         //c.commit();
  156.         stmt.close();
  157.         c.close();
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement