Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.vasile2k.databaze;
- import javax.swing.*;
- import javax.swing.table.DefaultTableModel;
- import java.awt.*;
- import java.sql.*;
- import java.util.ArrayList;
- public class Databazuitor {
- public static void main(String... args){
- try {
- Class.forName("org.sqlite.JDBC");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- JFrame frame = new JFrame("Bazie Die Datie");
- frame.setLocation(200, 200);
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
- JPanel panel = new JPanel();
- panel.setPreferredSize(new Dimension(1280, 720));
- JButton loadButton = new JButton("Load shit");
- loadButton.addActionListener(e -> {
- try {
- loadData(panel);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- });
- JButton saveButton = new JButton("Save shit");
- saveButton.addActionListener(e -> {
- try {
- saveData(panel);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- });
- JPanel buttonPanel = new JPanel();
- buttonPanel.add(loadButton);
- buttonPanel.add(saveButton);
- frame.add(panel);
- frame.add(buttonPanel);
- frame.pack();
- frame.setVisible(true);
- }
- public static void loadData(JPanel panel) throws SQLException {
- Connection connection = DriverManager.getConnection("jdbc:sqlite:tiobe.db");
- Statement statement = connection.createStatement();
- String query = "SELECT * FROM Ranking";
- ResultSet resultSet = statement.executeQuery(query);
- ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
- int columnCount = resultSetMetaData.getColumnCount();
- String[] columnNames = new String[columnCount];
- for(int i = 1; i <= columnCount; ++i){
- String columnName = resultSetMetaData.getColumnName(i);
- columnNames[i - 1] = columnName;
- System.out.printf("%20s", columnName);
- }
- System.out.println();
- System.out.println();
- String[][] columnsData = {};
- DefaultTableModel model = new DefaultTableModel(columnsData, columnNames);
- JTable table = new JTable(model);
- // Clear all components and add a new Table
- panel.removeAll();
- panel.add(new JScrollPane(table));
- SwingUtilities.updateComponentTreeUI(panel);
- while (resultSet.next()){
- ArrayList<String> data = new ArrayList<>();
- for(int i = 1; i <= columnCount; ++i){
- String columnData = resultSet.getString(i);
- data.add(columnData);
- System.out.printf("%20s", columnData);
- }
- model.addRow(data.toArray());
- System.out.println();
- }
- resultSet.close();
- statement.close();
- connection.close();
- }
- public static void saveData(JPanel panel) throws SQLException {
- JTable table = getTableFromPane(panel);
- if(table == null){
- return;
- }
- Connection connection = DriverManager.getConnection("jdbc:sqlite:tiobe.db");
- ArrayList<String> columnNames = new ArrayList<>();
- for(int i = 0; i < table.getColumnCount(); ++i) {
- columnNames.add(table.getColumnName(i));
- }
- String tableColumns = columnNames.toString().replace("[", "(").replace("]", ")");
- String bindData = "(";
- for(int i = 0; i < columnNames.size() - 1; ++i){
- bindData += "?, ";
- }
- if(columnNames.size() > 0){
- bindData += "?";
- }
- bindData += ")";
- for(int i = 0; i < table.getRowCount(); ++i){
- ArrayList<String> data = new ArrayList<>();
- String query = "UPDATE Ranking SET " + tableColumns + " = " + bindData + " WHERE rowid=" + (i+1);
- PreparedStatement preparedStatement = connection.prepareStatement(query);
- for(int j = 0; j < table.getColumnCount(); ++j){
- preparedStatement.setString(j+1, table.getValueAt(i, j).toString());
- }
- preparedStatement.addBatch();
- preparedStatement.executeBatch();
- }
- connection.close();
- }
- public static JTable getTableFromPane(JComponent panel){
- Component[] components = panel.getComponents();
- for(Component c : components){
- if(c instanceof JTable) {
- return (JTable) c;
- }
- if(c instanceof Container){
- JTable comp = getTableFromPane((JComponent) c);
- if(comp != null){
- return comp;
- }
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement