Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Database/JDBC/SQL/JDO/Hibernate  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 java+Access出现只能读不能写的问题,望知道的朋友给与帮助!
wanghang110119





发贴: 47
积分: 0
于 2006-08-17 09:58 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我用java连接Access数据库,连接成功后,发现只能检索,但是不能插入和修改
检索代码:
import java.sql.*;
public class r2
{
public static void main(String args[]) throws SQLException,ClassNotFoundException
{
try
{
String url="jdbc:odbc:hh";
String user="";
String password="";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection c=DriverManager.getConnection(url,user,password);
Statement s=c.createStatement();
ResultSet rs=s.executeQuery("select name from b1 where id=2");
while(rs.next())
  {
  String s1=rs.getString("name");
  System.out.println(s1);
  }
s.close();
}
catch(ClassNotFoundException cnfe)
{
System.out.println("cuowu");
}
catch(SQLException cnfe)
{
System.out.println("youcuowu");
}
catch(Exception e)
{
System.out.println("haishicuowu");
}

}
}

插入代码:
import java.sql.*;
public class rr{
public static void main (String args[])
{
System.out.println("操作数据库");
try {
String strurl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=db1.mdb";//不用设置数据源,直接调用数据库
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //驱动程序
Connection conn = DriverManager.getConnection(strurl); //建立连接对象
Statement stmt1 = conn.createStatement();
System.out.println("数据库添加记录操作:");
String sql;
sql="insert into b1 (id,name) values (5,6)";
stmt1.executeUpdate(sql);
Statement stmt = conn.createStatement();
System.out.println("数据库添加记录成功!"+sql);
}
catch(ClassNotFoundException cnfe)
{
System.out.println("cuowu");
}
catch(SQLException cnfe)
{
System.out.println("youcuowu");
}
catch(Exception e)
{
System.out.println("haishicuowu");
}
}
}

两个程序运行之后都没有报错,但是插入之后在表中没有显示我的结果
希望有经验的朋友给与帮助,万分感谢!



只有去做才知道行不行!
作者 Re:java+Access出现只能读不能写的问题,望知道的朋友给与帮助! [Re:wanghang110119]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2006-08-17 10:21 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Have you tried the same connection method as in "select"?

Have you tried applying directly in your Access database
insert into b1 (id,name) values (5,6)
?

int numRowsAffected = stmt1.executeUpdate(sql);
to check the number of rows inserted.



作者 Re:java+Access出现只能读不能写的问题,望知道的朋友给与帮助! [Re:wanghang110119]
wanghang110119





发贴: 47
积分: 0
于 2006-08-18 15:06 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
谢谢元老同志,我今天做了几次测试,在别的电脑上运行正常
所以我推断应该是我的数据库的权限问题。



只有去做才知道行不行!
作者 Re:java+Access出现只能读不能写的问题,望知道的朋友给与帮助! [Re:wanghang110119]
dreamtobetrue





发贴: 18
积分: 0
于 2006-09-24 16:58 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我的程序也出现了搂主这样的情况
import java.sql.*;
public class Access
{

public static void main(String[] args)
{
try
{
String dburl= "jdbc:odbc:MS Access Database";
String dbuser="";
String dbpassword="";
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();
}
}
}
不会抱错!没有抛出异常
编译运行成功后,在数据库favs表中找不到我要插入的那行数据
但是可以读数据,望碰到过这种问题的朋友指点一下!



作者 Re:java+Access出现只能读不能写的问题,望知道的朋友给与帮助! [Re:wanghang110119]
dreamtobetrue





发贴: 18
积分: 0
于 2006-09-24 17:00 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我测试过好长时间,换过电脑用过,还是这种情况
我用的是office2003中的Access数据库,xp系统
希望大家帮助一下,感谢!




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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