【问题标题】:"data exception: division by zero" when inserting row插入行时出现“数据异常:除以零”
【发布时间】:2017-01-17 00:00:01
【问题描述】:

我正在尝试将从 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>

我不太清楚为什么它会说“被零除”导致错误,但我在数据库中没有任何数字字段的字段。

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

【问题讨论】:

  • 我无法重现您的问题。请尝试在 Access 中打开数据库并对其执行压缩和修复数据库操作。如果问题仍然存在,请考虑发布一个指向您的数据库文件副本的链接,以便可靠地重现错误。
  • 添加了链接,谢谢。

标签: java sql ms-access netbeans ucanaccess


【解决方案1】:

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

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

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

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

【讨论】:

  • 完美运行.. 再次感谢汤普森勋爵!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多