每次我运行程序时,都会出现错误


  给定的文件不存在:Registration.accdb


“ Registeration.accdb”文件位于我拥有代码的目录中,并且在下图中,我指定了数据库路径。

java - 连接到.accdb文件时,“给出的文件不存在”-LMLPHP

// Registeration of Student
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Registeration extends ControlDesk{
private JLabel title,rollno,firstname,lastname,gender,fathername,contact,alter_contact,course,section,address,session;
private JTextField rollnotxt,firstnametxt,lastnametxt,gendertxt,fathernametxt,contacttxt,alter_contacttxt,coursetxt,sectiontxt,addresstxt,sessiontxt;
private JButton register,controlDesk;
private JFrame registerFrame;
private JPanel registerPanel;
//Constructor
public Registeration(){
    registerFrame = new JFrame("Library Management System - Registeration");
    registerPanel = new JPanel();
    registerFrame.add(registerPanel);
    registerPanel.setBackground(Color.YELLOW);
    registerPanel.setLayout(null);
    registerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    registerFrame.setSize(1024,720);
    registerFrame.setVisible(true);

    title = new JLabel("Registeration Form");
    title.setFont(new Font("Arial",Font.PLAIN,25));
    title.setBounds(300,10,300,30);
    registerPanel.add(title);
    //<--labels
    rollno = new JLabel("Roll Number:");
    rollno.setBounds(100,50,100,30);
    rollno.setFont(new Font("Arial",Font.PLAIN,15));
    registerPanel.add(rollno);

    firstname = new JLabel("First Name:");
    firstname.setBounds(100,100,100,30);
    firstname.setFont(new Font("Arial",Font.PLAIN,15));
    registerPanel.add(firstname);

    lastname = new JLabel("Last Name:");
    lastname.setBounds(100,150,100,30);
    lastname.setFont(new Font("Arial",Font.PLAIN,15));
    registerPanel.add(lastname);

    fathername = new JLabel("Father's Name: ");
    fathername.setBounds(100,200,100,30);
    fathername.setFont(new Font("Arial",Font.PLAIN,15));
    registerPanel.add(fathername);

    contact = new JLabel("Contact:");
    contact.setBounds(100,250,100,30);
    contact.setFont(new Font("Arial",Font.PLAIN,15));
    registerPanel.add(contact);

    alter_contact = new JLabel("Alternative Contact:");
    alter_contact.setBounds(100,300,100,30);
    alter_contact.setFont(new Font("Arial",Font.PLAIN,15));
    registerPanel.add(alter_contact);

    course = new JLabel("Course");
    course.setBounds(100,350,100,30);
    course.setFont(new Font("Arial",Font.PLAIN,15));
    registerPanel.add(course);

    section = new JLabel("Section");
    section.setBounds(100,400,100,30);
    section.setFont(new Font("Arial",Font.PLAIN,15));
    registerPanel.add(section);

    address = new JLabel("Address:");
    address.setBounds(100,450,100,30);
    address.setFont(new Font("Arial",Font.PLAIN,15));
    registerPanel.add(address);

    session = new JLabel("Session");
    session.setBounds(100,500,100,30);
    session.setFont(new Font("Arial",Font.PLAIN,15));
    registerPanel.add(session);

    gender = new JLabel("Gender");
    gender.setBounds(100,550,100,30);
    gender.setFont(new Font("Arial",Font.PLAIN,15));
    registerPanel.add(gender);
    //--> end labels
    //---. text field -->
    rollnotxt = new JTextField("");
    rollnotxt.setBounds(300, 50, 250, 30);
    registerPanel.add(rollnotxt);

    firstnametxt = new JTextField("");
    firstnametxt.setBounds(300,100,250,30);
    registerPanel.add(firstnametxt);

    lastnametxt = new JTextField("");
    lastnametxt.setBounds(300,150,250,30);
    registerPanel.add(lastnametxt);

    fathernametxt = new JTextField("");
    fathernametxt.setBounds(300,200,250,30);
    registerPanel.add(fathernametxt);

    contacttxt = new JTextField("");
    contacttxt.setBounds(300,250,250,30);
    registerPanel.add(contacttxt);

    alter_contacttxt = new JTextField("");
    alter_contacttxt.setBounds(300,300,250,30);
    registerPanel.add(alter_contacttxt);

    coursetxt = new JTextField("");
    coursetxt.setBounds(300,350,250,30);
    registerPanel.add(coursetxt);

    sectiontxt = new JTextField("");
    sectiontxt.setBounds(300,400,250,30);
    registerPanel.add(sectiontxt);

    addresstxt = new JTextField("");
    addresstxt.setBounds(300,450,250,30);
    registerPanel.add(addresstxt);

    sessiontxt = new JTextField("");
    sessiontxt.setBounds(300,500,250,30);
    registerPanel.add(sessiontxt);

    gendertxt = new JTextField("");
    gendertxt.setBounds(300,550,250,30);
    registerPanel.add(gendertxt);
    //-- start textField-->

    register = new JButton("Register");
    register.setBounds(300,600,150,50);
    //          event on register button
    register.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
                // store in database
            try{
                Connection con = DriverManager.getConnection("jdbc:ucanaccess://Registeration.accdb"); // connection
                Statement st = con.createStatement();
                String sql = "insert into Registeration values('"+ rollnotxt.getText()  +"','"+firstnametxt.getText()+"','"+lastnametxt.getText()+"','"+
                lastnametxt.getText() + "','" + fathernametxt.getText()+"','"+contacttxt.getText()+"','"+alter_contacttxt.getText()+"','"+coursetxt.getText()+"','"+
                        sectiontxt.getText()+"','"+addresstxt.getText()+"','"+sessiontxt.getText()+"','"+gendertxt.getText()+"')";

                st.executeQuery(sql);
                if(st.executeUpdate(sql) > 0){
                    JOptionPane.showMessageDialog(null,"Student Added");
                }

                con.close();
            }
            catch(SQLException se){
                System.out.println("Error:- "+ se.getMessage());
            }

        }
    });
    registerPanel.add(register);

    controlDesk = new JButton("Control Desk");
    controlDesk.setBounds(500,600,150,50);
    controlDesk.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            new ControlDesk();
        }
    });
    registerPanel.add(controlDesk);
}// constructor end

}

最佳答案

mmmm ....请记住,您没有使用jdbc-odbc桥,因此配置Registeration.accdb ODBC源是无用的(以及代码位置)。
 您只需将名称Registeration.accdb替换为完整路径(例如jdbc:ucanaccess:// c://folder/Registeration.accdb)。而且您最好阅读ucanaccess网站;-)

10-04 17:51