连接到 .accdb 文件时出现“给定文件不存在”

Posted

技术标签:

【中文标题】连接到 .accdb 文件时出现“给定文件不存在”【英文标题】:"Given file does not exist" when connecting to .accdb file 【发布时间】:2015-11-07 14:55:38 【问题描述】:

每次运行我的程序都会出错

给定文件不存在:Registeration.accdb

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

// 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


【问题讨论】:

【参考方案1】:

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

【讨论】:

这对我不起作用,我使用了这个:“jdbc:ucanaccess://C:/Users/Pharma/Database3.accbd”有什么想法吗? 是的,修改后我得到“用户缺少权限或找不到对象:用户”错误。 使用 3.0.4 你的版本太旧了

以上是关于连接到 .accdb 文件时出现“给定文件不存在”的主要内容,如果未能解决你的问题,请参考以下文章

尝试使用 pymssql 连接到数据库时出现 KeyError

连接到 HTTPS 站点时出现 SSLHandshakeException

ODBC 从 R 连接到网络驱动器上的 Access DB .accdb 文件

尝试连接到 localhost phpmyadmin 时出现错误

将文件添加到 Android 中的媒体库时出现“java.lang.IllegalStateException:未连接到 MediaScannerService”

从 C# 连接到 .accdb。 “无法识别的数据库格式”错误