Insert record into Microsoft Access

Hi folks:

I followed The JAVA TM Tutorial and used JDBC-ODBC bridge creating a database in Microsoft Access, it worked .

NAMEVARCHAR(32)(PRIMARYKEY),

PID INTEGER

PRICEFLOAT

SALESINTEGER

TOTALINTEGER

The following code is to insert the record into Table5. It has been well compiled and executed with no exception message, but I can not get the record in the Access actually. Could you kindly tell me the problem?

Thank you very much

Dawei

public class Insert {

public static void main (String args[]) {

String Url = "jdbc:odbc:mage";

String user = "";

String password ="";

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch (Exception e) {

System.out.println("Failed to load JDBC/ODBC driver.");

return;

}

try {

Connection con=DriverManager.getConnection(Url,user,password);

Statement statement=con.createStatement();

=====================================================

statement.executeUpdate(

"INSERT INTO Table4 " +

"VALUES ('Cola', 101, 7.99, 0,10)");

====================================================

} catch (Exception e) {

e.printStackTrace();

}

}

}

[1317 byte] By [daweiguan] at [2007-9-26 2:58:33]
# 1
You say Table5, but your code says Table4. Is that a typo, or is that perhaps the problem?Michael Bishop
mbishop78 at 2007-6-29 10:53:35 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
sorry it is a tying mistake of my message, Table4 is right.Dawei
daweiguan at 2007-6-29 10:53:35 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
It is not the problem.Dawei
daweiguan at 2007-6-29 10:53:35 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
You list 5 fields for the table. Your code is only inserting 4 values. You need to either make the insertion field names explicit or add 5 values.
jschell at 2007-6-29 10:53:35 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
HiI checked my code again, it is five values"VALUES ('Cola', 101, 7.99, 0, 10)");Dawei
daweiguan at 2007-6-29 10:53:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
>but I can not get the record in the Access actually. How do you know?Do you use MS Access to look? Then maybe you are looking at the wrong database.
jschell at 2007-6-29 10:53:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7
HiTo check the result, I just simply open the corresponding database mage and Table4 in Access after I execute the program. There is nothing.In addition I have found there is a file(*.idb) generated after I execute the class every time. Dawei
daweiguan at 2007-6-29 10:53:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8
try closing the connection to database. that might do the trick
srivya at 2007-6-29 10:53:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9
It works. thanks.Dawei
daweiguan at 2007-6-29 10:53:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...