By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,319 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,319 IT Pros & Developers. It's quick & easy.

problem with the output

P: 37
hi.. to all..
i executed the following program.. i entered the data in the output screen..but the data is not updated in the database.... Is there any problem with the program.. plz help me........

Expand|Select|Wrap|Line Numbers
  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.sql.*;
  4. import java.awt.event.*;
  5. import sun.jdbc.odbc.*;
  6.  
  7. public class Customerdetails extends Frame implements ActionListener
  8. {
  9.  
  10.      TextField ccod,cname,add,phno,conp,email,fax;
  11.      Label l1,l2,l3,l4,l5,l6,l7;
  12.      Button save,exit;
  13.      Connection con;
  14.      ResultSet rs;
  15.      Statement stmt;
  16.      String s1,s2,s3,s4,s5,s6,s7;
  17.  
  18.  
  19. public Customerdetails() 
  20. {
  21.  
  22.     super("CustomerDetails");
  23.     setSize(700,750);
  24.     setLayout(null);
  25.     setBackground(new java.awt.Color(245, 117, 105));
  26.     l1=new Label("Customercode");
  27.     l2=new Label("CompanyName");
  28.     l3=new Label("Address");
  29.     l4=new Label("PhoneNumber");
  30.     l5=new Label("Contact person");
  31.     l6=new Label("EmailAddress");
  32.     l7=new Label("FaxNumber");
  33.     l1.setBounds(20,100,100,70);
  34.     l2.setBounds(20,160,100,70);
  35.     l3.setBounds(20,220,100,70);
  36.     l4.setBounds(20,280,100,70);
  37.     l5.setBounds(20,340,100,70);
  38.     l6.setBounds(20,400,100,70);
  39.     l7.setBounds(20,460,100,70);
  40.     ccod=new TextField();
  41.     cname=new TextField();
  42.     add=new TextField();
  43.     phno=new TextField();
  44.     conp=new TextField();
  45.     email=new TextField();
  46.     fax=new TextField();
  47.     ccod.setBounds(200,120,150,40);
  48.     cname.setBounds(200,180,150,40);
  49.     add.setBounds(200,240,150,40);
  50.     phno.setBounds(200,300,150,40);
  51.     conp.setBounds(200,360,150,40);
  52.     email.setBounds(200,420,150,40);
  53.     fax.setBounds(200,480,150,40);
  54.     Button save= new Button("Save");
  55.     Button exit= new Button("Exit");
  56.     save.setBounds(160,580,100,40);
  57.     exit.setBounds(280,580,100,40);
  58.     add(l1);
  59.     add(ccod);
  60.     add(l2);
  61.     add(cname);
  62.     add(l3);
  63.     add(add);
  64.    add(l4);
  65.    add(phno);
  66.    add(l5);
  67.    add(conp);
  68.    add(l6);
  69.    add(email);
  70.    add(l7);
  71.    add(fax);
  72.    add(save);
  73.    add(exit);
  74.   save.addActionListener(this);
  75.   exit.addActionListener(this);
  76.   setVisible(true);
  77.  
  78.  
  79.  
  80. addWindowListener(new WindowAdapter(){
  81.   public void WindowClosing(WindowEvent w) {System.exit(0);}});
  82. }
  83.  public void actionPerformed(ActionEvent ae)
  84. {
  85. Customerdetails cud= new Customerdetails();
  86.  if(ae.getSource()==save)
  87. {
  88.  s1=ccod.getText();
  89.  s2=cname.getText();
  90.  s3=add.getText();
  91.  s4=phno.getText();
  92.  s5=conp.getText();
  93.  s6=email.getText();
  94.  s7=fax.getText();
  95.  
  96. try
  97. {
  98.       String query = "insert into Customerdetails (Customercode,CompanyName,Address,PhoneNumber,Contact person,EmailAddress,FaxNumber)"+ "values('"+s1+"','"+s2+"','"+s3+"','"+s4+"','"+s5+"','"+s6+"','"+s7+"')";
  99.       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  100.       con=DriverManager.getConnection("jdbc:odbc:prism");
  101.       stmt=con.createStatement();
  102.       rs=stmt.executeQuery(query);
  103.       stmt.close();
  104.  
  105. }
  106. catch(ClassNotFoundException e)
  107. {
  108.   System.out.println(e);
  109. }
  110. catch(SQLException e)
  111. {
  112.   System.out.println(e);
  113. }
  114. }
  115. else if(ae.getSource()==exit)
  116. {
  117. System.exit(0);
  118. }
  119. else
  120. {
  121. System.exit(0);
  122. }
  123. }
  124.  public static void main(String args[]) 
  125. {
  126.    Customerdetails cud= new Customerdetails();
  127. }
  128. }
  129.  
  130.  
Sep 11 '07 #1
Share this Question
Share on Google+
3 Replies


100+
P: 201
Hi
you are using rs=stmt.executeQuery(query); commands. This is for retreving the data. use following
rs=stmt.executeUpdate(query);
your data will be saved in the database.
Sep 11 '07 #2

P: 37
Hi...
if i change like this ..
rs=stmt.executeUpdate(query);
it shows me an error..
plz see my codes once again.. i'm not updating the data.....i hav get input data from the user..and hav to add that data to the database..

ragards
pradeep
Sep 11 '07 #3

10K+
P: 13,264
Hi...
if i change like this ..
rs=stmt.executeUpdate(query);
it shows me an error..
plz see my codes once again.. i'm not updating the data.....i hav get input data from the user..and hav to add that data to the database..

ragards
pradeep
Read the API docs for the method that was suggested to you. It does not return a ResultSet. You don't need a resultset for inserting/updating data.
Sep 11 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.