Please help me in this coding

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2007
Posts: 6
Reputation: sam18 is an unknown quantity at this point 
Solved Threads: 0
sam18 sam18 is offline Offline
Newbie Poster

Please help me in this coding

 
0
  #1
Aug 27th, 2007
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Please help me in this coding

 
0
  #2
Aug 27th, 2007
Post what error messages you get.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Please help me in this coding

 
1
  #3
Aug 27th, 2007
Well, a couple of things. This query makes little sense
  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
  1. String value = rest.getString("theFieldNameYouWant");
  2. msg.setText(value);
If you have other errors, post them.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 6
Reputation: sam18 is an unknown quantity at this point 
Solved Threads: 0
sam18 sam18 is offline Offline
Newbie Poster

Re: Please help me in this coding

 
0
  #4
Aug 27th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 6
Reputation: sam18 is an unknown quantity at this point 
Solved Threads: 0
sam18 sam18 is offline Offline
Newbie Poster

Re: Please help me in this coding

 
0
  #5
Aug 27th, 2007
Now this coding is working but i want to display output in Frame not in Command Prompt.
Plz solve it out.


  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Please help me in this coding

 
0
  #6
Aug 27th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 6
Reputation: sam18 is an unknown quantity at this point 
Solved Threads: 0
sam18 sam18 is offline Offline
Newbie Poster

Re: Please help me in this coding

 
0
  #7
Aug 27th, 2007
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.

Originally Posted by Ezzaral View Post
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
untitled.JPG  
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Please help me in this coding

 
0
  #8
Aug 27th, 2007
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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 2
Reputation: Juma is an unknown quantity at this point 
Solved Threads: 0
Juma Juma is offline Offline
Newbie Poster

Re: Please help me in this coding

 
0
  #9
Apr 6th, 2008
I am new to this webb, how could I send my Question?Opinion or even Chat?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC