📄 insertdb.java
字号:
package Dbprocess;
import java.sql.*;
import PizzaProduct.*;
import OrderInf.*;
public class InsertDB {
Connection con;
public InsertDB(PizzaOrders po,Connection con){
this.con=con;
Pizza[] pizzas = po.getPizzas();
try{
con.setAutoCommit(false);// 更改JDBC事务的默认提交方式
String sql = "INSERT INTO pizzasaled(pizzaid,name,type,size,topping,saleid,routines,instore) VALUES (?,?,?,?,?,?,?,?)";
int i;
int count = getCount("pizzasaled");
count++;
int ordercount = getCount("salesorder")+1;
String sql2 = "INSERT INTO salesorder(saleid,totalprice,instore) VALUES(?,?,?)";
PreparedStatement pstate2 = con.prepareStatement(sql2);
pstate2.setString(1,"ORD"+Integer.toString(ordercount));
pstate2.setInt(2,po.getTotalprice());
pstate2.setString(3,po.getInstore());
pstate2.executeUpdate();
for(i=0;i<pizzas.length;i++){
PreparedStatement pstate = con.prepareStatement(sql);
pstate.setString(1,"P"+Integer.toString(count));
pstate.setString(2,pizzas[i].getName());
pstate.setString(3,pizzas[i].getType());
pstate.setString(4,pizzas[i].getSize());
pstate.setString(5,pizzas[i].getTopping());
pstate.setString(6,"ORD"+Integer.toString(ordercount));
pstate.setInt(7,pizzas[i].getRoutines());
pstate.setString(8,pizzas[i].getInstore());
pstate.executeUpdate();
count++;
}
String sql3 = "INSERT INTO revenue(saleid,revenue) VALUES(?,?)";
PreparedStatement pstate3 = con.prepareStatement(sql3);
pstate3.setString(1,"ORD"+Integer.toString(ordercount));
pstate3.setDouble(2,(po.getTotalprice()*0.1));
pstate3.executeUpdate();
con.commit();//提交JDBC事务
con.setAutoCommit(true);// 恢复JDBC事务的默认提交方式
}catch (Exception e) {
try{
con.rollback();
}catch (Exception e2){
e2.printStackTrace();
}
e.printStackTrace();
}
}
public InsertDB(String saleid,String staffid,Connection con){
this.con=con;
try{
String sql = "INSERT INTO staffsent(saleid,staffid) VALUES(?,?)";
PreparedStatement pstate = con.prepareStatement(sql);
pstate.setString(1,saleid);
pstate.setString(2,staffid);
pstate.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}
}
public int getCount(String atable){
try{
String sql = "select count(*) as count from " + atable;
//PreparedStatement pstate = con.prepareStatement(sql);
//pstate.setString(1,atable);
Statement state = con.createStatement();
ResultSet resultSet = state.executeQuery(sql);
resultSet.next();
int i=resultSet.getInt("count");
return i;
}
catch (Exception e) {
e.printStackTrace();
return 0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -