问题描述
好的,我正在使用 ms access 和 Java Netbeans 为结帐寄存器创建代码。该程序应该将 ProductID、Price、quantity、total 和 lastid 插入到表 AdminSales 中,但是每当我运行该程序时,它都会说只读取了表数据。请任何人都可以帮助我吗?插入第一个数据库有效,但我无法将其插入到 AdminSales 表中,请这是给我的 IA 的,所以请提供任何帮助,我们将不胜感激。
private void sales()
{
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDateTime Now = LocalDateTime.Now();
String date = dtf.format(Now);
String subtot = txtsub.getText();
String pay = txtpay.getText();
String bal = txtbal.getText();
int lastinsertid = 0;
try {
Class.forName("net.ucanaccess.jdbc.ucanaccessDriver");
conn=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\cnoye\\Documents//mydb.accdb"); //connect to database
String query = "insert into Sales(Date,Subtotal,Pay,Balance)values(?,?,?)";
insert = conn.prepareStatement(query,Statement.RETURN_GENERATED_KEYS);
insert.setString(1,date);
insert.setString(2,subtot);
insert.setString(3,pay);
insert.setString(4,bal);
insert.executeUpdate();
ResultSet genteratedKeyResult = insert.getGeneratedKeys();
if(genteratedKeyResult.next())
{
lastinsertid = genteratedKeyResult.getInt(1);
}
int row = jTable1.getRowCount();
String query1 = "insert into AdminSales(SalesID,ProductID,Price,Quantity,Total)values(?,?)";
insert = conn.prepareStatement(query1);
String ProductID="";
String price="";
String qty="";
double total=0;
for(int i = 0; i<jTable1.getRowCount(); i++)
{
ProductID = (String)jTable1.getValueAt(i,0);
price = (String)jTable1.getValueAt(i,3);
qty = (String)jTable1.getValueAt(i,2);
total= (double)jTable1.getValueAt(i,4);
insert.setInt(1,lastinsertid);
insert.setString(2,ProductID);
insert.setString(3,price);
insert.setString(4,qty);
insert.setDouble(5,total);
insert.executeUpdate();
}
insert.addBatch();
JOptionPane.showMessageDialog(this,"Record Saved");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Checkouttest.class.getName()).log(Level.SEVERE,null,ex);
JOptionPane.showMessageDialog(this,ex);
} catch (sqlException ex) {
Logger.getLogger(Checkouttest.class.getName()).log(Level.SEVERE,ex);
JOptionPane.showMessageDialog(this,ex);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)