Advertisement
Guest User

Untitled

a guest
Aug 18th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Component;
  3. import java.awt.Container;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.ResultSetMetaData;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.util.ArrayList;
  13. import java.util.Vector;
  14.  
  15. import javax.swing.JTable;
  16. import javax.swing.SwingUtilities;
  17. import javax.swing.event.TableModelEvent;
  18. import javax.swing.event.TableModelListener;
  19. import javax.swing.JButton;
  20. import javax.swing.JFrame;
  21. import javax.swing.JScrollPane;
  22. import javax.swing.table.DefaultTableModel;
  23.  
  24. import com.mysql.jdbc.PreparedStatement;
  25.  
  26. public class PreviewButton
  27. {
  28. public JButton Preview = new JButton("Preview"); //Creates new JButton that can be accessed by other classes
  29. public JButton SaveButton = new JButton("Save");
  30. public JTable table = new JTable();
  31. DefaultTableModel tableModel = new DefaultTableModel(); //Creates new default table so tableModel resets on opening a new table.
  32. public DefaultTableModel changedTableModel = new DefaultTableModel();
  33. public PreviewButton(MainFrame mainFrame)
  34. {
  35. Preview.addActionListener(new ActionListener()
  36. {
  37. public void actionPerformed(ActionEvent e) {
  38. JFrame previewframe = new JFrame("Preview Window");
  39. previewframe.setVisible(true);
  40. previewframe.setSize(500, 500);
  41. try (Connection PreviewCon = DriverManager.getConnection("jdbc:mysql://somewhere:port/schema","user","pass");
  42. Statement stmt = PreviewCon.createStatement(); //creates statement "stmt" which holds connection from above and allows me to write sql statement
  43. ResultSet rstables = stmt.executeQuery("SELECT * FROM " + mainFrame.SelectedTable))
  44. //create result set that selects all the data from the table name held in SelectedTable
  45. {
  46. ResultSetMetaData metaData = rstables.getMetaData(); //Gets meta data from result set of SelectedTable
  47. int columnCount = metaData.getColumnCount(); //Get number of columns from meta data
  48. for (int columnIndex = 1; columnIndex <= columnCount;columnIndex++) //Get all columns names from meta data
  49. {
  50. tableModel.addColumn(metaData.getColumnLabel(columnIndex)); //Adds column names to table model
  51. }
  52. Object [] row = new Object[columnCount]; //Creates an array with the size of the column count
  53. while (rstables.next())
  54. {
  55. for (int i = 0; i < columnCount; i++)
  56. {
  57. row[i] = rstables.getObject(i+1); //Gets object from the column with the index of the result set
  58. }
  59. tableModel.addRow(row); // Adds the row to the table model
  60. }
  61. table.setModel(tableModel); // Adds the table model to the JTable
  62. previewframe.add(table);
  63. previewframe.add(SaveButton, BorderLayout.SOUTH);
  64. }
  65. catch (SQLException p)
  66. {
  67. p.printStackTrace();
  68. }
  69.  
  70. }
  71.  
  72. });
  73. }
  74. public void TableChange(MainFrame mainFrame)
  75. {
  76. int column = 0;
  77. int row = 0;
  78. table.getModel().addTableModelListener(new TableModelListener() //adds tablemodellistener to the tablemodel from above
  79. {
  80. public void tableChanged(TableModelEvent t)//checks if the tablemodel has been changed
  81. {
  82. Vector<Vector<String>> data = new Vector<Vector<String>>(); //creates new vector that holds a string called data
  83. for (int column = 0; column < table.getModel().getColumnCount(); column++) //loops through the columns of tableModel starting with column 0
  84. {
  85. for (int row = 0; row<table.getModel().getRowCount(); row++) //loops through the rows of tableModel starting with 0
  86. {
  87. data.addElement((Vector<String>) table.getModel().getValueAt(row, column)); //adds the value of tableModel at 0,0 to data
  88. }
  89. changedTableModel.addRow(data); // adds the row of data from the data vector to another tableModel
  90. }
  91. System.out.println(data);
  92. System.out.println(column);
  93. System.out.println(row);
  94. }
  95. });
  96. }
  97. public void SaveButton(MainFrame mainFrame)
  98. {
  99. SaveButton.addActionListener(new ActionListener()
  100. {
  101. public void actionPerformed(ActionEvent s)
  102. {
  103. Statement TruncateTable = null; //instantiates the statement "TruncateTalbe" with value null
  104. try (Connection SaveCon = DriverManager.getConnection("jdbc:mysql://somewhere:port/schema","user","pass");
  105. Statement stmt = SaveCon.createStatement(); //creates statement "stmt" which holds connection from above and allows me to write sql statement
  106. ResultSet rstables = stmt.executeQuery("SELECT * FROM " + mainFrame.SelectedTable)) //get result set from all data from selectedtable
  107. {
  108. TruncateTable = SaveCon.createStatement();
  109. TruncateTable.executeUpdate("TRUNCATE TABLE " + mainFrame.SelectedTable); //executes a truncate on selectedtable
  110. java.sql.PreparedStatement InsertTable = SaveCon.prepareStatement("INSERT INTO " +mainFrame.SelectedTable+" VALUES(?,?,?)"); //prepare statement for inserting values into the now empty selected table
  111. for (int column = 0; column < changedTableModel.getColumnCount(); column++) //loops through changedTableModel columns start 0
  112. {
  113. for (int row = 0; row<changedTableModel.getRowCount(); row++) //loops through changedTableModel rows start with 0
  114. {
  115. String DataID = (String) changedTableModel.getValueAt(row, column); //create new object "o" that holds the value of changedTableModel at 0,0
  116. InsertTable.setString(column+1, DataID);
  117. System.out.println(DataID);
  118. }
  119. InsertTable.executeUpdate(); //execute InsertTable statement
  120. InsertTable.clearParameters();
  121. }
  122. }
  123. catch (SQLException e)
  124. {
  125. e.printStackTrace();
  126. }
  127. }
  128. });
  129. }
  130.  
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement