There is a good article here on the topic in general - http://www.javaworld.com/javaworld/javaqa/2001-06/04-qa-0629-excel.html
Although I'm not sure you can set the password in excel through Java's JDBC as the OCBD driver has many SQL commands unsupported (for example it is not possible to insert, update, alter or delete with excel but you can select).
ussually you would use
statement.executeUpdate("SET PASSWORD FOR user=PASSWORD(password)"); or
statement.executeUpdate("SET PASSWORD=PASSWORD(password)");for current user;
Bamkin
Dear Bamkin ,
I used your code and paste it in my program. The error is as the follow:
Code:
stmnt = c.createStatement();
String query = "select StudentNumber, Email, New_Email_Address from [Sheet1$] where Len(New_Email_Address) > 0";
System.out.println(query);
rs = stmnt.executeQuery(query);
stmnt.executeUpdate("SET PASSWORD=PASSWORD('Test')");
Error:
(Remark: Invalid SQL statement)
java.sql.SQLException: [Microsoft][ODBC Excel Driver] 柍澚揑 SQL 捖弎幃丟梐婜堊'DELETE'丄'INSERT'丄'PROCEDURE'丄'SELECT' 埥 'UPDATE' 丅
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3111)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:288)
at hk.gov.edb.util.ExcelHandle.updateNewEmail(ExcelHandle.java:414)
at hk.gov.edb.core.AppMain.doUpdate(AppMain.java:369)
at hk.gov.edb.core.AppMain$7.construct(AppMain.java:584)
at hk.gov.edb.util.SwingWorker$2.run(SwingWorker.java:127)
at java.lang.Thread.run(Thread.java:595)
Indeed, the SET command must be unsupported in OCDB Unfortunate.
I have on numerous occasions used a programme to extract everything from excel into a proper database where it can be fully manipulated rather than bother stuggling with Excel 'spreadsheets pretending to databases'.
My advice in most circumstances would be to scrap Excel as it is not designed for fast data retrieval by and programme besides Excel.
(If you don't mind my asking ) Why do you need to password protect the shpreadsheet? If your programme is running on the clientside, the user can open the spreadsheet without your programme. If your programme is running on the serverside, you can use your programme to not serve anything unless the user enters a password (use the programme to handle the password).
Bamkin