views:

91

answers:

1

Hi

I'm Riya a B.Tech. student and i have to make a corejava project on the topic COLLEGE MANAGEMENT SYSTEM . I have created the required database using MS Access. I m facing problem in a code which i m attaching here. Actually i want to update a data in the data base so i have made 2 frames in one of them Roll no. is asked when it is entered then the 2nd frame opens and asks Enter Marks to be updated...when i enter the marks to be updated then a dialogue box opens shoing "Marks Updated Successfully". Till here i don't have any problem..the problem is when i open the database i see that actually the Marks is not updated there.So please please please somebody help me solving this problem.

The code is as follows:

1ST FRAME

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

class MyFrame03 extends JFrame implements ActionListener {
    JLabel l1;
    JTextField t1;
    JPanel p1, p2;
    JButton b1;
    Connection con;
    PreparedStatement pst;
    String s1;

    MyFrame03() {
        setLayout(new GridLayout(4, 1));
        setTitle("Enter Data Required");
        setBackground(Color.blue);
        l1 = new JLabel("Roll no");
        t1 = new JTextField(12);
        p1 = new JPanel();
        p2 = new JPanel();
        b1 = new JButton("SUBMIT");
        p1.add(l1);
        p1.add(t1);
        p2.add(b1);
        add(p1, "Center");
        add(p2, "South");
        b1.addActionListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 500);
        setVisible(true);
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("Jdbc:Odbc:dsn3");
            if (con != null)
                System.out.println("Connection Established");
        }
        catch (Exception e) {
            System.out.println("exception");
        }
    }

    public void actionPerformed(ActionEvent e1) {
        if (e1.getSource() == b1) {
            s1 = t1.getText();
            try {
                pst = con.prepareStatement("insert into stud values(?)");
                pst.setString(1, s1);
                pst.executeUpdate();
                con.commit();
                con.close();
            }
            catch (SQLException e) {
                System.out.println("except1");
            }
            MyFrame04 m1 = new MyFrame04(s1);
            dispose();
        }
    }

    public static void main(String s[]) throws SQLException {
        MyFrame03 m1 = new MyFrame03();
    }
}

2nd FRAME

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

class MyFrame04 extends JFrame implements ActionListener {
    JLabel l1;
    JTextField t1;
    JPanel p1, p2;
    JButton b1;
    String s1, s2;
    Connection con;
    PreparedStatement pst;

    public MyFrame04(String s1) {
        this.s1 = s1;
        setLayout(new GridLayout(4, 1));
        setTitle("Update Marks");
        setBackground(Color.blue);
        l1 = new JLabel("Enter Marks");
        t1 = new JTextField(12);
        b1 = new JButton("SUBMIT");
        p1 = new JPanel();
        p2 = new JPanel();
        p1.add(l1);
        p1.add(t1);
        p2.add(b1);
        add(p1, "Center");
        add(p2, "South");
        b1.addActionListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 500);
        setVisible(true);
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("Jdbc:Odbc:dsn3");
            if (con != null)
                System.out.println("Connection Established");
        }
        catch (Exception e) {
            System.out.println("exception");
        }
    }

    public void actionPerformed(ActionEvent e1) {
        if (e1.getSource() == b1) {
            s2 = t1.getText();
            try {
                pst = con.prepareStatement("UPDATE stud set Marks=? WHERE Roll no=?");
                pst.setString(1, s2);
                pst.setString(2, s1);
                pst.executeUpdate();
                con.commit();
                con.close();
            }
            catch (SQLException e) {
                System.out.println("except1");
            }
            JOptionPane.showMessageDialog(this, "Marks updated succesfully");
            dispose();
        }
    }
}

Thankyou

+2  A: 

Does your database access code work without Swing? The simplest way to see this would be something like:

class HelloDatabase {
  public static void main(String[] args) {
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("Jdbc:Odbc:dsn3");
        if (con != null) {
            int marks = 1, roll = 1; // or whatever (your data here)
            System.out.println("Connection Established");
            PreparedStatement pst = con.prepareStatement("UPDATE stud SET marks=? WHERE roll_num=?");
            pst.setString(1, marks);
            pst.setString(2, roll);
            pst.executeUpdate();
            con.commit();
            con.close();
        }
    }
    catch (Exception e) {
        System.out.println("exception");
    }    
  }
}

If this gets a handle and accesses your database properly, you're in luck.

By the way -- and this may actually be your issue -- I noticed that your SQL query from the original post had an ambiguity in it:

UPDATE stud set Marks=? WHERE Roll no=?

"Roll no" would not get recognized a valid SQL field, as far as I understand -- these should be a single 'word' (like 'roll', 'roll_num' or even 'RollNum' -- but I don't think a name like 'Roll no' would work here.)

Joe
thanx joe for ur help..it helped me...the problem was the space between Roll and no...i changed it to roll then it worked
np :) you always want to write the smallest test possible that exhibits the question you're concerned with.
Joe
how's it going?
Joe