无法打开数据库'|'

时间:2012-01-12 16:29:22

标签: sql ms-access select

任何人都可以解释一下这种类型的Microsoft数据库异常是什么吗?! “|”是什么意思??

我正在使用此代码:

public static int insert(int ms, String linea) throws ConnessioneException {

    try {

     Connection conn=SingletonConnection2.getInstance();

     PreparedStatement ps=conn.prepareStatement("INSERT INTO calls" +
            "(ms,content) VALUES (?,?)");

     ps.setInt(1, ms);
     ps.setString(2, linea);

     ps.executeUpdate();


    }catch (SQLException ex){
        ex.printStackTrace();

    }catch(Exception e) {
        e.printStackTrace();
        //ECCEZIONE GENERALE
        throw new ConnessioneException();
    }
    return ms;

}

public static ArrayList<String> select(int id) throws ConnessioneException {

    ArrayList<String> array=new ArrayList<String>();

    try {
     Connection conn=SingletonConnection2.getInstance();
     PreparedStatement ps=conn.prepareStatement("SELECT * FROM calls WHERE ms=? ");
     ps.setInt(1,id);
     ResultSet rs = ps.executeQuery();

    while (rs.next()){
        String s=rs.getString("content");
        array.add(s);
    }

        rs.close();
        ps.close();
    } catch (SQLException e1) {
        e1.getMessage();
        e1.printStackTrace();
        throw new ConnessioneException();

    }
    return array;
}

public static ArrayList<Integer> getAllMs() throws ConnessioneException {

    ArrayList<Integer> array=new ArrayList<Integer>();

    try {
     Connection conn=SingletonConnection2.getInstance();
     PreparedStatement ps=conn.prepareStatement("SELECT DISTINCT ms FROM calls");
     ResultSet rs = ps.executeQuery();

    while (rs.next()){
        int s=rs.getInt("ms");
        array.add(s);
    }

        rs.close();
        ps.close();
    } catch (SQLException e1) {
        e1.getMessage();
        e1.printStackTrace();
        throw new ConnessioneException();

    }
    return array;
}

insert和select(int id)正常工作..最后一个给出了错误..

不知道是否重要,但我正在使用.mdw文件..

类SingletonConnection2如下:

public class SingletonConnection2 {

private static Connection conn;
private static String driverConnection;
private static String stringConnection;
private static String databaseName="";
private static String idConnection="";
private static String passConnection="";


private SingletonConnection2() throws ConnessioneException{


    idConnection = "root";
    passConnection = "";

    String slash="\\";

    String path=null;

    String temp=System.getProperty("java.io.tmpdir");
    if ( !(temp.endsWith("/") || temp.endsWith("\\")) )
           temp = temp + System.getProperty("file.separator");
    File tempDir = new File(temp);

    File temporaryFile = new File(tempDir, "db.mdw");

    InputStream templateStream = getClass().getResourceAsStream("db.mdw");

    try {
        IOUtils.copy(templateStream, new FileOutputStream(temporaryFile));

    } catch (FileNotFoundException e1) {
        e1.printStackTrace();

    } catch (IOException e1) {
        e1.printStackTrace();

    }catch (Exception e1){
        e1.printStackTrace();
    }

    path = temporaryFile.getAbsolutePath();



    driverConnection = "sun.jdbc.odbc.JdbcOdbcDriver";
    stringConnection = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+path;

    try {
        Class.forName(driverConnection);
        conn = DriverManager.getConnection(stringConnection,idConnection,passConnection);
    } catch (Exception e) {
        e.printStackTrace();
        throw new ConnessioneException(e.getStackTrace());
    }
}

public static Connection getInstance()throws ConnessioneException{
    if(conn==null)
        new SingletonConnection2();

    return conn; 
}


public static void closeConnection(){
    try {
        conn.close();

    } catch (SQLException e) {

        e.printStackTrace();
    }
}

}

我需要将db保存在临时文件中,因为当项目工作时,我应该将数据库和项目导出为单个.jar

任何想法为什么会发生?


堆栈跟踪如下:

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Cannot open database '|'.  It may not be a database that your application recognizes, or the file may be corrupt.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(Unknown Source)
at DAO.RiordinaDAO.insert(RiordinaDAO.java:27)

我还注意到只有当我尝试对大量记录进行选择时才会出现该错误。可能是Microsoft Access限制吗?

0 个答案:

没有答案