Advertisement
Guest User

Untitled

a guest
May 6th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.00 KB | None | 0 0
  1. package net.vasile2k.databaze;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.table.DefaultTableModel;
  5. import java.awt.*;
  6. import java.sql.*;
  7. import java.util.ArrayList;
  8.  
  9. public class Databazuitor {
  10.  
  11.     public static void main(String... args){
  12.         try {
  13.             Class.forName("org.sqlite.JDBC");
  14.         } catch (ClassNotFoundException e) {
  15.             e.printStackTrace();
  16.         }
  17.  
  18.         JFrame frame = new JFrame("Bazie Die Datie");
  19.         frame.setLocation(200, 200);
  20.         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  21.         frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
  22.  
  23.         JPanel panel = new JPanel();
  24.         panel.setPreferredSize(new Dimension(1280, 720));
  25.  
  26.         JButton loadButton = new JButton("Load shit");
  27.         loadButton.addActionListener(e -> {
  28.             try {
  29.                 loadData(panel);
  30.             } catch (SQLException e1) {
  31.                 e1.printStackTrace();
  32.             }
  33.         });
  34.  
  35.         JButton saveButton = new JButton("Save shit");
  36.         saveButton.addActionListener(e -> {
  37.             try {
  38.                 saveData(panel);
  39.             } catch (SQLException e1) {
  40.                 e1.printStackTrace();
  41.             }
  42.         });
  43.  
  44.         JPanel buttonPanel = new JPanel();
  45.         buttonPanel.add(loadButton);
  46.         buttonPanel.add(saveButton);
  47.  
  48.         frame.add(panel);
  49.         frame.add(buttonPanel);
  50.         frame.pack();
  51.         frame.setVisible(true);
  52.  
  53.     }
  54.  
  55.     public static void loadData(JPanel panel) throws SQLException {
  56.  
  57.         Connection connection = DriverManager.getConnection("jdbc:sqlite:tiobe.db");
  58.         Statement statement = connection.createStatement();
  59.         String query = "SELECT * FROM Ranking";
  60.         ResultSet resultSet = statement.executeQuery(query);
  61.         ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
  62.  
  63.         int columnCount = resultSetMetaData.getColumnCount();
  64.  
  65.         String[] columnNames = new String[columnCount];
  66.  
  67.         for(int i = 1; i <= columnCount; ++i){
  68.             String columnName = resultSetMetaData.getColumnName(i);
  69.             columnNames[i - 1] = columnName;
  70.             System.out.printf("%20s", columnName);
  71.         }
  72.  
  73.         System.out.println();
  74.         System.out.println();
  75.  
  76.         String[][] columnsData = {};
  77.  
  78.         DefaultTableModel model = new DefaultTableModel(columnsData, columnNames);
  79.  
  80.         JTable table = new JTable(model);
  81.  
  82.         // Clear all components and add a new Table
  83.         panel.removeAll();
  84.         panel.add(new JScrollPane(table));
  85.         SwingUtilities.updateComponentTreeUI(panel);
  86.  
  87.         while (resultSet.next()){
  88.             ArrayList<String> data = new ArrayList<>();
  89.             for(int i = 1; i <= columnCount; ++i){
  90.                 String columnData = resultSet.getString(i);
  91.                 data.add(columnData);
  92.                 System.out.printf("%20s", columnData);
  93.             }
  94.             model.addRow(data.toArray());
  95.             System.out.println();
  96.         }
  97.  
  98.         resultSet.close();
  99.         statement.close();
  100.         connection.close();
  101.     }
  102.  
  103.     public static void saveData(JPanel panel) throws SQLException {
  104.         JTable table = getTableFromPane(panel);
  105.         if(table == null){
  106.             return;
  107.         }
  108.  
  109.         Connection connection = DriverManager.getConnection("jdbc:sqlite:tiobe.db");
  110.  
  111.         ArrayList<String> columnNames = new ArrayList<>();
  112.         for(int i = 0; i < table.getColumnCount(); ++i) {
  113.             columnNames.add(table.getColumnName(i));
  114.         }
  115.  
  116.         String tableColumns = columnNames.toString().replace("[", "(").replace("]", ")");
  117.  
  118.         String bindData = "(";
  119.         for(int i = 0; i < columnNames.size() - 1; ++i){
  120.             bindData += "?, ";
  121.         }
  122.         if(columnNames.size() > 0){
  123.             bindData += "?";
  124.         }
  125.         bindData += ")";
  126.  
  127.  
  128.         for(int i = 0; i < table.getRowCount(); ++i){
  129.             ArrayList<String> data = new ArrayList<>();
  130.  
  131.             String query = "UPDATE Ranking SET " + tableColumns + " = " + bindData + " WHERE rowid=" + (i+1);
  132.             PreparedStatement preparedStatement = connection.prepareStatement(query);
  133.  
  134.             for(int j = 0; j < table.getColumnCount(); ++j){
  135.                 preparedStatement.setString(j+1, table.getValueAt(i, j).toString());
  136.             }
  137.             preparedStatement.addBatch();
  138.             preparedStatement.executeBatch();
  139.  
  140.         }
  141.  
  142.         connection.close();
  143.     }
  144.  
  145.     public static JTable getTableFromPane(JComponent panel){
  146.         Component[] components = panel.getComponents();
  147.  
  148.         for(Component c : components){
  149.             if(c instanceof JTable) {
  150.                 return (JTable) c;
  151.             }
  152.             if(c instanceof Container){
  153.                 JTable comp = getTableFromPane((JComponent) c);
  154.                 if(comp != null){
  155.                     return comp;
  156.                 }
  157.             }
  158.         }
  159.         return null;
  160.     }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement