my dog learned polymorphism*
The moose likes JDBC and the fly likes jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "jdbc" Watch "jdbc" New topic
Author

jdbc

alka_duggal
Greenhorn

Joined: Nov 28, 2000
Posts: 6
hi i am alka
my problem is i cant run the program
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class jdbc extends Frame implements ActionListener
{
TextField t1;
TextField t2;
Button b;
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Panel P;
ResultSet rs;
Connection con;
Statement st;
public jdbc()
{
super("your data here");
setLayout(new GridLayout(5,1));
t1=new TextField(20);
t2=new TextField(20);
b=new Button("next");
b1=new Button("previous");
b2=new Button("exit");
b3=new Button("close");
b4=new Button("clear");
b5=new Button("modify");
b6=new Button("search");
P=new Panel();
add(new Label("name"));
add(t1);
add(new Label("class"));
add(t2);
add(P);
P.add(b);
P.add(b1);
P.add(b2);
P.add(b3);
P.add(b4);
P.add(b5);
P.add(b6);
b.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:aa");
st=con.createStatement();
rs=st.executeQuery("select*from table1");
}
catch(Exception e)
{}
}
public static void main(String args[])
{
jdbc j=new jdbc();
j.setSize(300,300);
j.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b)
{
try
{
rs.next();
t1.setText(rs.getString(1));
t2.setText(rs.getString(2));
}
catch(Exception e)
{}
}
if(ae.getSource()==b5)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:aa");
PreparedStatement ps=con.prepareStatement("insert into table1(name,class) values(?,?)");
t1.setText(rs.getString(1));
t2.setText(rs.getString(2));
ps.executeUpdate();
}
catch(Exception e)
{}
}
}
}
could any body tell me the solution
Pranav Jaidka
Ranch Hand

Joined: Sep 14, 2000
Posts: 264
Hi Alka
it would be great if you can actually post what exactly the problem is .Its not easy going through the code and making out .
rgds
pranav
NageswaraRao Karra
Ranch Hand

Joined: Nov 12, 2000
Posts: 34
Hello!
i have modified a little of yr.code and it works fine.
here is the code.

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class jdbc extends Frame implements ActionListener
{
TextField t1;
TextField t2;
Button b;
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Panel P;
ResultSet rs;
Connection con;
Statement st;
public jdbc()
{
super("your data here");
setLayout(new GridLayout(5,1));
t1=new TextField(20);
t2=new TextField(20);
b=new Button("next");
b1=new Button("previous");
b2=new Button("exit");
b3=new Button("close");
b4=new Button("clear");
b5=new Button("modify");
b6=new Button("search");
P=new Panel();
add(new Label("name"));
add(t1);
add(new Label("class"));
add(t2);
add(P);
P.add(b);
P.add(b1);
P.add(b2);
P.add(b3);
P.add(b4);
P.add(b5);
P.add(b6);
b.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("Jdbc:Odbc:aa",);
st=con.createStatement();
rs=st.executeQuery("select*from table1");
}
catch(Exception e)
{e.printStackTrace();}
}
public static void main(String args[])
{
jdbc j=new jdbc();
j.setSize(300,300);
j.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b)
{
try
{
rs.next();

t1.setText(rs.getString(1));
t2.setText(rs.getString(2));
}
catch(Exception e)
{ b.setEnabled(false);
//e.printStackTrace();
}
}
if(ae.getSource()==b5)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:Odbc:aa");

PreparedStatement ps=con.prepareStatement("insert into table1 values(?,?)");
ps.setString(1,t1.getText());
ps.setString(2,t2.getText());
ps.executeUpdate();

}
catch(Exception e)
{e.printStackTrace();}
}
}
}
/*Assuming you are using database which does not require username and password*/
else you may use con=DriverManager.getConnection("jdbc:Odbc:aa","username","password");
[This message has been edited by NageswaraRao Karra (edited December 01, 2000).]
alka_duggal
Greenhorn

Joined: Nov 28, 2000
Posts: 6
hi thanx 4 ur reply.but u could not understand my problem
my problem is with action performed part of the program next button is working but modify button is not working.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Please note that The Java Ranch has a naming policy, described here and "alka_duggal" is not a valid name as it has an "_" in it. Please choose one which meets the requirements.
Thanks.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Raukutam Sandeep
Ranch Hand

Joined: Nov 15, 2000
Posts: 61
hi alka,

why are u using setText method.
In the 'modify' button code,
use setString of PreparedStatement object if it is a string, use setInt if the value is an integer.
In u'r program,
ps.setString(1,t1.getText());
ps.setString(2,t2.getText());
If the textfield has an integer value who backend datatype is int, use setInt method instead.
Hope u understood.
regds,
Sandeep.
NageswaraRao Karra
Ranch Hand

Joined: Nov 12, 2000
Posts: 34
hello!
i will go through the code again.but it works fine whether it is modify or next ...buttons.if it is prepared statement we use setString method as far as i know.anyway i follow it up
alka_duggal
Greenhorn

Joined: Nov 28, 2000
Posts: 6
hi thanx for ur reply but my prog is still not working i want that processing should bo done when i click on modify button
but its not happenning
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
"alka_duggal", please chose another name as mentioned above, "alka_duggal" will soon be switched off and you will be unable to post using that name.
 
 
subject: jdbc