Results 1 to 4 of 4
  1. #1
    zeeshan is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default data base connectivity

    :confused:
    hello friends! this a program which adds two number and store the result in database, but error.... plz solve

    plz solve this problem

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

    public class calc1 extends JFrame implements ActionListener {
    Container ctn = getContentPane();
    JLabel lbl1,lbl2,lbl3;
    JTextField txt1,txt2,txt3;
    JButton btn;

    calc1(){
    // Container ctn = getContentPane();
    this.setLayout(null);
    lbl1 = new JLabel("ENTER THE NO.1");
    lbl2 = new JLabel("ENTER THE NO.2");
    lbl3 = new JLabel("SUM");

    txt1 = new JTextField(20);
    txt2 = new JTextField(20);
    txt3 = new JTextField(20);

    btn = new JButton("ADD");

    btn.addActionListener(this);
    Color c1 = new Color(235,34,111);
    ctn.setBackground(c1);
    lbl1.setBounds(50,50,100,30);
    txt1.setBounds(170,50,100,30);
    lbl2.setBounds(50,100,100,30);
    txt2.setBounds(170,100,100,30);
    btn.setBounds(100,250,100,30);
    lbl3.setBounds(50,200,100,30);
    txt3.setBounds(170,200,100,30);
    txt1.setBackground(Color.WHITE);
    txt2.setBackground(Color.WHITE);
    txt3.setEditable(false);
    this.setSize(500,500);
    this.setVisible(true);
    this.setResizable(false);
    ctn.add(lbl1);
    ctn.add(lbl2);
    ctn.add(lbl3);
    ctn.add(txt1);
    ctn.add(txt2);
    ctn.add(txt3);
    ctn.add(btn);
    }

    public void actionPerformed(ActionEvent e){

    int a = Integer.parseInt(txt1.getText());
    int b = Integer.parseInt(txt2.getText());
    int c= a+b;
    txt3.setText(String.valueOf(c));
    if(e.getSource()==btn){
    ctn.setBackground(Color.PINK);
    }

    int n = Integer.parseInt(txt3.getText());
    try {
    class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:calc");
    Statement st = con.createStatement();
    String sql="insert into table1 "+"col1"+"values"+"n";
    st.executeUpdate(sql);
    }catch (SQLException ee){
    System.out.println(ee.toString());
    }


    }

    public static void main (String[] args) {
    calc1 c = new calc1();
    }
    }
    Last edited by zeeshan; 02-21-2010 at 06:15 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by zeeshan View Post
    :confused:
    hello friends!

    plz solve this problem
    Hello, and welcome to the forum. Question: what problem? You don't state an actual question anywhere.

    Also, please look at my signature regarding using code tags when posting code on the forum. Doing this will make your code MUCH easier to read.

    Much luck!

  3. #3
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    Your jdbc code is no where. It has be within some method but since u closed the braces just before jdbc code, it doesn't belong to any method. And database interaction shd happen after certain button is clicked which is also no there. May be the braces after ctn.setBackground(Color.PINK); should't b there.

  4. #4
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    Quote Originally Posted by zeeshan View Post
    :confused:
    hello friends!

    plz solve this problem

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

    public class calc1 extends JFrame implements ActionListener {
    Container ctn = getContentPane();
    JLabel lbl1,lbl2,lbl3;
    JTextField txt1,txt2,txt3;
    JButton btn;

    calc1(){
    // Container ctn = getContentPane();
    this.setLayout(null);
    lbl1 = new JLabel("ENTER THE NO.1");
    lbl2 = new JLabel("ENTER THE NO.2");
    lbl3 = new JLabel("SUM");

    txt1 = new JTextField(20);
    txt2 = new JTextField(20);
    txt3 = new JTextField(20);

    btn = new JButton("ADD");

    btn.addActionListener(this);
    Color c1 = new Color(235,34,111);
    ctn.setBackground(c1);
    lbl1.setBounds(50,50,100,30);
    txt1.setBounds(170,50,100,30);
    lbl2.setBounds(50,100,100,30);
    txt2.setBounds(170,100,100,30);
    btn.setBounds(100,250,100,30);
    lbl3.setBounds(50,200,100,30);
    txt3.setBounds(170,200,100,30);
    txt1.setBackground(Color.WHITE);
    txt2.setBackground(Color.WHITE);
    txt3.setEditable(false);
    this.setSize(500,500);
    this.setVisible(true);
    this.setResizable(false);
    ctn.add(lbl1);
    ctn.add(lbl2);
    ctn.add(lbl3);
    ctn.add(txt1);
    ctn.add(txt2);
    ctn.add(txt3);
    ctn.add(btn);
    }

    public void actionPerformed(ActionEvent e){

    int a = Integer.parseInt(txt1.getText());
    int b = Integer.parseInt(txt2.getText());
    int c= a+b;
    txt3.setText(String.valueOf(c));
    if(e.getSource()==btn){
    ctn.setBackground(Color.PINK);
    }

    /* int n = Integer.parseInt(txt3.getText());
    try {
    class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:calc");
    Statement st = con.createStatement();
    String sql="insert into table1 "+"col1"+"values"+"n";
    st.executeUpdate(sql);
    }catch (SQLException ee){
    System.out.println(ee.toString());
    }*/


    }

    public static void main (String[] args) {
    calc1 c = new calc1();
    }
    }
    your method ended right there leaving jdbc code no where.

Similar Threads

  1. data structure and data base??
    By ahmed13 in forum Advanced Java
    Replies: 8
    Last Post: 03-27-2009, 05:48 AM
  2. how to store the data in data base
    By eclipse3.4ide in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 04:25 AM
  3. error while retrieving data from data base
    By kirtesh4u in forum New To Java
    Replies: 5
    Last Post: 11-15-2008, 04:10 PM
  4. Replies: 1
    Last Post: 08-06-2007, 05:38 PM
  5. Connection to data base
    By Daniel in forum JDBC
    Replies: 2
    Last Post: 06-07-2007, 04:55 AM

Posting Permissions

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