Search code examples
javasqlms-accessnetbeansucanaccess

"data exception: division by zero" when inserting row


I am trying to insert data entered from a JFrame and series of textfields into my database table. I have been able to do so with smaller amounts of data, but Im having a lot of trouble with this.

<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>

When I insert my values I get an error message saying:

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


</pre></code>

Im not too sure why it would say an error is being caused "division by zero", but I do not have any fields in the database that are of numerical fields.

EDIT: http://www53.zippyshare.com/v/DMLjdpDw/file.html Link to Access database


Solution

  • Your [AccountTbl] table contains three additional fields:

    [TotalRating] - Long Integer, Default 0
    [noRating] - Long Integer, Default 0
    [overallRating] - Calculated: [TotalRating]/[noRating]

    Since you are not supplying a value for [noRating] it is defaulting to zero, and therefore the [overallRating] calculation is trying to divide by zero.

    You should change the default value of the [noRating] column to Null, so the [overallRating] calculation will return Null if no [noRating] is entered.