日期:2014-05-20  浏览次数:20647 次

关于java与数据库的问题
我用的是Access,我需要对我数据库里面的借书,书目管理,这个两个表进行更新操作,下面是我的代码,但是系统总是提示SQLException错误,这到底是为什么,顺便说一下,我数据库里的数据都是文本格式的。


[code=Java][/code]public boolean BorrowInformation(String NO,String BookNO){
Connection conn = getConnection();
  String s = new String("");
  int i = 0;
  int j = 0;

try{
PreparedStatement pstmt = conn.prepareStatement("select 是否可借,可借数量,已借出数量 from 书目管理 where 图书编号 = ?");
pstmt.setString(1,BookNO);

rs = pstmt.executeQuery();
rs.next();
  i = Integer.parseInt(rs.getString(2));
  j = Integer.parseInt(rs.getString(3)); 
   
if(rs.getString(1).equals("否")){
return false;
}
if(i<1 ){
return false;
}

pstmt = conn.prepareStatement("insert into 借书 values(?,?,?,?,?)");
pstmt.setString(1,BookNO);
pstmt.setString(2,NO);
pstmt.setString(3,"2012/2/23");
pstmt.setString(4,"2012/3/23");
pstmt.setString(5,"1");

if(pstmt.executeUpdate()>0){
i--;j++;
try{
PreparedStatement pstmt1 = conn.prepareStatement("update 书目管理 set 可借数量 = ? where 图书编号 = ?");
pstmt1.setString(1,Integer.toString(i));
pstmt1.setString(2,BookNO);
 
PreparedStatement pstmt2 = conn.prepareStatement("update 书目管理 set 已借出数量 = ? where 图书编号 = ?");
pstmt2.setString(1,Integer.toString(j));
pstmt2.setString(2,BookNO);

if(pstmt1.executeUpdate()>0&&pstmt2.executeUpdate()>0)
return true;
}
catch(SQLException e){
System.out.println("pstmt1");
}

}
 
conn.close();
}
catch(SQLException e){
System.out.println("SQLException");
}
return false;
}


------解决方案--------------------
pstmt.setString(3,"2012/2/23");
pstmt.setString(4,"2012/3/23");
这两个是日期格式吧?!access里日期好像要用#2012/2/23#,记不清楚了,你试试吧.........