943,648 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1050
  • Java RSS
Aug 27th, 2007
0

Please help me in this coding

Expand Post »
I want to display result when a student enter their rollno in a text box and click on the submit button.
I know there are errors as i have tried many times before and each time i have changed it so
Please help me in locating errors.I need it early.
Thanks.

Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.TextField;
  3. import java.awt.Label;
  4. import java.awt.Button;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.*;
  8. import java.sql.Connection;
  9. import java.sql.*;
  10. import java.sql.DriverManager;
  11. import java.sql.Statement;
  12.  
  13. class res extends Frame implements ActionListener
  14.  
  15. {
  16. Connection con;
  17. Statement st;
  18. ResultSet rs;
  19.  
  20. List lP1=new List();
  21. List lP2=new List();
  22. List lP3=new List();
  23. List lP4=new List();
  24. List lP5=new List();
  25.  
  26. Label res=new Label("Result");
  27.  
  28. Label P1=new Label("P1");
  29. Label P2=new Label("P2");
  30. Label P3=new Label("P3");
  31. Label P4=new Label("P4");
  32. Label P5=new Label("P5");
  33.  
  34.  
  35. Button b1=new Button("Submit");
  36. Button b2=new Button("Cancel");
  37.  
  38.  
  39. TextField msg=new TextField(200);
  40.  
  41. public res()
  42. {
  43. setLayout(null);
  44. setVisible(true);
  45. setSize(900,900);
  46.  
  47. res.setBounds(320,50,650,20);
  48.  
  49. P1.setBounds(200,80,80,30);
  50. P2.setBounds(280,80,80,30);
  51. P3.setBounds(370,80,80,30);
  52. P4.setBounds(440,80,80,30);
  53. P5.setBounds(520,80,80,30);
  54.  
  55. lP1.setBounds(200,130,80,150);
  56. lP2.setBounds(280,130,80,150);
  57. lP3.setBounds(360,130,80,150);
  58. lP4.setBounds(440,130,80,150);
  59. lP5.setBounds(520,130,80,150);
  60.  
  61. msg.setBounds(100,400,300,20);
  62.  
  63. b1.setBounds(320,370,60,20);
  64. b2.setBounds(250,300,50,20);
  65.  
  66. add(res);
  67. add(P1);
  68. add(P2);
  69. add(P3);
  70. add(P4);
  71. add(P5);
  72.  
  73. add(lP1);
  74. add(lP2);
  75. add(lP3);
  76. add(lP4);
  77. add(lP5);
  78.  
  79. add(b1);
  80. add(b2);
  81.  
  82. add(msg);
  83.  
  84. b1.addActionListener(this);
  85. b2.addActionListener(this);
  86.  
  87.  
  88. try
  89. {
  90. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  91. con=DriverManager.getConnection("jdbc:odbc:student");
  92. st=con.createStatement();
  93. }
  94. catch(Exception e)
  95. {
  96. System.out.println(e);
  97. }
  98.  
  99. }
  100. public void actionPerformed(ActionEvent ae)
  101. {
  102. try
  103. {
  104. if(ae.getSource()==b1)
  105. {
  106.  
  107.  
  108. ResultSet rest=st.executeQuery("select * from result where result='"+ b1 + "'");
  109. if(rest.next())
  110. {
  111. msg.setText("Result");
  112. return;
  113. }
  114. else if(ae.getSource()==b2)
  115. {
  116. dispose();
  117. }
  118.  
  119. }
  120. }
  121. catch(Exception e)
  122. {
  123. System.out.println(e);
  124. }
  125. }
  126.  
  127.  
  128. public void keyTyped(KeyEvent ke)
  129. {
  130.  
  131. }
  132. public void keyPressed(KeyEvent ke)
  133. {
  134. }
  135. public void keyReleased(KeyEvent ke)
  136. {
  137.  
  138. }
  139.  
  140. }
  141. class result
  142. {
  143.  
  144. public static void main(String as[])
  145. {
  146. res demo=new res();
  147. }
  148. }
Last edited by sam18; Aug 27th, 2007 at 12:08 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sam18 is offline Offline
6 posts
since Aug 2007
Aug 27th, 2007
0

Re: Please help me in this coding

Post what error messages you get.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 27th, 2007
1

Re: Please help me in this coding

Well, a couple of things. This query makes little sense
Java Syntax (Toggle Plain Text)
  1. executeQuery("select * from result where result='"+ b1 + "'");
The field name in the where clause is the same as the table name.

Second, after you run the query and call .next(), you need to retrieve the field value like so
Java Syntax (Toggle Plain Text)
  1. String value = rest.getString("theFieldNameYouWant");
  2. msg.setText(value);
If you have other errors, post them.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Aug 27th, 2007
0

Re: Please help me in this coding

yeah i want to know little more
I want to create a page with a Rollno text box
and student have to enter their rollno in that textbox to get their result.
i am not getting that how to execute the query.
and applying the condition if the rollno entered by student doesnot exist.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sam18 is offline Offline
6 posts
since Aug 2007
Aug 27th, 2007
0

Re: Please help me in this coding

Now this coding is working but i want to display output in Frame not in Command Prompt.
Plz solve it out.


Java Syntax (Toggle Plain Text)
  1.  
  2. import java.awt.*;
  3. //import java.applet.*;
  4. import java.awt.event.*;
  5. import java.lang.*;
  6. import java.sql.*;
  7.  
  8. public class wscreen extends Frame
  9. implements WindowListener,ActionListener
  10. {
  11. Button b1;
  12. TextField t1;
  13. Label l1,l2;
  14. String msg,msg1,msg2,msg3,msg4,msg5,msg6,msg7;
  15.  
  16. public wscreen()
  17. {
  18. super("imp");
  19. setLayout(null);
  20.  
  21. b1= new Button("submit");
  22.  
  23. l1=new Label("Result Declared",Label.CENTER);
  24. l2=new Label("Enter ROLL NO", Label.LEFT);
  25.  
  26. t1=new TextField(5);
  27. /*t2=new TextField(5);
  28. t3=new TextField(5);
  29. t4=new TextField(5);
  30. t5=new TextField(5);
  31. t6=new TextField(5);
  32. t7=new TextField(5);
  33. t8=new TextField(5);*/
  34.  
  35.  
  36. Font f=new Font("Impact", Font.BOLD,25);
  37. Color c1=new Color(255,200,210);
  38. Color c2=new Color(200,200,210);
  39.  
  40. l1.setFont(f);
  41. l1.setForeground(c1);
  42. l1.setBackground(c2);
  43. l1.setBounds(90,20,200,30);
  44. add(l1);
  45.  
  46. l2.setBounds(20,70,90,30);
  47. add(l2);
  48.  
  49. t1.setBounds(200,70,90,20);
  50. add(t1);
  51. /*
  52. t2.setBounds(200,200,90,10);
  53.   add(t2);
  54. t3.setBounds(200,230,90,10);
  55.   add(t3);
  56. t4.setBounds(200,250,90,20);
  57.   add(t4);
  58. t5.setBounds(200,270,90,20);
  59.   add(t5);
  60. t6.setBounds(200,290,90,20);
  61.   add(t6);
  62. t7.setBounds(200,300,90,20);
  63.   add(t7);
  64. t8.setBounds(200,310,90,20);
  65.   add(t8);*/
  66.  
  67. b1.setBounds(90,120,200,30);
  68. add(b1);
  69.  
  70. addWindowListener(this);
  71. b1.addActionListener(this);
  72.  
  73. }
  74.  
  75.  
  76. public void actionPerformed(ActionEvent ae)
  77. {
  78. Object ob=ae.getSource();
  79.  
  80. if(ob==b1);
  81.  
  82.  
  83. }
  84.  
  85. public void paint(Graphics g)
  86. {
  87. msg="ROLLNO :"+t1.getText();
  88. msg1="NAME :";
  89. msg2="RESULT :";
  90. msg3="P1 :";
  91. msg4="P2 :";
  92. msg5="P3 :";
  93. msg6="P4 :";
  94. msg7="P5 :";
  95. g.drawString(msg,90,190);
  96. g.drawString(msg1,90,210);
  97. g.drawString(msg2,90,230);
  98. g.drawString(msg3,90,250);
  99. g.drawString(msg4,90,270);
  100. g.drawString(msg5,90,290);
  101. g.drawString(msg6,90,310);
  102. g.drawString(msg7,90,330);
  103. }
  104.  
  105. public static void main(String ar[])
  106. {
  107. wscreen d=new wscreen();
  108. d.setSize(400,400);
  109. d.setVisible(true);
  110.  
  111. String Sname;
  112. String Sno,m1,m2,m3,m4,m5;
  113.  
  114.  
  115. Connection Con=null;
  116. Statement Stmt;
  117. ResultSet ERs;
  118. try
  119. {
  120. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  121. Con=DriverManager.getConnection("jdbc:odbc:data");
  122. Stmt=Con.createStatement();
  123. ERs=Stmt.executeQuery("SELECT * FROM s1");
  124.  
  125. while(ERs.next())
  126. {
  127. Sno=ERs.getString("ROLLNO");
  128. Sname=ERs.getString("NAME");
  129. m1=ERs.getString("P1");
  130. m2=ERs.getString("P2");
  131. m3=ERs.getString("P3");
  132. m4=ERs.getString("P4");
  133. m5=ERs.getString("P5");
  134.  
  135.  
  136. System.out.println(Sno+" "+Sname+" "+m1+" "+m2+" "+m3+" "+m4+""+m5+"\n");
  137.  
  138. }
  139. Stmt.close();
  140. Con.close();
  141. }
  142. catch(Exception e)
  143. {
  144. System.out.println(e);
  145. }
  146.  
  147.  
  148. }
  149.  
  150. public void windowClosing(WindowEvent we)
  151. {
  152. this.setVisible(false);
  153. System.exit(0);
  154. }
  155.  
  156. public void windowActivated(WindowEvent we)
  157. {
  158. }
  159.  
  160. public void windowDeactivated(WindowEvent we)
  161. {
  162. }
  163. public void windowOpened(WindowEvent we)
  164. {
  165. }
  166. public void windowClosed(WindowEvent we)
  167. {
  168. }
  169. public void windowIconified(WindowEvent we)
  170. {
  171. }
  172.  
  173. public void windowDeiconified(WindowEvent we)
  174. {
  175. }
  176.  
  177. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sam18 is offline Offline
6 posts
since Aug 2007
Aug 27th, 2007
0

Re: Please help me in this coding

You do not want to be overriding paint on the Frame to display data. If you do need to override painting code, override paintComponent() on the appropriate component and be sure that you call super.paintComponent() if required. In any case, custom painting code is not called for here.

You should use an appropriate component to display your result data such as JTable, JLabels, or JTextArea. The choice will depend on what you are trying to display, as that is not very clear from your code.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Aug 27th, 2007
0

Re: Please help me in this coding

Thank you so much Ezzaral for helping me
actually i want to make a frame in which there is a text box in which a student has to enter their rollno to view their result.I am attaching the screenshot of window.Plz check it.

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
You do not want to be overriding paint on the Frame to display data. If you do need to override painting code, override paintComponent() on the appropriate component and be sure that you call super.paintComponent() if required. In any case, custom painting code is not called for here.

You should use an appropriate component to display your result data such as JTable, JLabels, or JTextArea. The choice will depend on what you are trying to display, as that is not very clear from your code.
Attached Thumbnails
Click image for larger version

Name:	untitled.JPG
Views:	13
Size:	16.5 KB
ID:	3985  
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sam18 is offline Offline
6 posts
since Aug 2007
Aug 27th, 2007
0

Re: Please help me in this coding

Just run your query in the ActionListener as you had in the first listing. You will need to set the roll number as a parameter.
Java Syntax (Toggle Plain Text)
  1. int rollNo = -1;
  2. try {
  3. rollNo = Integer.parseInt( t1.getText() );
  4. } catch (NumberFormatException nfe){
  5. JOptionPane.showMessageDialog(wscreen.this,"Not a valid roll number");
  6. }
  7. if (rollNo > -1) {
  8. PreparedStatement pstmt = Con.prepareStatement("SELECT * FROM s1 where RollNo=?);
  9. pstmt.setInt(1, rollNo);
  10. ResultSet rs = pstmt.executeQuery();
  11. if ( rs.next() ){
  12. // set output here
  13. }
  14. else {
  15. // no record was found
  16. }
  17. }
  18.  
If there is a result (from .next() ), then simply set the data fields from that result. You can use a JLabel with HTML if you want a result that is similar to direct painting on the Frame, without the hassle of custom painting. Look up JLabel in the API docs for more info.

You can also define the prepared statement outside the listener and reuse it with different roll number parameters if you will be submitting it many times. I just showed it in there for convenience. Don't forget to always close statements when you are done with them, as some drivers don't release those on their own very well.
Last edited by Ezzaral; Aug 27th, 2007 at 5:44 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Apr 6th, 2008
0

Re: Please help me in this coding

I am new to this webb, how could I send my Question?Opinion or even Chat?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Juma is offline Offline
2 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Is this possible in Java?
Next Thread in Java Forum Timeline: Java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC