使用JDBC和编译将Microsoft Access数据库连接到Java [英] Connecting a Microsoft Access Database to Java using JDBC and compiling

查看:215
本文介绍了使用JDBC和编译将Microsoft Access数据库连接到Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我们正在制作数据库程序(用户GUI和数据库)的学校数据库项目。使用Microsoft Access 2010我创建了数据库,并用一些示例数据填充它,并将其保存为.mdb格式,并将其放置在我的项目文件夹中。

for a school database project we are making a database program (user GUI and the database). Using Microsoft Access 2010 I created the database and populated it with some sample data, and saved it in .mdb format and placed it in my project folder.

eclipse下面的代码工作正常,连接甚至检索查询。但是,我发现我无法将代码导出到一个jar并运行它(这是项目需要,给他们一个工作副本的程序在CD或闪存驱动器),我也无法移植代码转换到Netbeans使其工作,以及尝试在Linux机器上编译。

When running it in eclipse the following code works fine, connects and even retrieves the query. However I find that I am unable to export the code to a jar and run it (which is required for the project, give them a working copy of your program on a CD or flash drive), and I'm also unable to port the code over to Netbeans to have it work, as well as trying to compile on a Linux machine.

我认为这是包含驱动程序或尝试使用Microsoft访问的问题。我在运行jar或运行在Netbeans时得到的错误在代码下面给出。所以我问我如何包括驱动程序使程序可移植,或者我怎么能解决这个问题?

I assume this is a problem with including drivers or trying to use Microsoft access. The error I get when running the jar or running on Netbeans is given below the code. So I ask either how do I include drivers to make the program portable, or how else can I approach this problem?

提前感谢

import java.sql.*;

public class JDBCTest {
    static Connection connection;
    static Statement statement;

    public static void main(String args[]){

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
            String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=TLDATABASEDBM.mdb";
            connection = DriverManager.getConnection( database ,"",""); 

            buildStatement();
            executeQuery();

        }catch(Exception e){
            e.printStackTrace();
            System.out.println("Error!");
        }
    }

    public static void buildStatement() throws SQLException {
        statement = connection.createStatement();
    }

    public static void executeQuery() throws SQLException {

        boolean foundResults = statement.execute("SELECT * FROM tblStaff  AS x WHERE City='Calgary'");
        if(foundResults){
            ResultSet set = statement.getResultSet();
            if(set!=null) displayResults(set);
        }else {
            connection.close();
        }
    }

    public static void displayResults(ResultSet rs) throws SQLException {
        ResultSetMetaData metaData = rs.getMetaData();
        int columns=metaData.getColumnCount();
        String text="";

        while(rs.next()){
            for(int i=1;i<=columns;++i) {
                text+=""+metaData.getColumnName(i)+":\t";
                text+=rs.getString(i);
                //text+="</"+metaData.getColumnName(i)+">";
                text+="\n";
            }
            text+="\n";
        }

        System.out.println(text);

    }
}

p>

The error mentioned above:


java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
        at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
        at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
        at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3073)
        at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
        at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:207)
        at tldatabase.DataConnect.makeConnection(DataConnect.java:35)
        at tldatabase.Main.main(Main.java:24)


推荐答案

我知道这篇文章是在几年前,但我想回答那些刚刚经历这个问题的人的问题。我花了一会儿知道问题的答案,所以这里是解决方案:

I know the post was years ago but I felt like answering the question for those who are just experiencing this right now. It took me a while to know the answer to the question so here's the solution:

http://wiki.netbeans.org/FaqSettingHeapSize

按照运行32位JVM。

Follow the "Running the 32-bit JVM".

所有你需要做的是在你的netbeans的安装文件夹中的netbeans.conf,并更改目录从这样:

All you have to do is find the netbeans.conf in the installation folder of your netbeans and change the directory from something like this:

netbeans_jdkhome = C:\Program Files \Java\jdk1.6.0_24

netbeans_jdkhome="C:\Program Files\Java\jdk1.6.0_24"

netbeans_jdkhome =C:\Program Files(x86)\Java\jdk1.6.0_21

netbeans_jdkhome="C:\Program Files (x86)\Java\jdk1.6.0_21"

问题是netbeans可能正在64位运行,只支持32位。所以这样做有希望解决问题。另外请务必安装:

The problem is netbeans might be running in 64 bit but MS Access only support 32-bit. So doing this would hopefully solve the problem. Also make sure to install this:

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=23734

这篇关于使用JDBC和编译将Microsoft Access数据库连接到Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆