如何在多个for循环中执行SQL查询?
问题描述:
我正在开发一个涉及Access数据库集成的Java程序。该计划的目的是跟上码头的停车和服务。服务部分的产品监听器是一系列for循环,意在节省时间以免自己列出每一项服务。但是,需要这些结果来更新数据库,并且到目前为止,在使用程序的GUI时没有更改,并且Access数据库没有在我选择的任何指定表中更新。当GUI处于活动状态时,从复选框中选择一个选项,并在我执行时执行数据库。当我选择第二个复选框时,无论我选择什么,它只会导致异常错误。我需要修改程序和/或数据库,但我不知道如何处理这种情况。我的代码列在下面,任何帮助将不胜感激。如何在多个for循环中执行SQL查询?
package marinaProject;
/** Services, repairs, and provisions that are provided at a marina.
* @author Chris Cardenas
*/
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.Random;
import java.sql.*;
import javax.swing.border.*;
import java.sql.SQLException;
import java.sql.Statement;
public class Services extends JFrame {
static final String DATABASE_URL = "jdbc:ucanaccess://V:/Desktop/MarinaFinal.accdb";
Connection connection = null;
Statement statement = null;
ResultSet result = null;
JTabbedPane tabbedPane = new JTabbedPane();
private JPanel mainPanel = new JPanel(),
buttonPanel = new JPanel(),
servicesPanel = new JPanel(),
repairsPanel = new JPanel(),
provisionsPanel = new JPanel();
private final int NSERVICES = 6;
private final int NREPAIRS = 4;
private final int NPROVISIONS = 5;
private JCheckBox servicesPackages[] = new JCheckBox[NSERVICES];
private String servicePackageLabels[] = {"Fuel \n", "Water \n", "Waste \n", "Paint \n", "Winter \n", "Charging \n"};
private double servicePrice[] = {4, 3, 4, 7, 10, 0};
String serviceDbStatement;
private JCheckBox repairsPackages[] = new JCheckBox[NREPAIRS];
private String repairPackageLabels[] = {"Mechanical \n", "Electrical \n", "Communication \n", "Navigation \n"};
private double repairPrice[] = {100, 212, 100, 150};
String repairsDbStatement;
private JCheckBox provisionsPackages[] = new JCheckBox[NPROVISIONS];
private String provisionPackageLabels[] = {"Ice \n", "First Aid Kit \n", "Chips \n", "Popcorn \n", "Soda \n"};
private double provisionsPrice[] = {5, 10, 3, 2, 2};
String provisionsDbStatement;
private double totalBill;
private String servicesPrinted[] = new String[6];
private String repairsPrinted[] = new String[4];
private String provisionsPrinted[] = new String[5];
private double servicesPricesPrinted[] = new double[6];
private double repairsPricesPrinted[] = new double[4];
private double provisionsPricesPrinted[] = new double[5];
private int serviceNumber = 0;
private double boatID = 12345;
private JButton doneButton = new JButton("Done");
public Services() {
setTitle("Services");
mainPanel.setLayout(new GridLayout(2, 1, 1, 1));
doneButton.addActionListener(new ActionListen());
buttonPanel.add(doneButton);
servicesPanel.setLayout(new BoxLayout(servicesPanel, BoxLayout.Y_AXIS));
servicesPanel.setBorder(new TitledBorder("Services"));
for (int i = 0; i < servicesPackages.length; i++) {
servicesPackages[i] = new JCheckBox(servicePackageLabels[i]);
servicesPanel.add(servicesPackages[i]);
servicesPackages[i].addItemListener(new ItemListen());
}
tabbedPane.addTab("Services", null, servicesPanel, "Services Information");
repairsPanel.setLayout(new BoxLayout(repairsPanel, BoxLayout.Y_AXIS));
repairsPanel.setBorder(new TitledBorder("Repairs"));
for (int i = 0; i < repairsPackages.length; i++) {
repairsPackages[i] = new JCheckBox(repairPackageLabels[i]);
repairsPanel.add(repairsPackages[i]);
repairsPackages[i].addItemListener(new ItemListen());
}
tabbedPane.addTab("Repairs", null, repairsPanel, "Repairs Information");
provisionsPanel.setLayout(new BoxLayout(provisionsPanel, BoxLayout.Y_AXIS));
provisionsPanel.setBorder(new TitledBorder("Provisions"));
for (int i = 0; i < provisionsPackages.length; i++) {
provisionsPackages[i] = new JCheckBox(provisionPackageLabels[i]);
provisionsPanel.add(provisionsPackages[i]);
provisionsPackages[i].addItemListener(new ItemListen());
}
tabbedPane.addTab("Provisions", null, provisionsPanel, "Provisions Information");
tabbedPane.setTabPlacement(SwingConstants.LEFT);
mainPanel.add(tabbedPane);
mainPanel.add(buttonPanel);
add(mainPanel);
Random rand = new Random();
serviceNumber = rand.nextInt(5000) + 1;
}
private class ItemListen implements ItemListener {
public void itemStateChanged(ItemEvent event) {
totalBill = 0;
for (int i = 0; i < servicesPackages.length; i++) {
if (servicesPackages[i].isSelected()) {
totalBill += servicePrice[i];
servicesPrinted[i] = servicePackageLabels[i];
servicesPricesPrinted[i] = servicePrice[i];
ResultSet serviceDbResult = null;
String servicesQuery;
}
}
for (int i = 0; i < repairsPackages.length; i++) {
if (repairsPackages[i].isSelected()) {
totalBill += repairPrice[i];
repairsPrinted[i] = repairPackageLabels[i];
repairsPricesPrinted[i] = repairPrice[i];
ResultSet repairsDbResult = null;
String repairssQuery;
}
}
for (int i = 0; i < provisionsPackages.length; i++) {
if (provisionsPackages[i].isSelected()) {
totalBill += provisionsPrice[i];
provisionsPrinted[i] = provisionPackageLabels[i];
provisionsPricesPrinted[i] = provisionsPrice[i];
ResultSet provisionsDbResult = null;
String provisionsQuery;
String DATABASE_URL = "jdbc:ucanaccess://V:/Desktop/MarinaFinal.accdb";
}
}
String DATABASE_URL = "jdbc:ucanaccess://V:/Desktop/MarinaFinal.accdb";
try {
{
//establish connection to database
connection = DriverManager.getConnection(DATABASE_URL);
System.out.println("Made a connection");
//create Statement for querying database
statement = connection.createStatement();
System.out.println("Established statement");
//SQL Query
String sql = "INSERT INTO Services (serviceNumber, boatID, repairMech, repairElectro, serviceWater, serviceWaste, serviceWinter,"
+ "serviceCharging, servicePaint, serviceFuel, repairCommunication, repairNavigation, provisionIce"
+ "provisionFirstAid, provisionChips, provisionPopcorn, provisionSoda, slipNumber, billNumber)"
+ "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
// TEMPORARY VARIABLES
int slipNumTEMP = 4;
int billnumTEMP = 454;
PreparedStatement ps1 = connection.prepareStatement(sql);
ps1.setDouble(1, serviceNumber);
ps1.setDouble(2, boatID);
ps1.setDouble(3, servicePrice[0]);
ps1.setDouble(4, servicePrice[1]);
ps1.setDouble(5, servicePrice[2]);
ps1.setDouble(6, servicePrice[3]);
ps1.setDouble(7, servicePrice[4]);
ps1.setDouble(8, servicePrice[5]);
ps1.setDouble(9, repairPrice[0]);
ps1.setDouble(10, repairPrice[1]);
ps1.setDouble(11, repairPrice[2]);
ps1.setDouble(12, repairPrice[3]);
ps1.setDouble(13, provisionsPrice[0]);
ps1.setDouble(14, provisionsPrice[1]);
ps1.setDouble(15, provisionsPrice[2]);
ps1.setDouble(16, provisionsPrice[3]);
ps1.setDouble(17, provisionsPrice[4]);
ps1.setDouble(18, slipNumTEMP);
ps1.setDouble(19, billnumTEMP);
// Execute Query
int rs = ps1.executeUpdate();
if (rs == 1) {
System.out.println("Query executed");
} else {
System.out.println("Not found.");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private class ActionListen implements ActionListener {
public void actionPerformed(ActionEvent e) {
String label = doneButton.getText();
if (e.getSource() == doneButton) {
if (label.equals("Done")) {
setVisible(false);
billGUI nextScreen = new billGUI(totalBill, servicesPrinted, repairsPrinted,
provisionsPrinted, servicesPricesPrinted, repairsPricesPrinted,
provisionsPricesPrinted);
nextScreen.setSize(400, 650);
nextScreen.setVisible(true);
}
}
}
}
}
编辑:下面是服务表和数据库中其他表的关系的图像。
答
再现你的问题,因为屏幕截图显示的是GUI形式,修复来到了你的SQL其上仔细读保持语法错误与缺少逗号。
只是provisionIce
后你没有一个逗号,因此该查询试图插入一个不存在的列,由于线路中断provisionIceprovisionFirstAid
其可输出完整性约束违规这MSACCESS.EXE会输出以下其中一个:
查询值和目标字段的数量不相同。
INSERT INTO语句包含未知字段名'provisionIceprovisionFirstAid'。
所以只需添加必要的逗号:
"repairCommunication, repairNavigation, provisionIce,"
----------------------------------------------------^
+ "provisionFirstAid, provisionChips, provisionPopcorn, provisionSoda,
其他注意事项:
- 截至目前,每一个复选框点击附加在所有列完全相同的数据。可能您仍处于沙箱开发模式。
- 考虑在启动GUI程序期间使用其他调用方法连接到Access数据库,然后在每个复选框上单击附加连接对象。现在,连接是在Listener事件上进行的。
- 考虑添加Window Listener或Event(在搜索或Google/Bing中可以找到的常规SO问题),以便在用户关闭窗口时正确结束程序。
什么是例外?你的数据库模式是什么? – Ivan
“完整性约束违规”是我所得到的例外情况。 – CJC
正如@Ivan所问,请张贴表格模式。是否有任何字段自动编号(boatid?),必需(非空约束),没有零长度,没有重复索引,不是双字段(因为所有参数都被转换为)等等。您可以使用MSAccess.exe在设计视图中检查这些属性。 – Parfait