"数据异常:除以零"插入行时

时间:2016-09-08 22:54:19

标签: java sql ms-access netbeans ucanaccess

我正在尝试将从JFrame和一系列文本字段输入的数据插入到我的数据库表中。我已经能够用较少的数据来做这件事,但我在这方面遇到了很多麻烦。

<pre><code>
package Vegan;

import java.sql.Connection;
import java.sql.DriverManager;


public class connectionString {

static Connection connection = null;

public static Connection getConnection()
{
    try
    {
        connection = DriverManager.getConnection("jdbc:ucanaccess://C:/Mo//MyDatabase1.accdb");
        System.out.println("---connection succesful---");
    }

    catch (Exception ex)
    {
        System.out.println("Connection Unsuccesful");
    }

    return connection;
}


</pre></code>
<pre><code>
package Vegan;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


public class DB {


private static ResultSet rs = null;
private static PreparedStatement ps = null;
private static Connection connection = null;

public DB() {
    connection = connectionString.getConnection();
}

public void AddSupplier(String pw, String un, String sn, String cn, String ws, String pa, String city, String pv, String pc) throws SQLException
{
    ps = connection.prepareStatement("INSERT INTO AccountTbl(StorePassword, Username, StoreName, ContactNo, Website, PhysAddress, City, Province, PostalCode) VALUES (?,?,?,?,?,?,?,?,?)");


    ps.setString(1, pw);
    ps.setString(2, un);
    ps.setString(3, sn);
    ps.setString(4, cn);
    ps.setString(5, ws);
    ps.setString(6, pa);
    ps.setString(7, city);
    ps.setString(8, pv);
    ps.setString(9, pc);
    ps.executeUpdate();
}

}     

<pre><code>
package Vegan;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


public class SupplierReg extends javax.swing.JFrame {
private Object JOptionpane;


public SupplierReg() {
    initComponents();
}

private void btnEnterActionPerformed(java.awt.event.ActionEvent evt) {                                         

    if ((txtAreaCode.getText().equals("")) || (txtCity.getText().equals("")) || txtContactNo.getText().equals("")
            || txtPassword.getText().equals("") || txtProvince.getText().equals("") || txtStoreName.getText().equals("") || txtStreetAddress.getText().equals("") || txtUsername.getText().equals("") || txtWebsite.getText().equals("")) {

        JOptionPane.showMessageDialog(rootPane, "Please fill in all the information boxes");

    } else {
        try {
            DB regDB = new DB();

            regDB.AddSupplier(txtUsername.getText(), txtPassword.getText(), txtStreetAddress.getText(), txtStoreName.getText(), txtCity.getText(), txtContactNo.getText(), txtProvince.getText(), txtWebsite.getText(), txtAreaCode.getText());
        } catch (SQLException ex) {

            System.out.println(ex.getLocalizedMessage().toString());
        }
            setVisible(false);
            new SupplierAbilityMenu().setVisible(true);
    }


}

</pre></code>

当我插入我的值时,我收到一条错误消息:

<pre><code>
run:
---connection succesful---
UCAExc:::3.0.6 data exception: division by zero
BUILD SUCCESSFUL (total time: 1 second)


</pre></code>

我不太清楚为什么它会说错误是由#s除零&#34;但我在数据库中没有任何数字字段的字段。

编辑:http://www53.zippyshare.com/v/DMLjdpDw/file.html链接到Access数据库

1 个答案:

答案 0 :(得分:2)

您的[AccountTbl]表包含三个附加字段:

[TotalRating] - 长整数,默认0
[noRating] - 长整数,默认0
[overallRating] - 计算:[TotalRating] / [noRating]

由于您没有为[noRating]提供值,因此它默认为零,因此[overallRating]计算试图除以零。

您应该将[noRating]列的默认值更改为Null,因此如果未输入[noRating],[overallRating]计算将返回Null。