Results 1 to 5 of 5
Thread: Help me with my Program.
- 05-20-2011, 04:00 PM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
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();
}
}
- 05-20-2011, 04:18 PM #2
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 04:21 PM.
- 05-20-2011, 08:21 PM #3
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.
- 05-21-2011, 01:46 PM #4
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
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.
- 05-21-2011, 01:58 PM #5
Similar Threads
-
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 12:57 PM -
How would I open a program from a single button of another program. Help...
By decgaid06 in forum New To JavaReplies: 13Last Post: 03-22-2011, 06:49 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks