保存数据的问题

时间:2011-04-09 06:38:28

标签: java ms-access

我的代码正常工作它正确地保存数据但是当我添加两个字段,即date和birthdate时,它给出了错误,即插入到语句中的syntex错误。我正在使用MS ACCESS for DB。在我的数据库中我使用了这两个字段的数据类型名词(即日期和更新) 我的代码是:

private void saveREGISTRATION1(java.awt.event.ActionEvent evt){
 int len,len1,len2;

    int regno= Integer.parseInt(cbregn.getSelectedItem().toString());
    if(cbregn.getSelectedItem().toString().equals("")){
    JOptionPane.showMessageDialog(null," SELECT THE REGISTRATION NO ");
    return;
    }


      int date=Integer.parseInt(tdate.getText());
      if(tdate.getText().equals(""))
      JOptionPane.showMessageDialog(null," ENTER THE DATE ");

//// String date = tdate.getText(); // if(date.equals(“”)) // JOptionPane.showMessageDialog(null,“输入日期”);

    String nm= cbnm.getSelectedItem().toString();
    if(nm.equals("")){
    JOptionPane.showMessageDialog(null," ENTER THE NAME ");
    return;
    }


    String place=tfplace.getText();
    if(place.equals("")){
    JOptionPane.showMessageDialog(null," ENTER THE NATIVE PLACE ");
    return;
    }



    String kul=tfkul.getText();
    if(kul.equals("")){
    JOptionPane.showMessageDialog(null," ENTER THE NAME OF KUL ");
    return;
    }

    String gotra=tfgotra.getText();
    if(gotra.equals("")){
    JOptionPane.showMessageDialog(null," ENTER THE GOTRA NAME ");
    return;
    }

    String kswami=tfswami.getText();
    if(kswami.equals("")){
    JOptionPane.showMessageDialog(null," ENTER THE KULSWAMI NAME ");
    return;
    }

    String raddr=taraddr.getText();
    if(raddr.equals("")){
    JOptionPane.showMessageDialog(null," ENTER THE RESIDENSIAL ADDRESS ");
    return;
    }

    int pincode=Integer.parseInt(tfpcd.getText());
    len1 = tfpcd.getText().length();
    if(len1!=7) {
    JOptionPane.showMessageDialog(null,"Enter The 7 Digit Pin Code","Error",JOptionPane.ERROR_MESSAGE);
    return;
    }

    int stdcd=Integer.parseInt(tfstdcode.getText());
    if(tfstdcode.getText().equals("")){
    JOptionPane.showMessageDialog(null," ENTER THE STD CODE ");
    return;
    }


    int tele=Integer.parseInt(tftele.getText());
    len2 = tftele.getText().length();
    if(len2!=7){
    JOptionPane.showMessageDialog(null,"Enter The 7 Digit Telephone No","Error",JOptionPane.ERROR_MESSAGE);
    return;
    }


    int mno=(int) Long.parseLong(tfmno.getText());
    len = tfmno.getText().length();
    if(len!=10) {
    JOptionPane.showMessageDialog(null,"Enter The 10 Digit Mobile No","Error",JOptionPane.ERROR_MESSAGE);
    return;
    }


    String email=tfemail.getText();
    if(email.equals(""))
    JOptionPane.showMessageDialog(null," ENTER THE EMAIL");

    if(email.equals("")) {
        tfemail.setText("-");

    } else // check if it is a valid email-id
    {
    int ind = email.indexOf("@");
        if (ind != -1) { } else {
            JOptionPane.showMessageDialog(null,"Invalid Email Id","Error",JOptionPane.ERROR_MESSAGE);
            tfemail.setText("");
            tfemail.requestFocus();
            return;
        }
    }
    String website=tfweb.getText();
    if(website.equals(""))
    JOptionPane.showMessageDialog(null," ENTER THE WEBSITE ");

    if(website.equals("")) {
        tfweb.setText("-");
    }
    else // check if it is a valid email-id
    {
    int ind = website.indexOf("www");
    if (ind != -1) { } else {
    JOptionPane.showMessageDialog(null,"Invalid Website","Error",JOptionPane.ERROR_MESSAGE);
    tfweb.setText("");
    tfweb.requestFocus();
    return;
   }
}

    String education=tfedu.getText();
    if(education.equals("")){
    JOptionPane.showMessageDialog(null," ENTER THE EDUCATION DETAILS");
    return;
   }

    String branch=tfbrch.getText();
    if(branch.equals("")){
    JOptionPane.showMessageDialog(null," ENTER THE BRANCH NAME ");
    return;
   }


  int brthdt=Integer.parseInt(tfbdt.getText());
  if(tfbdt.getText().equals(""))

// String brthdt = tfbdt.getText(); // if(brthdt.equals(“”))       JOptionPane.showMessageDialog(null,“输入出生日期”);

    String bloodgroup=(String)cbbldgrp.getSelectedItem();
    if(bloodgroup.equals("")){
    JOptionPane.showMessageDialog(null," SELECT THE BLOODGROUP");
    return;
   }

    try
    {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con=DriverManager.getConnection("jdbc:odbc:wanisamajDB");
        Statement stmt=con.createStatement();
       //String qry= "INSERT INTO Registration1(RegistrationNo,SeniorPerson,NativePlace,Kul,Gotra,KulSwami,ResidensialAddress,PinCode,STDcode,TelephoneNo,MobileNo,Email,Website,Education,Branch,BloodGroup) VALUES('"+regno+"','"+nm+"','"+place+"','"+kul+"','"+gotra+"','"+kswami+"','"+raddr+"','"+pincode+"','"+stdcd+"','"+tele+"','"+mno+"','"+email+"','"+website+"','"+education+"','"+branch+"','"+bloodgroup+"')";
        System.out.println("qry");
        String qry= "INSERT INTO Registration1(RegistrationNo,SeniorPerson,NativePlace,Kul,Gotra,KulSwami,ResidensialAddress,PinCode,STDcode,TelephoneNo,MobileNo,Email,Website,Education,Branch,BloodGroup,Date,BirthDate) VALUES('"+regno+"','"+nm+"','"+place+"','"+kul+"','"+gotra+"','"+kswami+"','"+raddr+"','"+pincode+"','"+stdcd+"','"+tele+"','"+mno+"','"+email+"','"+website+"','"+education+"','"+branch+"','"+bloodgroup+"','"+date+"','"+brthdt+"')";
        stmt.executeUpdate(qry);
        JOptionPane.showMessageDialog(null,"RECORD IS SAVED SUCCESSFULLY ");
        con.close();

    }
    catch(SQLException eM) {
        System.out.println(" "+eM);
        JOptionPane.showMessageDialog(null,"RECORD IS NOT SAVED");
    } 
    catch(Exception et)
    {
        System.out.println("error:"+et.getMessage());
    }

}

3 个答案:

答案 0 :(得分:1)

您的Java代码必须生成一个INSERT语句,Access'数据库引擎可以接受该语句。

在Access中,假设两个字段都是日期数据类型,则此语句应该有效。

INSERT INTO tblFoo([Date],BirthDate) Values (#2011/04/09#, #1960/05/10#)

注意我在方括号中附上了第一个字段的名称。我这样做是为了让数据库引擎知道Date是表中字段的名称,不要与Date()函数混淆。

Access SQL使用#作为日期文字值的分隔符。

但是,您指出Date和BirthDate字段是数字而不是日期数据类型。如果是真的,这对我来说似乎是一个设计错误。但无论哪种方式,您现有的INSERT语句都会尝试将文本值插入Date和BirthDate字段。仔细查看qry变量赋值的简化版本。

String qry= "INSERT INTO Registration1([Date],BirthDate) VALUES('"+date+"','"+brthdt+"')";

如果Date和BirthDate是日期数据类型,请将分隔符从单引号更改为哈希字符。

String qry= "INSERT INTO Registration1([Date],BirthDate) VALUES(#"+date+"#,#"+brthdt+"#)";

如果Date和BirthDate实际上是数字,请将它们更改为日期数据类型。

从根本上说,当您显然对Access的工作方式一无所知时,您正试图将Java与Access数据库一起使用。因此,您的Access问题更难以识别,因为它在所有冗长的Java内容中都被模糊了。

我怀疑通过在Access会话中创建SQL语句可以获得更多成功。获得数据库引擎可以无错误地执行的SQL语句后,使用Java代码重新创建该SQL。

答案 1 :(得分:0)

您在数据库中为插入date和birthdayDate采用了哪种数据类型? 正如你所说,你使用数字数据类型来存储这些数据,将它们改为日期数据类型。

答案 2 :(得分:0)

请使用井号/井号'#'

,而不是用引号括住日期字符串
,'#+date+#','#+brthdt+#')";