Java连接到数据库,期望它在工作区

时间:2017-02-11 00:42:08

标签: java mysql database eclipse

目前我正在尝试运行以下用于连接到MS Access数据库的代码。代码似乎正在加载正确的驱动程序并连接到数据库,但每当我尝试执行查询时,它都会返回一个SQL异常,声称它无法找到数据库。但是,它似乎认为数据库位于我的Eclipse项目工作空间中,考虑到它之前如何成功连接到数据库,这一点尤为奇怪。如何指定程序在哪里查找数据库?

public class DatabaseClass {
    public static void main(String[] args) {
        try {
             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
             System.out.println("driver loaded");
        } //end of try
             catch(java.lang.ClassNotFoundException e) {
             System.err.print("ClassNotFoundException: ");
             System.err.println(e.getMessage());
        }//end of catch
        try {
            Connection conn = DriverManager.getConnection("jdbc:odbc:myDatabase");
            System.out.println("Connected to the database");
            conn.setAutoCommit(false);
            Statement st = conn.createStatement();
            String query = "<query goes here>";
            ResultSet rs = st.executeQuery(query);
            // handle Results here
        }//end of try
        catch (SQLException se){
             System.out.println("Not connected to database");
             System.out.println(se);
        }//end of catch

    }
}

0 个答案:

没有答案