Может кто-нибудь объяснить мне, что это за исключение базы данных 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;
}
вставка и выбор (int id) работают правильно .. последний выдает эту ошибку ..
donНе знаю, имеет ли это значение, но я использую файл .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
Есть идеи, почему это происходит?
Stacktraceвыглядит следующим образом:
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?