2012-01-12 157 views
0

任何人都可以解釋我什麼類型的微軟數據庫異常是?這是什麼意思,「|」 ??無法打開數據庫'|'

我使用這個代碼:

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; 
} 

插入和選擇(INT ID)工作properly..the最後一個給出了錯誤..

不知道,但如果它的事項我「M使用.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(); 
    } 
} 

}

我需要保存數據庫中的臨時文件,因爲當項目工作,我應該數據庫和項目導出爲一個單一的.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) 

我也注意到,只有當我嘗試做一個選擇了records..maybe的大量出現該錯誤是一個Microsoft Access限制嗎?

+0

這種方法是什麼樣的? 'SingletonConnection2.getInstance();' – 2012-01-12 16:32:14

+0

我添加了代碼.. – 2012-01-12 16:41:55

+0

而且一個堆棧跟蹤會幫助很大。 – kba 2012-01-12 20:04:13

回答

1

對此的MS Access錯誤信息......

Cannot open database '|' 

...你問 「這是什麼意思的是 」|「?

管道字符是一個佔位符數據庫名稱。顯示錯誤消息時,該字符將被db名稱替換。

由於在您的情況下沒有更換管道,這表明數據庫引擎沒有收到數據庫名稱。不幸的是我不懂Java。