Advertisement
Guest User

Untitled

a guest
Jan 12th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. public class Gui extends JFrame {
  2. private static Connector conni;
  3. private Connection conn = null;
  4. private JButton bt;
  5. private JPanel panel;
  6.  
  7. public Gui() {
  8. super("Frame");
  9. panel = new JPanel();
  10. bt = new JButton("Connect to Database 'World'");
  11. panel.add(bt);
  12. bt.addActionListener(new ActionListener() {
  13.  
  14. @Override
  15. public void actionPerformed(ActionEvent e) {
  16.  
  17. conn = conni.Connector();
  18.  
  19. if (conn != null) {
  20. dispose();
  21. new Gui2(conn);
  22.  
  23. } else {
  24. System.out.println("Return false");
  25.  
  26. }
  27.  
  28. }
  29.  
  30. });
  31. add(panel);
  32.  
  33. pack();
  34. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35. setLocationRelativeTo(null);
  36. setVisible(true);
  37.  
  38. }
  39.  
  40. }
  41.  
  42. public class Connector {
  43.  
  44. private static Connection conn = null;
  45.  
  46. public static Connection Connector() {
  47. String data = "jdbc:mysql://localhost/world";
  48. String user = "root";
  49. String pass = "toot";
  50. try {
  51. conn = DriverManager.getConnection(data, user, pass);
  52.  
  53. } catch (Exception e) {
  54.  
  55. JOptionPane.showMessageDialog(null, e.getMessage());
  56.  
  57. }
  58. if (conn != null) {
  59.  
  60. System.out.println("Connection Suceess");
  61. return conn;
  62.  
  63. } else {
  64.  
  65. return conn;
  66.  
  67. }
  68.  
  69. }
  70.  
  71. }
  72.  
  73. public class Gui2 extends JFrame {
  74. private Statement state = null;
  75. private ResultSet rs = null;
  76.  
  77. private JButton bt, delete;
  78. private JTextField text;
  79. private JPanel panel;
  80. private GridBagLayout layout;
  81. private GridBagConstraints constraints;
  82.  
  83. public Gui2(Connection conn) {
  84. layout = new GridBagLayout();
  85. constraints = new GridBagConstraints();
  86. panel = new JPanel();
  87. panel.setLayout(layout);
  88.  
  89. text = new JTextField(15);
  90. bt = new JButton("Submit Query");
  91. delete = new JButton("Delete Selected Row");
  92. constraints.insets = new Insets(5, 2, 5, 10);
  93. constraints.gridy = 0;// row 0
  94. constraints.gridx = 0;// column 0
  95. // TextField add on JPanel with given constraints
  96. panel.add(text, constraints);
  97. constraints.gridx++;
  98. panel.add(delete, constraints);
  99. constraints.gridx++;
  100. panel.add(bt, constraints);
  101.  
  102. // North BorderLayout
  103. add(panel, BorderLayout.NORTH);
  104.  
  105. try {
  106. state = conn.createStatement();
  107. rs = state.executeQuery("select * from city");
  108. } catch (SQLException e) {
  109.  
  110. JOptionPane.showMessageDialog(null, e.getMessage());
  111. }
  112.  
  113. JTable table = new JTable();
  114. JScrollPane spane = new JScrollPane(table);
  115.  
  116. add(spane, BorderLayout.CENTER);
  117.  
  118. table.setModel(new TableModel(rs));
  119.  
  120. delete.addActionListener(new ActionListener() {
  121.  
  122. @Override
  123. public void actionPerformed(ActionEvent e) {
  124.  
  125. int rowIndex = table.getSelectedRow();
  126.  
  127. Object columnIndexValue = table.getModel().getValueAt(rowIndex, 0);
  128.  
  129. String columnName = table.getModel().getColumnName(0);
  130.  
  131. String query = "delete from world.city" + " where " + columnName + "=" + columnIndexValue;
  132.  
  133. try {
  134.  
  135. PreparedStatement pre = conn.prepareStatement(query);
  136.  
  137. pre.executeUpdate();
  138.  
  139. JOptionPane.showMessageDialog(null, "Row Deleted Successfully");
  140. } catch (Exception e1) {
  141. JOptionPane.showMessageDialog(null, e1.getMessage());
  142. }
  143.  
  144. }
  145.  
  146. });
  147.  
  148. setSize(817, 538);
  149. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  150. setLocationRelativeTo(null);
  151. setVisible(true);
  152.  
  153. }
  154.  
  155. }
  156.  
  157. public class TableModel extends AbstractTableModel {
  158.  
  159. private List ColumnHeader;
  160. private List tableData;
  161.  
  162. public TableModel(ResultSet rs) {
  163.  
  164. List rowData;
  165.  
  166. try {
  167.  
  168. ResultSetMetaData meta = rs.getMetaData();
  169.  
  170. int totalcolumn = meta.getColumnCount();
  171.  
  172. ColumnHeader = new ArrayList(totalcolumn);
  173.  
  174. tableData = new ArrayList();
  175.  
  176. for (int i = 1; i <= totalcolumn; i++) {
  177. ColumnHeader.add(meta.getColumnName(i));
  178.  
  179. }
  180.  
  181. while (rs.next()) {
  182.  
  183. rowData = new ArrayList(totalcolumn);
  184.  
  185. for (int i = 1; i <= totalcolumn; i++) {
  186.  
  187. rowData.add(rs.getObject(i));
  188. }
  189. tableData.add(rowData);
  190.  
  191. }
  192.  
  193. } catch (Exception e) {
  194.  
  195. JOptionPane.showMessageDialog(null, e.getMessage());
  196. }
  197.  
  198. }
  199.  
  200. @Override
  201. public int getColumnCount() {
  202.  
  203. return ColumnHeader.size();
  204. }
  205.  
  206. @Override
  207. public int getRowCount() {
  208.  
  209. return tableData.size();
  210. }
  211.  
  212. @Override
  213. public Object getValueAt(int rowIndex, int columnIndex) {
  214.  
  215. List rowData2 = (List) tableData.get(rowIndex);
  216.  
  217. return rowData2.get(columnIndex);
  218. }
  219.  
  220. public String getColumnName(int columnIndex) {
  221. return (String) ColumnHeader.get(columnIndex);
  222.  
  223. }
  224.  
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement