Advertisement
Guest User

Untitled

a guest
May 14th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package vaja8_02_netbeans;
  7.  
  8. /**
  9. *
  10. * @author Vid
  11. */
  12.  
  13. import java.sql.*;
  14. import java.awt.GridLayout;
  15. import java.awt.event.ActionEvent;
  16. import javax.swing.AbstractAction;
  17. import javax.swing.JButton;
  18. import javax.swing.JFrame;
  19. import javax.swing.JLabel;
  20. import javax.swing.JPanel;
  21. import javax.swing.JTextArea;
  22. import javax.swing.JTextField;
  23. import java.awt.BorderLayout;
  24. import java.awt.Container;
  25. import java.awt.Dimension;
  26. import java.awt.FlowLayout;
  27. import java.awt.event.ActionListener;
  28. import java.util.Arrays;
  29. import javax.swing.JScrollPane;
  30. import javax.swing.JTable;
  31.  
  32. public class NALOGA1 {
  33. static String[][] data;
  34. static String[] stolpci;
  35. /**
  36. * @param args the command line arguments
  37. * @throws java.sql.SQLException
  38. */
  39. public static void main(String[] args) throws SQLException {
  40.  
  41. Connection povezava = DriverManager.getConnection("jdbc:mysql://localhost:3306/phonebook?zeroDateTimeBehavior=convertToNull", "mojuser", "test");
  42. Statement stavek = povezava.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
  43.  
  44. JFrame okvir = new JFrame("Vnosna forma");
  45. okvir.setSize(860, 300);
  46. okvir.setLayout(new GridLayout(4,0));
  47. okvir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  48.  
  49.  
  50. Container neki = okvir.getContentPane();
  51. JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
  52. JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
  53. JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.CENTER));
  54. neki.add(panel1, BorderLayout.LINE_START);
  55. neki.add(panel2, BorderLayout.LINE_START);
  56. neki.add(panel3, BorderLayout.LINE_START);
  57.  
  58. JTextField box = new JTextField(75);
  59. panel1.add(box);
  60.  
  61. JButton gumb = new JButton(new AbstractAction("IZVEDI UKAZ"){
  62.  
  63.  
  64.  
  65. @Override
  66. public void actionPerformed(ActionEvent e){
  67. try{
  68. ResultSet rezultati = stavek.executeQuery(box.getText());
  69. ResultSetMetaData meta = rezultati.getMetaData();
  70. int j = prestejVrstice(rezultati);
  71. int k = meta.getColumnCount();
  72.  
  73.  
  74. stolpci = new String[k];
  75. for(int i = 0; i < k; i++){
  76. stolpci[i] = meta.getColumnName(i);
  77. }
  78. System.out.println(Arrays.toString(stolpci));
  79. data = new String[j][k];
  80. int vrst = 0;
  81. while(rezultati.next()){
  82. for(int x = 0; x < k; x++){
  83. data[vrst][x] = String.valueOf(rezultati.getObject(x));
  84. }
  85. vrst++;
  86. }
  87. System.out.println(Arrays.toString(data[0]));
  88. } catch (SQLException s){
  89. }
  90. JTable tabela = new JTable(data, stolpci);
  91. JScrollPane pane = new JScrollPane(tabela);
  92. panel3.add(pane);
  93. }
  94.  
  95. });
  96.  
  97.  
  98.  
  99.  
  100.  
  101. panel2.add(gumb);
  102.  
  103. okvir.setVisible(true);
  104.  
  105.  
  106.  
  107. }
  108. static int prestejVrstice(ResultSet a) throws SQLException{
  109.  
  110. a.last();
  111. int size = a.getRow();
  112. return size;
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement