Advertisement
Guest User

Untitled

a guest
Dec 30th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import java.awt.*;
  4. import java.sql.*;
  5.  
  6. public class Searchdb extends JFrame implements ActionListener {
  7.  
  8. //Initializing Components
  9. JLabel lb,lbd,lb1, lb2, lb3, lb5;
  10. JTextField tf1, tf2,tf3,tf5,tfd;
  11. JButton btn;
  12. JMenuBar menuBar = new JMenuBar();
  13. JMenu menu = new JMenu("Student");
  14. JMenu menu1 = new JMenu("Teacher");
  15. //Creating Constructor for initializing JFrame components
  16. Searchdb() {
  17. //Providing Title
  18. super("Fetching Roll Information");
  19.  
  20. setJMenuBar(menuBar);
  21. menuBar.add(menu);
  22. menuBar.add(menu1);
  23.  
  24.  
  25. lb5 = new JLabel("Roll Number:");
  26. lb5.setBounds(20, 20, 100, 20);
  27. tf5 = new JTextField(20);
  28. tf5.setBounds(130, 20, 200, 20);
  29.  
  30. lbd = new JLabel("Date:");
  31. lbd.setBounds(20, 50, 100, 20);
  32. tfd = new JTextField(20);
  33. tfd.setBounds(130, 50, 200, 20);
  34.  
  35.  
  36. btn = new JButton("Submit");
  37. btn.setBounds(50, 50, 100, 20);
  38. btn.addActionListener(this);
  39.  
  40. lb = new JLabel("Fetching Student Information From Database");
  41. lb.setBounds(30, 80, 450, 30);
  42. lb.setForeground(Color.black);
  43. lb.setFont(new Font("Serif", Font.PLAIN, 12));
  44. setVisible(true);
  45. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46. setSize(500, 500);
  47.  
  48. lb1 = new JLabel("Name:");
  49. lb1.setBounds(20, 120, 100, 20);
  50. tf1 = new JTextField(50);
  51. tf1.setBounds(130, 120, 200, 20);
  52. lb2 = new JLabel("Fathername:");
  53. lb2.setBounds(20, 150, 100, 20);
  54. tf2 = new JTextField(100);
  55. tf2.setBounds(130, 150, 200, 20);
  56. lb3 = new JLabel("State:");
  57. lb3.setBounds(20, 180, 100, 20);
  58. tf3 = new JTextField(50);
  59. tf3.setBounds(130, 180, 200, 20);
  60.  
  61. setLayout(null);
  62.  
  63. //Add components to the JFrame
  64. add(lb5);
  65. add(tf5);
  66. add(lbd);
  67. add(tfd);
  68. add(btn);
  69.  
  70. add(lb);
  71. add(lb1);
  72. add(tf1);
  73. add(lb2);
  74. add(tf2);
  75. add(lb3);
  76. add(tf3);
  77.  
  78.  
  79. //Set TextField Editable False
  80. tf1.setEditable(false);
  81. tf2.setEditable(false);
  82. tf3.setEditable(false);
  83.  
  84. }
  85.  
  86. public void actionPerformed(ActionEvent e) {
  87. //Create DataBase Coonection and Fetching Records
  88.  
  89. try {
  90. String str = tf5.getText();
  91.  
  92. Datestri = tfd.getText();//Getting the unable to convert String to Date error
  93.  
  94. System.out.println(str);
  95. System.out.println(stri);
  96.  
  97. Class.forName("oracle.jdbc.driver.OracleDriver");
  98. Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//host:port/servicename","username","password");
  99. PreparedStatement st = con.prepareStatement("select Name,Fathername,State from student_db where roll_number=? and medium=?");
  100. System.out.println(st);
  101. st.setString(1, str);
  102. st.setDate(2, stri);
  103.  
  104.  
  105.  
  106. //Excuting Query
  107. ResultSet rs = st.executeQuery();
  108. System.out.println(rs);
  109.  
  110. if (rs.next()) {
  111. String s = rs.getString(1);
  112. String s1 = rs.getString(2);
  113. String s2 = rs.getString(3);
  114.  
  115.  
  116. //Sets Records in TextFields.
  117. tf1.setText(s);
  118. tf2.setText(s1);
  119. tf3.setText(s2);
  120.  
  121. } else {
  122. JOptionPane.showMessageDialog(null, "Student not Found");
  123. }
  124.  
  125. //Create Exception Handler
  126. } catch (Exception ex) {
  127.  
  128. System.out.println(ex);
  129. }
  130. }
  131.  
  132. public void actionPerformed(ActionEvent e) {
  133. //Create DataBase Coonection and Fetching Records
  134.  
  135. //Teacher information should be retrieved from the db
  136.  
  137. }
  138.  
  139. //Running Constructor
  140.  
  141. public static void main(String args[]) {
  142. new Searchdb();
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement