我正试图将从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数据库
您的[AccountTbl]表包含三个附加字段:
[TotalRating] -长整数,默认为0
[noRating] -长整数,默认为0
[overallRating] -计算:[TotalRating]/[noRating]
由于您没有为[noRating]提供一个值,它默认为零,因此[overallRating]的计算试图除以零。
您应该将[noRating]列的默认值更改为Null,因此如果没有输入[noRating],则[overallRating]计算将返回Null。