Hallo zusammen,
Ich habe in Access eine DB erstellt, kann mit Java auch draufzugreifen, bekomme alles ausgelesen, kann aber keinen INSERT machen. Die Fehlermeldung:
[Microsoft][ODBC Microsoft Access Driver] Unzulässige SQL-Anweisung; 'DELETE', 'INSERT', 'SELECT' oder 'UPDATE' erwartet.
Wo mach ich was falsch?
Output:
args[0]: F:\Entwicklung\Java\Access_Dbs\Depot.mdb
args[1]: Select * from Maintab
Datebase URL: jdbcdbcriver={Microsoft Access Driver (*.mdb)};DBQ=F:/Entwicklung/Java/Access_Dbs/Depot.mdb;DriverID=22;READONLY=false}
--------------------------
Query Result
--------------------------
Date Depot Nr sum_dep_now diff_yes_amount diff_yes_percent sum_start_amount win_amount win_percent
--------------------------
20010101 1 450000.78 123.00 null null 5.57 null
--------------------------
20010102 1 -12345.66 null null null 987.00 null
--------------------------
7878 0 770.00 .00 .00 .00 6780.00 .00
--------------------------
20010103 10 4567899.78 444567.78 -3.66 765767.99 7878.99 767.00
--------------------------
20020101 1 54553.55 5543.00 3454.00 3454.00 34555.00 567.00
--------------------------
Rows: 5
Resultset has 8 cols.
SQLException: [Microsoft][ODBC Microsoft Access Driver] Unzulässige SQL-Anweisung; 'DELETE', 'INSERT', 'SELECT' oder 'UPDATE' erwartet.
37000
Ich habe in Access eine DB erstellt, kann mit Java auch draufzugreifen, bekomme alles ausgelesen, kann aber keinen INSERT machen. Die Fehlermeldung:
[Microsoft][ODBC Microsoft Access Driver] Unzulässige SQL-Anweisung; 'DELETE', 'INSERT', 'SELECT' oder 'UPDATE' erwartet.
Wo mach ich was falsch?
Code:
import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
// First argument is database file name
// Second argument is your query in quotes
public class AccessJDBCTest {
public static void main(String args[]) {
System.out.println("args[0]: " + args[0] );
System.out.println("args[1]: " + args[1] );
// System.out.println("args[2]: " + args[2] );
if(args.length != 2) {
System.out.println("Example: java AccessJDBCTest database_name.mdb \"database query\"");
System.exit(1);
}
java.sql.Connection conn = null;
try {
conn = AccessJDBCUtil.getAccessDBConnection(args[0]);
Statement stmt = conn.createStatement();
conn.setReadOnly(false); // reicht nicht
if(stmt.execute(args[1])) {
printResultSet(System.out, stmt.getResultSet(), "Query Result");
} else {
System.out.println("DDL executed successfully");
}
ResultSet uprs;
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
uprs = stmt.executeQuery("SELECT * FROM Maintab");
// Check the column count
ResultSetMetaData md = uprs.getMetaData();
System.out.println("Resultset has " + md.getColumnCount() + " cols.");
String sql = "INSERT INTO my_table(col_string) VALUES ('QWERT')";
stmt.executeUpdate(sql);
// So scheint das richtig
String strInsert = "INSERT INTO Maintab(Date) VALUES ('20080611')" ;
stmt.executeUpdate( "\"" + strInsert + "\"" ) ;
uprs.close();
stmt.close();
conn.close();
} catch(SQLException s) {
System.err.println("SQLException: " + s.getMessage());
System.out.println(s.getSQLState());
} finally {
if(conn != null) {
try {
conn.close();
} catch(SQLException ignore) {}
}
}
}
/** Prints badly ResultSet to PrintStream */
public static void printResultSet(PrintStream p, ResultSet rs, String title) throws SQLException {
if(rs != null) {
ResultSetMetaData metaData = rs.getMetaData();
int cols = metaData.getColumnCount();
p.println("\n--------------------------\n" + title + "\n--------------------------");
for(int i = 1;i <= cols;i++) {
p.print(metaData.getColumnLabel(i) + "\t");
}
p.println("\n--------------------------");
int count = 0;
while(rs.next()) {
for(int i = 1;i <= cols;i++) {
p.print(rs.getString(i) + "\t");
}
p.println("\n--------------------------");
count++;
}
p.println("Rows: " + count);
}
}
}
Output:
args[0]: F:\Entwicklung\Java\Access_Dbs\Depot.mdb
args[1]: Select * from Maintab
Datebase URL: jdbcdbcriver={Microsoft Access Driver (*.mdb)};DBQ=F:/Entwicklung/Java/Access_Dbs/Depot.mdb;DriverID=22;READONLY=false}
--------------------------
Query Result
--------------------------
Date Depot Nr sum_dep_now diff_yes_amount diff_yes_percent sum_start_amount win_amount win_percent
--------------------------
20010101 1 450000.78 123.00 null null 5.57 null
--------------------------
20010102 1 -12345.66 null null null 987.00 null
--------------------------
7878 0 770.00 .00 .00 .00 6780.00 .00
--------------------------
20010103 10 4567899.78 444567.78 -3.66 765767.99 7878.99 767.00
--------------------------
20020101 1 54553.55 5543.00 3454.00 3454.00 34555.00 567.00
--------------------------
Rows: 5
Resultset has 8 cols.
SQLException: [Microsoft][ODBC Microsoft Access Driver] Unzulässige SQL-Anweisung; 'DELETE', 'INSERT', 'SELECT' oder 'UPDATE' erwartet.
37000