Results 1 to 5 of 5
  1. #1
    kailaselamkar is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default Help me with my Program.

    In this program,I have created 2 textfields(NAME and CITY) and 2 buttons(ACCEPT and RESET).
    I am trying to create a program which will accept name and city in the textfields.On clicking the insert button the values will be inserted in a table in sql 2005.
    I have created a table called table1 in sql 2005.
    On directly providing the values in the INSERT statement ,the program works properly.But I am not able to create the program to take the values during runtime.
    The program compiles properly,even runs but I am not getting the desired output.
    (The program reaches upto t1.getText() and remains stuck there.)
    Please help.

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.sql.*;

    public class SqlExample extends JFrame implements ActionListener

    {
    JLabel l1,l2;
    JTextField t1,t2;
    JButton b1,b2;
    JPanel p1;
    String s1,s2;

    public void gui()
    {
    JLabel l1=new JLabel("NAME");
    JLabel l2=new JLabel("CITY");
    JTextField t1=new JTextField(20);
    JTextField t2=new JTextField(10);
    JButton b1=new JButton("INSERT");
    JButton b2=new JButton("RESET");
    JPanel p1=new JPanel();
    p1.add(l1);
    p1.add(t1);
    p1.add(l2);
    p1.add(t2);
    p1.add(b1);
    p1.add(b2);
    p1.setBackground(Color.RED);
    add(p1);
    b1.addActionListener(this);
    b2.addActionListener(this);
    System.out.println("yOU HAVE REACHED HERE");
    }

    void registerDriver()
    {
    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }
    catch(Exception e1)
    {
    }
    }
    public void actionPerformed(ActionEvent ae)
    {
    if(ae.getActionCommand()=="INSERT")
    {
    try
    {

    String str="INSERT INTO table1(NAME,CITY) VALUES(?,?)";
    Connection con=DriverManager.getConnection("jdbc:odbc:Mydatas ource");
    System.out.println("You have passed the connection command");
    s1=t1.getText();
    s2=t2.getText();
    System.out.println("You have passes the getText command.");
    PreparedStatement stmt=con.prepareStatement("INSERT INTO table1(NAME,CITY) VALUES(?,?)");
    System.out.println("You have passed the statement command");
    stmt.setString(1,t1.getText());
    stmt.setString(2,t2.getText());
    System.out.println("You have passed the setstring command");
    ResultSet rs= stmt.executeQuery();
    if(ae.getActionCommand()=="RESET")
    System.out.println("You have pressed the RESET button.");
    }
    catch(Exception e2)
    {
    }
    }
    }
    public static void main(String z[])

    {
    SqlExample se=new SqlExample();
    se.gui();
    se.setSize(250,250);
    se.setVisible(true);
    se.registerDriver();
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    One problem you have is testing for equality of Strings with ==. Use the equals method.

    Add print outs in the catch exception clauses and do a printStackTrace to show what the error is.
    Last edited by Norm; 05-20-2011 at 05:21 PM.

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    It's also helpful to tell us what the desired output is, and what you are getting instead. It also helps if your code is in [code] tags.

  4. #4
    kailaselamkar is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    I just want to create a program where the user will type the name and the city in the textfields provided.Then on clicking the INSERT button the values in the textfields will be added in a table created in sql 2005.
    The program compiles without any errors.On clicking the INSERT button the program executes upto System.out.println("You have passed the connection command");but does not proceeed further i.e the values are not inserted in the table.Please help.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Add print outs in the catch exception clauses and do a printStackTrace to show if there is an error.

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 01:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 07:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 07:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •