我的数据库字段有bianhao,date,time等,bianhao是我的关键字,我要根据编号修改date;在数据库中bianhao是int型,在本程序中的bianhao变量为字符串,该值从另一个程序传过来的.下面是我的程序,请大虾进来看看import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import   javax.swing.table.*;   
import   java.util.*;   
public class Modrec1 extends JFrame implements ActionListener
{
private Connection co;
 private   JPanel   pane1   =   null;
 private   JPanel   pane2   =   null;
 private   JPanel   pane3   =   null;
 private   JButton   insert =   new JButton("修改");
 private JTextField t1=new JTextField();
 private JTextField t2=new JTextField();
 private JTextField t3=new JTextField();
 private JTextField t4=new JTextField();
 private JTextField t5=new JTextField();
 private JTextField t6=new JTextField();
 private JTextField t7=new JTextField();
 private JTextField t8=new JTextField();
 private JTextField t9=new JTextField();
 private JTextField t10=new JTextField();
 private JTextField t11=new JTextField();
 private JTextField t12=new JTextField();
 private JTextField t13=new JTextField();
 private String bianhao;
 private Container c;
public  Modrec1(Connection con2,String cellValue2)
{
JFrame frame   =   new   JFrame("按日期");
pane1   =   new   JPanel(null);  
pane2   =   new   JPanel(null);  
pane3   =   new   JPanel(null);  
co=con2;
bianhao=cellValue2;
bianhao1=Integer.parseInt(bianhao);

c=getContentPane();
pane1.add(new JLabel("请填写需要修改的内容"));
JLabel l1=new JLabel("*请输入日期(20080101)",SwingConstants.LEFT);

l1.setBounds(12,12,175,30);
  pane2.add(l1);
     t1.setBounds(190,12,175,30);
pane2.add(t1);
pane2.add(insert);
insert.setBounds(400,300,100,50) ; 
c.add(pane1,BorderLayout.NORTH);
c.add(pane2,BorderLayout.CENTER);
c.add(pane3,BorderLayout.SOUTH); 
insert.addActionListener(this);

setSize(1280,960);
setVisible(true); 
}
public void actionPerformed(ActionEvent e)
{
String date1;


try
{

 
        if (!(t1.getText().equals("")))
{
 Statement stmt=co.createStatement(); 
 date1=t1.getText();
PreparedStatement pstmt=co.prepareStatement("update table1 set date = ? where bianhao = ?");

pstmt.setString(1,date1);

pstmt.setString(2,bianhao);

pstmt.executeUpdate();
pstmt.close();

  } 
}
    catch(SQLException e3)
  {
   JOptionPane.showMessageDialog(null,"数据库插入失败"); 
e3.printStackTrace();   }
  
}

标红的地方出错,如下:java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] UPDATE 语句的语
法错误。
        at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbc.SQLExecute(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(Unknown Source)        at Modrec1.actionPerformed(Modrec1.java:86)

解决方案 »

  1.   

    两个参数都是字符串?,sql中字符串要用单引号
      

  2.   

    编号在数据库中是int型的,但是你给赋值的确实String类型的,语句会拼成这样:
    update table1 set date = '20090303' where bianhao = '1'没用java玩过Access,不知道Acess见到这样的语句会不会报错。不妨给bianhao设置前转一下。pstmt.setString(2,Integer.decode(bianhao)); (有出现类型转换异常的可能)
      

  3.   

    PreparedStatement pstmt=co.prepareStatement("update table1 set date = ? where bianhao = ?"); pstmt.setString(1,date1); pstmt.setString(2,bianhao); 改成PreparedStatement pstmt=co.prepareStatement("update table1 set date = '"+date1+"' where bianhao = '"+bianhao+"'); 试试
      

  4.   

    pstmt.setInt(2,Integer.decode(bianhao)); 不好意思,上面写错了。
      

  5.   

    根据大虾们的方法写了一遍,但是都没有解决access语法错误的问题,真不知道应该怎么办,无论如何,都谢谢大虾们的指点
      

  6.   

    没弄过这个不过个人觉的应该是数据类型转换出了问题!你应该擦看下java于access数据转换格式。
    呵呵个人愚见希望能帮到你
      

  7.   

    pstmt.set(2,Integer.parseInt(bianhao));
      

  8.   

    既然知道了是语句错误 那就对语句下手咯
      1.确定类型
          数据库存储类型和java参数类型一致
      2.语法
        sql的语法
          比如说sql字段字符要加(''),必须统一你的类型
    基本上搞定了    语句语法也就解决了!
      

  9.   


    date1=t1.getText(); 
    PreparedStatement pstmt=co.prepareStatement("update table1 set date = ? where bianhao = ?"); pstmt.setString(1,date1); pstmt.setString(2,bianhao); //改为pstmt.setInt(2,Integer.parseInt(bianhao.trim()));看看pstmt.executeUpdate(); pstmt.close(); //而且这边我不知道LZ还有没有对数据库进一步操作,如果没有的话建议connection.close()这个也要释放
      

  10.   

    PreparedStatement  setDate 
      

  11.   

    依我愚见,应该专门写一个DAO类。其次可以确定是update语句拼接出错,把不确定的值用''包起来。还就就是建议分开写,再写一个DAO类public class Dao{
      public Connection getConnection()throws Exception{
         Class.forName(com....); 
         string url = "...";
         return DriverManager....
      }  public boolean updateData(//对象){
        //在这里想做什么就做什么
      }
    }
      

  12.   

    date、time 是数据库保留字,建议改成别的名字,如:date1,time1
      

  13.   

    就是把你的字段名 date 改成 date1,time 改成 time1
      

  14.   

    我写错了点,把
    把PreparedStatement pstmt=co.prepareStatement("update table1 set date = ? where bianhao = ?"); pstmt.setString(1,date1); pstmt.setString(2,bianhao); 改成PreparedStatement pstmt=co.prepareStatement("update table1 set date = '"+date1+"' where bianhao = '"+bianhao+"'"); 再试试