Advertisement
Guest User

SearchResult.java

a guest
Oct 29th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. package net.roseindia.jtableExample;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.table.DefaultTableModel;
  5.  
  6. import java.awt.*;
  7. import java.sql.*;
  8. import java.awt.event.*;
  9.  
  10. public class SearchResult implements ActionListener{
  11. JFrame frame, frame1;
  12. JTextField textbox;
  13. JLabel label;
  14. JButton button;
  15. JPanel panel;
  16. static JTable table;
  17.  
  18. String driverName = "com.mysql.jdbc.Driver";
  19. String url = "jdbc:mysql://localhost:3306/record";
  20. String userName = "root";
  21. String password = "root";
  22. String[] columnNames = {"Roll No", "Name", "Class", "Section"};
  23.  
  24. public void createUI()
  25. {
  26. frame = new JFrame("Database Search Result");
  27. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28. frame.setLayout(null);
  29. textbox = new JTextField();
  30. textbox.setBounds(120,30,150,20);
  31. label = new JLabel("Enter your roll no");
  32. label.setBounds(10, 30, 100, 20);
  33. button = new JButton("search");
  34. button.setBounds(120,130,150,20);
  35. button.addActionListener(this);
  36.  
  37. frame.add(textbox);
  38. frame.add(label);
  39. frame.add(button);
  40. frame.setVisible(true);
  41. frame.setSize(500, 400);
  42. }
  43.  
  44. public void actionPerformed(ActionEvent ae)
  45. {
  46. button = (JButton)ae.getSource();
  47. System.out.println("Showing Table Data.......");
  48. showTableData();
  49. }
  50.  
  51. public void showTableData()
  52. {
  53.  
  54. frame1 = new JFrame("Database Search Result");
  55. frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  56. frame1.setLayout(new BorderLayout());
  57. //TableModel tm = new TableModel();
  58. DefaultTableModel model = new DefaultTableModel();
  59. model.setColumnIdentifiers(columnNames);
  60. //DefaultTableModel model = new DefaultTableModel(tm.getData1(), tm.getColumnNames());
  61. //table = new JTable(model);
  62. table = new JTable();
  63. table.setModel(model);
  64. table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  65. table.setFillsViewportHeight(true);
  66. JScrollPane scroll = new JScrollPane(table);
  67. scroll.setHorizontalScrollBarPolicy(
  68. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  69. scroll.setVerticalScrollBarPolicy(
  70. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
  71. String textvalue = textbox.getText();
  72. String roll= "";
  73. String name= "";
  74. String cl = "";
  75. String sec = "";
  76. try
  77. {
  78. Class.forName(driverName);
  79. Connection con = DriverManager.getConnection(url, userName, password);
  80. String sql = "select * from student where rollno = "+textvalue;
  81. PreparedStatement ps = con.prepareStatement(sql);
  82. ResultSet rs = ps.executeQuery();
  83. int i =0;
  84. if(rs.next())
  85. {
  86. roll = rs.getString("rollno");
  87. name = rs.getString("name");
  88. cl = rs.getString("class");
  89. sec = rs.getString("section");
  90. model.addRow(new Object[]{roll, name, cl, sec});
  91. i++;
  92. }
  93. if(i <1)
  94. {
  95. JOptionPane.showMessageDialog(null, "No Record Found","Error",
  96. JOptionPane.ERROR_MESSAGE);
  97. }
  98. if(i ==1)
  99. {
  100. System.out.println(i+" Record Found");
  101. }
  102. else
  103. {
  104. System.out.println(i+" Records Found");
  105. }
  106. }
  107. catch(Exception ex)
  108. {
  109. JOptionPane.showMessageDialog(null, ex.getMessage(),"Error",
  110. JOptionPane.ERROR_MESSAGE);
  111. }
  112. frame1.add(scroll);
  113. frame1.setVisible(true);
  114. frame1.setSize(400,300);
  115. }
  116.  
  117. public static void main(String args[])
  118. {
  119. SearchResult sr = new SearchResult();
  120. sr.createUI();
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement