Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 KB | None | 0 0
  1. package WypozyczalniaSamochodow;
  2.  
  3.  
  4. import java.awt.Color;
  5. import java.awt.Font;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.MouseAdapter;
  9. import java.awt.event.MouseEvent;
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.sql.Statement;
  16.  
  17. import javax.swing.JButton;
  18. import javax.swing.JFrame;
  19. import javax.swing.JScrollPane;
  20. import javax.swing.JTable;
  21. import javax.swing.JTextField;
  22. import javax.swing.table.DefaultTableModel;
  23.  
  24. public class JTableRow {
  25.  
  26. public static void main(String[] args){
  27.  
  28.  
  29.  
  30. // create JFrame and JTable
  31. JFrame frame = new JFrame();
  32. JTable table = new JTable();
  33.  
  34. // create a table model and set a Column Identifiers to this model
  35. Object[] columns = {"Marka","Typ","Rocznik","Przebieg", "Dostępność"};
  36. DefaultTableModel model = new DefaultTableModel();
  37. model.setColumnIdentifiers(columns);
  38.  
  39. // set the model to the table
  40. table.setModel(model);
  41.  
  42. // Change A JTable Background Color, Font Size, Font Color, Row Height
  43. table.setBackground(Color.LIGHT_GRAY);
  44. table.setForeground(Color.black);
  45. Font font = new Font("",1,22);
  46. table.setFont(font);
  47. table.setRowHeight(30);
  48.  
  49. // create JTextFields
  50. JTextField textId = new JTextField();
  51. JTextField textFname = new JTextField();
  52. JTextField textLname = new JTextField();
  53. JTextField textAge = new JTextField();
  54. JTextField txtAge = new JTextField();
  55.  
  56. // create JButtons
  57. JButton btnAdd = new JButton("Add");
  58. JButton btnDelete = new JButton("Delete");
  59. JButton btnUpdate = new JButton("Update");
  60.  
  61. textId.setBounds(20, 220, 100, 25);
  62. textFname.setBounds(20, 250, 100, 25);
  63. textLname.setBounds(20, 280, 100, 25);
  64. textAge.setBounds(20, 310, 100, 25);
  65. txtAge.setBounds(20, 340, 100, 25);
  66.  
  67. btnAdd.setBounds(150, 220, 100, 25);
  68. btnUpdate.setBounds(150, 265, 100, 25);
  69. btnDelete.setBounds(150, 310, 100, 25);
  70.  
  71. // create JScrollPane
  72. JScrollPane pane = new JScrollPane(table);
  73. pane.setBounds(0, 0, 880, 200);
  74.  
  75. frame.setLayout(null);
  76.  
  77. frame.add(pane);
  78.  
  79. // add JTextFields to the jframe
  80. frame.add(textId);
  81. frame.add(textFname);
  82. frame.add(textLname);
  83. frame.add(textAge);
  84. frame.add(txtAge);
  85.  
  86. // add JButtons to the jframe
  87. frame.add(btnAdd);
  88. frame.add(btnDelete);
  89. frame.add(btnUpdate);
  90.  
  91. // create an array of objects to set the row data
  92. Object[] row = new Object[5];
  93.  
  94. // button add row
  95. btnAdd.addActionListener(new ActionListener(){
  96.  
  97. @Override
  98. public void actionPerformed(ActionEvent e) {
  99.  
  100. row[0] = textId.getText();
  101. row[1] = textFname.getText();
  102. row[2] = textLname.getText();
  103. row[3] = textAge.getText();
  104. row[4] = txtAge.getText();
  105.  
  106. // add row to the model
  107. model.addRow(row);
  108.  
  109. String marka = textId.getText();
  110. String typ = textFname.getText();
  111. String rocznik = textLname.getText();
  112. String przebieg = textAge.getText();
  113. String dostepnosc= txtAge.getText();
  114. try {
  115. // 1. Get connection to database
  116. Connection myConn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "Warszawa as sysdba", "Bkbrd1934");
  117. //2. Create a statement
  118. Statement myStmt = myConn.createStatement();
  119. //3. Execute SQL query
  120. String doddaj = "INSERT INTO AASAMOCHOD (MARKA, TYP, ROCZNIK, PRZEBIEG, DOSTEPNOSC)"
  121. + " values (?, ?, ?, ?, ?)";
  122.  
  123. PreparedStatement preparedStmt = myConn.prepareStatement(doddaj);
  124. preparedStmt.setString(1, marka);
  125. preparedStmt.setString(2, typ);
  126. preparedStmt.setString(3, rocznik);
  127. preparedStmt.setString(4, przebieg);
  128. preparedStmt.setString(5, dostepnosc);
  129.  
  130. preparedStmt.execute();
  131.  
  132. }
  133. catch (Exception exc) {
  134. exc.printStackTrace();
  135. }
  136.  
  137. }
  138. });
  139.  
  140. // button remove row
  141. btnDelete.addActionListener(new ActionListener(){
  142.  
  143. @Override
  144. public void actionPerformed(ActionEvent e) {
  145.  
  146. // i = the index of the selected row
  147. int i = table.getSelectedRow();
  148. if(i >= 0){
  149. // remove a row from jtable
  150. model.removeRow(i);
  151.  
  152. String marka = textId.getText();
  153.  
  154. try {
  155. // 1. Get connection to database
  156. Connection myConn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "Warszawa as sysdba", "Bkbrd1934");
  157. //2. Create a statement
  158. //Statement myStmt = myConn.createStatement();
  159. //3. Execute SQL query
  160. String doddaj = "DELETE FROM AASAMOCHOD WHERE"
  161. + "MARKA = ?;";
  162.  
  163.  
  164. PreparedStatement preparedStmt = myConn.prepareStatement(doddaj);
  165. preparedStmt.setString(1, marka);
  166. preparedStmt.executeUpdate();
  167.  
  168. }
  169. catch (Exception exc) {
  170. exc.printStackTrace();
  171. }
  172.  
  173. }
  174. else{
  175. System.out.println("Delete Error");
  176. }
  177. }
  178.  
  179.  
  180.  
  181. });
  182.  
  183. // get selected row data From table to textfields
  184. table.addMouseListener(new MouseAdapter(){
  185.  
  186. @Override
  187. public void mouseClicked(MouseEvent e){
  188.  
  189. // i = the index of the selected row
  190. int i = table.getSelectedRow();
  191.  
  192. textId.setText(model.getValueAt(i, 0).toString());
  193. textFname.setText(model.getValueAt(i, 1).toString());
  194. textLname.setText(model.getValueAt(i, 2).toString());
  195. textAge.setText(model.getValueAt(i, 3).toString());
  196. txtAge.setText(model.getValueAt(i, 4).toString());
  197. }
  198. });
  199.  
  200. // button update row
  201. btnUpdate.addActionListener(new ActionListener(){
  202.  
  203. @Override
  204. public void actionPerformed(ActionEvent e) {
  205.  
  206. // i = the index of the selected row
  207. int i = table.getSelectedRow();
  208.  
  209. if(i >= 0)
  210. {
  211. model.setValueAt(textId.getText(), i, 0);
  212. model.setValueAt(textFname.getText(), i, 1);
  213. model.setValueAt(textLname.getText(), i, 2);
  214. model.setValueAt(textAge.getText(), i, 3);
  215. model.setValueAt(txtAge.getText(), i, 4);
  216. }
  217. else{
  218. System.out.println("Update Error");
  219. }
  220. }
  221. });
  222.  
  223. frame.setSize(900,430);
  224. frame.setLocationRelativeTo(null);
  225. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  226. frame.setVisible(true);
  227.  
  228. // String imie, nazwisko, miasto, nrkarty;
  229. //
  230. // try {
  231. // // 1. Get connection to database
  232. // Connection myConn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "Warszawa as sysdba", "Bkbrd1934");
  233. // //2. Create a statement
  234. // Statement myStmt = myConn.createStatement();
  235. // //3. Execute SQL query
  236. // ResultSet myRs = myStmt.executeQuery("SELECT * FROM AAKLIENT");
  237. // //4. Process the result set
  238. // while(myRs.next())
  239. // {
  240. // imie = myRs.getString("MARKA");
  241. // nazwisko = myRs.getString("TYP");
  242. // miasto = myRs.getString("MIASTO");
  243. // nrkarty = myRs.getString("NRKARTY");
  244. // }
  245. //
  246. // row[0] = imie;
  247. // row[1] = textFname.getText();
  248. // row[2] = textLname.getText();
  249. // row[3] = textAge.getText();
  250. // row[4] = txtAge.getText();
  251. //
  252. //
  253. // // add row to the model
  254. // model.addRow(row);
  255. //
  256. // }
  257. //
  258. // catch(SQLException e){
  259. //
  260. // }
  261. }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement