Hi,

Having some issues connecting to MS Access Database. I think its ot connecting to the database. Below are the source codes:

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

public class DB extends DBDriver implements ActionListener
{
    JFrame f;
    JLabel Fname = new JLabel("First Name: ");
    JLabel Lname = new JLabel("Last Name: ");
    JLabel Snum  = new JLabel("Student Number: ");
    JLabel Sec   = new JLabel("Cys: ");
    JLabel Age   = new JLabel("Age: ");
    JLabel Gen   = new JLabel("Gender: ");

    JTextField t1 = new JTextField(20);
    JTextField t2= new JTextField(20);
    JTextField t3= new JTextField(20);
    JTextField t4= new JTextField(20);
    JTextField t5= new JTextField(20);
    JTextField t6= new JTextField(20); 

    JButton deleteButton = new JButton("Delete");
    JButton addButton = new JButton("Add");
    JButton viewButton = new JButton("View");
    JButton editButton = new JButton("Edit");
    JButton submitButton = new JButton("Submit");
    public DB() 
    {
   frame();
    }

 public void frame()
 {
 JFrame f = new JFrame();
 f.setSize(800,600);
 f.setVisible(true);
 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 f.setLocationRelativeTo(null);
 f.setTitle("Student Info");
 f.setLayout(null);

 f.add(Age);
 f.add(Fname);
 f.add(Lname);
 f.add(Sec);
 f.add(Snum);
 f.add(Gen);
 f.add(t1);
 f.add(t2);
 f.add(t3);
 f.add(t4);
 f.add(t5);
 f.add(t6);
 f.add(submitButton);

 Fname.setBounds(20,20,100,30);
 t1.setBounds(85,25,100,20);
 Lname.setBounds(20,60,100,30);
 t2.setBounds(85,65,100,20);
 Snum.setBounds(20,100,100,30);
 t3.setBounds(117,105,100,20);
 Sec.setBounds(20,140,100,30);
 t4.setBounds(47,145,100,20);
 Age.setBounds(20,180,100,30);
 t5.setBounds(47,185,100,20);
 Gen.setBounds(20,220,100,30);
 t6.setBounds(65,225,100,20);
 submitButton.setBounds(45,125,35,40);
 submitButton.addActionListener(this);

 try
        {
            rs.next();
            t1.setText(rs.getString("fName"));
            t2.setText(rs.getString("lName"));
            t3.setText(rs.getString("Snum"));
            t4.setText(rs.getString("Sec"));
            t5.setText(rs.getString("Age"));
            t6.setText(rs.getString("Gender"));

        }
        catch(Exception ex)
        {

        }

 }  
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==submitButton)
{               String Fname = t1.getText();
                String Lname = t2.getText();
                String Snum = t3.getText();
                String Sec = t4.getText();
                String Age = t5.getText();
                String Gen = t6.getText();

                try
                {
                rs.moveToInsertRow();

                rs.updateString("fName",Fname);
                rs.updateString("lName",Lname);
                rs.updateString("Snum",Snum);
                rs.updateString("Sec",Sec);
                rs.updateString("Age",Age);
                rs.updateString("Gender",Gen);
                rs.insertRow();

                st.close();
                rs.close();

                st = con.createStatement(rs.TYPE_SCROLL_INSENSITIVE,rs.CONCUR_UPDATABLE);
                String sql = "select * from Table1";
                rs = st.executeQuery(sql);

                rs.next();

                t1.setText(rs.getString("fName"));
                t2.setText(rs.getString("lName"));
                t3.setText(rs.getString("Snum"));
                t4.setText(rs.getString("Sec"));
                t5.setText(rs.getString("Age"));
                t6.setText(rs.getString("Gender"));


        }

            catch(Exception ex)
            {

            }
}



}   
}

---

import java.sql.*;

public class DBDriver
{
    Connection con;
    Statement st;
    ResultSet rs;

public DBDriver()
    {
        Marisolle();
    }
 public void Marisolle()
    {
        try
        {

        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        Class.forName(driver);

        String SD = "jdbc:odbc:Database1";
        con = DriverManager.getConnection(SD);
        st = con.createStatement(rs.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
        String sql = "select * from Table1";
        rs = st.executeQuery(sql);

        }

     catch(Exception ex)
        {

        } 
    }
 public static void main(String[] args)
    {
        new DBDriver();
        new DB();

    }


---

Recommended Answers

All 2 Replies

Never NEVER NEVER do this when writing new code:

} catch (Exception e) {
}

If/when there is an error you just told Java that you didn't want to know anything about it, and please discard the detailed error message that Java just created for you.
ALWAYS put an e.printStackTrace(); in your catch blocks until/unless you have a good reason to do something else.

IThank you for the reply and for the code advice. The problem is with my Java 64bit accessing MS Access. I replaced Java 64bit with Java 32 bit. Many thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.