Java 添加包含列的行,这些列对主键和外键JDBC进行了排序
我的程序有一个添加项和完成事务选项。Java 添加包含列的行,这些列对主键和外键JDBC进行了排序,java,jdbc,prepared-statement,oracle-sqldeveloper,Java,Jdbc,Prepared Statement,Oracle Sqldeveloper,我的程序有一个添加项和完成事务选项。FinishTransaction类要求用户输入客户信息、付款方式和付款方式 付款总额显示在窗口中。当用户单击结帐按钮时,数据应从客户表(插入客户信息)、订单表(插入购买的商品信息)和交易表(插入交易信息)传输 事务表有一列TRANS_CUSTNUM,该列是引用CUSTOMER表中CUST_NUM的外键 我的程序工作正常,除了在事务表中。它不会在我的SQL developer中添加行。你认为我的代码有什么错误 import javax.swing.*; imp
FinishTransaction
类要求用户输入客户信息、付款方式和付款方式
付款总额显示在窗口中。当用户单击结帐按钮时,数据应从客户
表(插入客户信息)、订单
表(插入购买的商品信息)和交易
表(插入交易信息)传输
事务表有一列TRANS_CUSTNUM
,该列是引用CUSTOMER
表中CUST_NUM
的外键
我的程序工作正常,除了在事务
表中。它不会在我的SQL developer中添加行。你认为我的代码有什么错误
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.logging.*;
public class FinishTransaction extends JFrame implements ActionListener{
JLabel totalLabel;
JLabel nameLabel;
JLabel addressLabel;
JLabel contactLabel;
JLabel custPaymentLabel;
JLabel methodLabel;
JLabel creditCardLabel;
JTextField totalTextField;
JTextField nameTextField;
JTextField addressTextField;
JTextField contactTextField;
JTextField custPaymentTextField;
JTextField creditCardTextField;
final JButton mainMenuButton = new JButton("Main Menu");
final ButtonGroup bGroup = new ButtonGroup();
final JRadioButton cashRadioButton = new JRadioButton("Cash");
final JRadioButton creditRadioButton = new JRadioButton("Credit Card");
final JButton checkoutButton = new JButton("Checkout");
static FinishTransaction fin = new FinishTransaction();
static AddItem add = new AddItem();
static int total = 0;
static int payment = 0;
static int change = 0;
static String payment_desc;
static int creditCard;
public FinishTransaction(){
//ui
}
public void actionPerformed(ActionEvent e){
if(checkoutButton.getName().equals(((Component)e.getSource()).getName())){
try{
payment = Integer.parseInt(custPaymentTextField.getText());
if(payment>=total){
change = payment - total;
JOptionPane.showMessageDialog(this, "Thank you for shopping! Your change is "+change, "Exiting", JOptionPane.INFORMATION_MESSAGE);
}
else
JOptionPane.showMessageDialog(this, "Your payment is not enough. Please try again!", "Error!", JOptionPane.ERROR_MESSAGE);
}
catch(NumberFormatException a){
JOptionPane.showMessageDialog(this, "Invalid input", "Error!", JOptionPane.ERROR_MESSAGE);
}
Connection conn = null;
PreparedStatement pstmt = null;
PreparedStatement pstmt2 = null;
PreparedStatement pstmt3 = null;
String URL = "jdbc:oracle:thin:@VAIO:49160:xe";
String USER = "mariel";
String PASS = "1234";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
try {
String name = nameTextField.getText();
String address = addressTextField.getText();
int contact = Integer.parseInt(contactTextField.getText());
conn = DriverManager.getConnection(URL, USER, PASS);
String sql = "INSERT INTO CUSTOMER " +
"VALUES(CustNumSeq.NEXTVAL, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, address);
pstmt.setInt(3, contact);
pstmt.executeUpdate();
for(int index=0;index<add.itemNum.length;index++){
String sql2 = "INSERT INTO ORDERS "+
"VALUES(OrderNumSeq.NEXTVAL, ?, ?)";
pstmt2 = conn.prepareStatement(sql2);
pstmt2.setInt(1,add.itemNum[index]);
pstmt2.setInt(2, add.quantity[index]);
pstmt2.executeUpdate();
}
creditCard = Integer.parseInt(creditCardTextField.getText());
String sql3 = "INSERT INTO TRANSACTION " +
"VALUES(TransNumSeq.NEXTVAL, CustNumSeq.NEXTVAL, ?, ?, ?, ?)";
pstmt3 = conn.prepareStatement(sql3);
pstmt3.setInt(1, payment);
pstmt3.setString(2, payment_desc);
pstmt3.setInt(3, creditCard);
pstmt3.setInt(4, change);
pstmt.executeUpdate();
}
catch (SQLException ex) {
}
catch(NumberFormatException a){
JOptionPane.showMessageDialog(this, "Invalid input", "Error!", JOptionPane.ERROR_MESSAGE);
}
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
catch(NumberFormatException a){
JOptionPane.showMessageDialog(this, "Invalid input", "Error!", JOptionPane.ERROR_MESSAGE);
}
finally{
try{
if(pstmt!=null)
pstmt.close();
}
catch(SQLException se2){
}
try{
if(pstmt2!=null)
pstmt2.close();
}
catch(SQLException se2){
}
try{
if(pstmt3!=null)
pstmt3.close();
}
catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}
catch(SQLException se){
}
}
}
else if(mainMenuButton.getName().equals(((Component)e.getSource()).getName())){
EmployeeMode emp = new EmployeeMode();
emp.setVisible(true);
emp.setResizable(false);
emp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
emp.setSize(400,300);
this.setVisible(false);
}
if(creditRadioButton.isSelected()){
creditCardLabel.setVisible(true);
creditCardTextField.setVisible(true);
payment_desc = "Credit Card";
}
else if(cashRadioButton.isSelected()){
creditCardLabel.setVisible(false);
creditCardTextField.setVisible(false);
payment_desc = "Cash";
}
}
public static void main(String args[]){
fin.setVisible(true);
fin.setResizable(false);
fin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fin.setSize(500,400);
}
}
import javax.swing.*;
导入java.awt.event.*;
导入java.awt.*;
导入java.awt.event.ActionListener;
导入java.sql.*;
导入java.util.logging.*;
公共类FinishTransaction扩展JFrame实现ActionListener{
JLabel;abel;
JLabel名称标签;
JLabel地址标签;
JLabel-contactLabel;
JLabel custPaymentLabel;
JLabel方法标签;
JLabel信用卡标签;
JTextField totalTextField;
JTextField name textfield;
JTextField地址textfield;
JTextField contactTextField;
JTextField custPaymentTextField;
JTextField creditCardTextField;
最终JButton mainMenuButton=新JButton(“主菜单”);
final ButtonGroup bGroup=新建ButtonGroup();
最终JRadioButton cashRadioButton=新JRadioButton(“现金”);
最终JRadioButton creditRadioButton=新的JRadioButton(“信用卡”);
最终JButton checkburatton=新JButton(“签出”);
静态FinishTransaction fin=新FinishTransaction();
静态AddItem add=新的AddItem();
静态整数总计=0;
静态整数支付=0;
静态整数变化=0;
静态字符串支付描述;
静态int信用卡;
公共FinishTransaction(){
//用户界面
}
已执行的公共无效操作(操作事件e){
if(checkOutbatton.getName().equals(((组件)e.getSource()).getName()){
试一试{
payment=Integer.parseInt(custPaymentTextField.getText());
如果(付款>=总额){
变更=付款-总额;
showMessageDialog(这是“感谢您的购物!您的更改是”+更改,“正在退出”,JOptionPane.INFORMATION\u消息);
}
其他的
showMessageDialog(这是“您的付款不足,请重试!”、“错误!”、JOptionPane.Error\u消息);
}
捕获(数字格式异常a){
showMessageDialog(这是“无效输入”,“错误!”,JOptionPane.Error\u消息);
}
连接conn=null;
PreparedStatement pstmt=null;
PreparedStatement pstmt2=null;
PreparedStatement pstmt3=空;
String URL=“jdbc:oracle:thin:@VAIO:49160:xe”;
字符串USER=“mariel”;
字符串PASS=“1234”;
试一试{
类forName(“oracle.jdbc.driver.OracleDriver”);
试一试{
String name=nameTextField.getText();
字符串地址=addressTextField.getText();
int contact=Integer.parseInt(contactTextField.getText());
conn=DriverManager.getConnection(URL、用户、通行证);
字符串sql=“插入客户”+
“值(CustNumSeq.NEXTVAL、、、?)”;
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,名称);
pstmt.setString(2,地址);
pstmt.setInt(3,触点);
pstmt.executeUpdate();
对于(int index=0;index1),我看不出您在代码中维护任何事务。
2) 在这种情况下,如果上一次插入有任何错误,您将无法看到第三个表中的数据。在插入第三个表时,请检查您的日志是否存在任何异常。
3) 我觉得很明显的问题是:
"INSERT INTO TRANSACTION " +
"VALUES(TransNumSeq.NEXTVAL, **CustNumSeq.NEXTVAL**, ?, ?, ?, ?)"
…如果您第二次调用该序列,它的客户号将与您在第一条语句中插入的客户号不同。
4) 如果您在事务表中将cust_num定义为外键,则代码应该会中断。因为cust_num在customer表中还不存在。我很确定此sql insert语句不正确
String sql3 = "INSERT INTO TRANSACTION " +
"VALUES(TransNumSeq.NEXTVAL, CustNumSeq.NEXTVAL, ?, ?)";
您正在向事务中插入一条记录,并尝试在CUSTOMER(CustNumSeq.NEXTVAL)上推进序列。您应该尝试使用
//when inserting into CUSTOMER
pstmt=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
pstmt.execute();//make the insert
//and then simply grab the key
ResultSet rs = stmt.getGeneratedKeys();
if (rs.next())
id = rs.getObject(1);
请注意,并非每个JDBC驱动程序都支持返回生成的密钥,因此您可能需要使用一个额外的select来获取最后生成的密钥
最后,您必须更改此部分以反映上面所做的更改
String sql3 = "INSERT INTO TRANSACTION " +
"VALUES(TransNumSeq.NEXTVAL, ?, ?, ?, ?, ?)";
pstmt3 = conn.prepareStatement(sql3);
pstmt3.setInt(1, id);
pstmt3.setInt(2, payment);
pstmt3.setString(3, payment_desc);
pstmt3.setInt(4, creditCard);
pstmt3.setInt(5, change);
您是否提交了连接..在完成事务以持久化数据“conn.commit();”之后,我在代码段中没有看到如何使其具有相等数量的客户号?按顺序进行选择,然后将数字存储在变量中。因此使用该数字。或者使用JDBCgetGeneratedKeys
facility。id部分是否应为static int id;
或FinishTransaction id=new FinishTransaction();
?如果您的序列是整数,那么只需使用int id=rs.getInt(1)我是否必须制作另一个stmt.executeUpdate
,将生成的键移动到变量中才能工作?~我明天将尝试编写此代码。~否。该键在ResultSet rs=PreparedStatement.getGeneratedKeys()中返回请注意,您可以通过调用java.sql.DataBaseMetadata.supportsGetGeneratedKeys()meth来检查此功能是否可用
String sql3 = "INSERT INTO TRANSACTION " +
"VALUES(TransNumSeq.NEXTVAL, ?, ?, ?, ?, ?)";
pstmt3 = conn.prepareStatement(sql3);
pstmt3.setInt(1, id);
pstmt3.setInt(2, payment);
pstmt3.setString(3, payment_desc);
pstmt3.setInt(4, creditCard);
pstmt3.setInt(5, change);