Guest User

Untitled

a guest
Dec 29th, 2016
83
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.Dimension;
  2. import java.awt.EventQueue;
  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;
  5. import javax.swing.JScrollPane;
  6.  
  7. import java.awt.Font;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.util.ArrayList;
  14. import javax.swing.JTable;
  15. import javax.swing.table.DefaultTableModel;
  16.  
  17.  
  18. public class GlasaneAnkete {
  19.  
  20. // Definisi JDBC driver name i URL baze
  21. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  22. static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/SurveyDB?verifyServerCertificate=false&useSSL=false";
  23.  
  24. // Db podaci
  25. static final String USER = "root";
  26. static final String PASS = "123456";
  27.  
  28. ArrayList<UserSurvey> userSurveys = new ArrayList<>();
  29. private JFrame frame;
  30. private JTable table;
  31. //
  32. /**
  33. * Launch the application.
  34. */
  35. public void GlasaneA() {
  36. EventQueue.invokeLater(new Runnable() {
  37. public void run() {
  38. try {
  39. GlasaneAnkete window = new GlasaneAnkete();
  40. window.frame.setVisible(true);
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. });
  46. }
  47.  
  48. /**
  49. * Create the application.
  50. */
  51. public GlasaneAnkete() {
  52. initialize();
  53. }
  54.  
  55. /**
  56. * Initialize the contents of the frame.
  57. */
  58.  
  59.  
  60. private void initialize() {
  61. frame = new JFrame();
  62. frame.setResizable(false);
  63. frame.setTitle("Ankete na kojima si glasao");
  64. frame.setBounds(100, 100, 520, 410);
  65. frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  66. frame.getContentPane().setLayout(null);
  67.  
  68. JLabel lblAnketeNaKojima = new JLabel("ANKETE NA KOJIMA SI GLASAO");
  69. lblAnketeNaKojima.setFont(new Font("Gadugi", Font.BOLD, 16));
  70. lblAnketeNaKojima.setBounds(131, 23, 273, 48);
  71. frame.getContentPane().add(lblAnketeNaKojima);
  72.  
  73. Object columnNames[] = { "Pitanje "};
  74. DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
  75.  
  76. //public void DodajuBazu(String s){
  77. Connection conn = null;
  78. PreparedStatement stmt = null;
  79. try {
  80. Login l = new Login();
  81. int userID = l.getId();
  82.  
  83. // Registruj JDBC driver
  84. Class.forName("com.mysql.jdbc.Driver");
  85.  
  86. // Zapocni konekciju conn
  87. conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/SurveyDB?verifyServerCertificate=false&useSSL=false", "root", "123456");
  88. String sqlSelect = "SELECT SA.question_id, SA.user_id, Q.question_text FROM `submited_answers` AS SA INNER JOIN questions AS Q ON SA.question_id = Q.id WHERE user_id = ?";
  89.  
  90. // Napravi statement i izvrsi query
  91. stmt = conn.prepareStatement(sqlSelect);
  92. stmt.setInt(1, userID);
  93. ResultSet rs = stmt.executeQuery();
  94.  
  95. while(rs.next())
  96. {
  97. UserSurvey us = new UserSurvey();
  98. us.questionId = rs.getInt("question_id");
  99. us.questionText = rs.getString("question_text");
  100. us.userID = rs.getInt("user_id");
  101.  
  102. userSurveys.add(us);
  103. }
  104.  
  105. rs.close();
  106. stmt.close();
  107. conn.close();
  108. } catch (SQLException se) {
  109. // Errors JDBC
  110. se.printStackTrace();
  111. }
  112. catch (Exception x) {
  113. // Errors za Class.forName
  114. x.printStackTrace();
  115. }
  116.  
  117. for (int i = 0; i < userSurveys.size(); i++){
  118. String name = userSurveys.get(i).questionText;
  119. Object[] data = {name};
  120. tableModel.addRow(data);
  121. }
  122.  
  123. table = new JTable(tableModel);
  124. table.setEnabled(false);
  125. table.setBounds(10, 104, 494, 266);
  126. //frame.getContentPane().add(table);
  127. //table.setPreferredScrollableViewportSize(new Dimension(450,63));
  128. table.setFillsViewportHeight(true);
  129.  
  130. JScrollPane js=new JScrollPane(table);
  131. js.setEnabled(false);
  132. js.setSize(494, 266);
  133. js.setLocation(10, 104);
  134. js.setVisible(true);
  135. frame.getContentPane().add(js);
  136.  
  137. }
  138. }
RAW Paste Data