• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

jdbc

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic