Advertisement
Guest User

Untitled

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