本文介绍了在MSAccess 2000中插入值时获取运行时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面提到的代码在MSAccess 2000中插入值,其中包含如下所述的表结构: -



I am using below mentioned code to insert values in MSAccess 2000 which having table structure as mentioned below:-

Field Name	Data Type
TodaysDate	Date/Time
Cart ID	Number
Client Name	Text
Team & Segment	Text
Duration	Number
Tape ID	Text
Start Date	Date/Time
End Date	Date/Time







代码: -






Code:-

private boolean enterDataIntoMSAccessDatabaseusingPreparedStatement() 
    {
        try {
            
            ps = connection.prepareStatement("INSERT INTO [Cart ID Details] VALUES (?,?,?,?,?,?,?,?)");
            
            System.out.println("After Query");
            
        } 
        
        catch (SQLException se) {
            
            generateErrorMessage("Error in Prepared Statement \n " + se.getMessage() );
            
            return false;
            
        }
        
        catch (Exception e)
        {
            generateErrorMessage("Unexpected Error Occured \n " + e.getMessage());
        }
        
        String todaysDate = cartIDApplicationAddCartIDDatejTextField.getText().trim();
        
        String cartID = cartIDApplicationAddCartIDCartIDjTextField.getText().trim();
        
        String clientName = cartIDApplicationAddCartIDClientNamejTextField.getText().trim();
        
        String teamSegment = cartIDApplicationAddCartIDTeamAndSegmentjTextField.getText().trim();
        
        String duration = cartIDApplicationAddCartIDDurationjTextField.getText().trim();
        
        String tapeID = cartIDApplicationAddCartIDTapeIDjTextField.getText().trim();
        
        String startDate = cartIDApplicationAddCartIDStartDatejTextField.getText().trim();
        
        String endDate = cartIDApplicationAddCartIDEndDatejTextField.getText().trim();
        
        try {
        
            //System.out.println("Before ps.setString()");
            
            ps.setString(1, todaysDate);
            ps.setString(2, cartID );
            ps.setString(3, clientName);
            ps.setString(4, teamSegment);
            ps.setString(5, duration);
            ps.setString(6, tapeID);
            ps.setString(7, startDate);
            ps.setString(8, endDate);


            //System.out.println("After ps.setString()");
          
                ps.executeUpdate();
           
            
       } 
        catch (SQLException se) {
            
            generateErrorMessage("Error while inserting data in database \n " + se.getMessage());
            
            return false;
        }
        
        catch (Exception e)
        {
            generateErrorMessage("Unexpected Error Occured \n" + e.getMessage() );
        }
        
        return true;
    }     





上面的布尔函数在保存按钮动作事件中调用,但是当我点击保存时我得到了作为运行时异常。



请帮助我解决这个问题。



The above boolean function is called in "Save Button" action event, but when I click Save I am getting as Runtime Exception.

Kindly help me to sort out this issue.

推荐答案

// formatter for converting the dates:
SimpleDateFormat df = new SimpleDateFormat("dd/mm/yyyy", Locale.ENGLISH);
// the date required is a java.sql.Date
    // not a hava.util.Date so it must be converted:
ps.setDate(1, new Date(df.parse(todaysDate).getTime()));
ps.setInt(2, Integer.parseInt(cartID));
ps.setString(3, clientName);
ps.setString(4, teamSegment);
ps.setInt(5, Integer.parseInt(duration));
ps.setString(6, tapeID);
ps.setDate(7, new Date(df.parse(startDate).getTime()));
ps.setDate(8, new Date(df.parse(endDate).getTime()));

//System.out.println("After ps.setString()");
ps.executeUpdate();





就个人而言,我会直接将数据提取到正确的数据类型,因为可能存在解析错误的代码只会在一般例外情况下捕获。



Personally, I'd extract the data directly to the correct data types as there may be parsing errors which your code will only catch under general exceptions.


这篇关于在MSAccess 2000中插入值时获取运行时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 02:07