Topic: java+access表数据不能修改

  Print this page

1.java+access表数据不能修改 Copy to clipboard
Posted by: dreamtobetrue
Posted on: 2006-09-25 08:08

import java.sql.*;
public class Access
{
  
  public static void main(String[] args)
  {
    try
    {
   String dburl= "jdbc:odbc:acc";
            String dbuser="acc";
            String dbpassword="123456";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn1 = DriverManager.getConnection(dburl,dbuser,dbpassword);
           Statement stmt1 = conn1.createStatement();
           stmt1.executeUpdate("insert into favs (f_title,f_url,f_username,f_time) values ('你的','www.tom.com ','挪威的森林','2006-2-19 12:45:19')");
           /*ResultSet rs=stmt1.executeQuery("select * from favs");
             while(rs.next())
      {
            String title=rs.getString("f_title");
            System.out.println(title);
      }*/
      
    }
    catch (ClassNotFoundException cnfe)
    {
      cnfe.printStackTrace();
}
catch(SQLException cnfe)
{
cnfe.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
    }
  }
}
程序没有抱错,编译运行正常,我感觉是不是我的Access数据库设置有问题。
望各位指点!

2.Re:java+access表数据不能修改 [Re: dreamtobetrue] Copy to clipboard
Posted by: why
Posted on: 2006-09-25 09:36

Don't double post!

Have you checked whether your mdb file is not read-only?
Have you tried running the insert statement inside Access?

3.Re:java+access表数据不能修改 [Re: dreamtobetrue] Copy to clipboard
Posted by: dreamtobetrue
Posted on: 2006-09-25 17:13

元老同志,多谢
1)我查过我的Access文件,不是只读形式的,而且数据源也不是只读的
2)我在我的Access文件中可以直接插入信息,但是用程序直接操作insert
就会失败。
3)我换了电脑试了试,还是没有成功。接着office 2000替换了office 2003
还是同样的问题。
感觉比较困惑,因为我的程序我自己感觉是没有问题的,应该是Access数据库的问题,但不知道问题所在。

4.Re:java+access表数据不能修改 [Re: dreamtobetrue] Copy to clipboard
Posted by: why
Posted on: 2006-09-25 18:04

check the return value of executeUpdate and see whether it is 0

try adding a ; at the end of the insert statement string

try PreparedStatement
I am wondering if f_time is of type DateTime, I won't pass a String to it like that, though it may be allowed.

程序没有


Just did a Google search. and found this
http://forum.java.sun.com/thread.jspa?threadID=597260&messageID=3178446
or
http://forum.java.sun.com/thread.jspa?threadID=712716&messageID=4122465

5.Re:java+access表数据不能修改 [Re: dreamtobetrue] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2006-09-25 18:27

哈哈,我知道你的问题。。但是我不是很清楚了。。

你的问题是没有把Connection 关掉。。conn1.close()还是stmt.close()我记不清了。

工作了以后快来谢谢我。。

嘉帆。

6.Re:java+access表数据不能修改 [Re: dreamtobetrue] Copy to clipboard
Posted by: dreamtobetrue
Posted on: 2006-09-26 10:14

多谢元老同志指出我的错误!
我以后会注意细节的。

7.Re:java+access表数据不能修改 [Re: dreamtobetrue] Copy to clipboard
Posted by: dreamtobetrue
Posted on: 2006-09-26 10:16

HenryShanley
多谢!果然是这个问题,我加上去之后就成功了。
但是能不能告诉我为什么,因为我在操作别的数据库如MS SQL ,MySQL的时候
没有关闭连接,同样可以进行插入或者Set操作。

8.Re:java+access表数据不能修改 [Re: dreamtobetrue] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2006-09-27 06:07

因为我也有犯过你同样的问题,你真得很走运,我记得我当时调试了很长很长时间才知道原因....痛苦啊
一段3000行的代码被我花了8个小时删到你上面的那几行代码才知道问题所在..血和泪阿..至于为什么我也不知道.
你还可以试一下同样的代码在GUI里面就可以运行的..很怪的

我已经从JDBC到ORM转型了..赶快去学Hibernate吧

嘉帆

9.Re:java+access表数据不能修改 [Re: dreamtobetrue] Copy to clipboard
Posted by: why
Posted on: 2006-09-27 11:42

dreamtobetrue wrote:
HenryShanley
多谢!果然是这个问题,我加上去之后就成功了。
但是能不能告诉我为什么,因为我在操作别的数据库如MS SQL ,MySQL的时候
没有关闭连接,同样可以进行插入或者Set操作。

In one of the link I mentioned above, it says:
You have to close the connection for Access to refelct the data changes. I believe the reason is mainly because Access is not a pure database and acts somewhat like file I/O.

In fact, you should make it a practice to close all the JDBC resources, i.e. ResultSet, Statement [includes PreparedStatement and CallableStatement] and the Connection Moon in every JDBC code and avoid all the problems that might follow later on. It is better to do so in a finally block to ensure that the resources are always closed.

Well, this is not really a reason but a guess.

10.Re:java+access表数据不能修改 [Re: why] Copy to clipboard
Posted by: nt
Posted on: 2006-09-27 13:43

强人啊,不关connection ,居然还讨论.....


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923