?? foodconfiguration.java
字號:
modify.addActionListener(new FoodConfiguration_modify_actionAdapter(this));
jPanel5.add(modify, null);
getContentPane().add(panel1, BorderLayout.CENTER);
panel1.add(tableScrollPane1, BorderLayout.CENTER);
panel1.add(DBNavToolBar, BorderLayout.SOUTH);
tableScrollPane1.getViewport().add(jdbTable1, null);
this.getContentPane().add(jPanel2, BorderLayout.NORTH);
jPanel2.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jLabel8, null);
jPanel1.add(foodName, null);
jPanel1.add(jLabel7, null);
jPanel1.add(producePlace, null);
jPanel1.add(jLabel6, null);
jPanel1.add(foodType, null);
jPanel1.add(jLabel5, null);
jPanel1.add(foodUnit, null);
jPanel1.add(jLabel4, null);
jPanel1.add(price, null);
jPanel1.add(jLabel3, null);
jPanel1.add(retailPrice, null);
jPanel1.add(jLabel2, null);
jPanel1.add(producer, null);
jPanel1.add(jLabel9, null);
jPanel1.add(note, null);
jPanel2.add(jPanel3, BorderLayout.NORTH);
jPanel3.add(jLabel10, null);
jPanel2.add(jPanel4, BorderLayout.SOUTH);
jPanel4.add(addFood, null);
jPanel4.add(deleteFood, null);
this.getContentPane().add(jPanel5, BorderLayout.SOUTH);
jPanel5.add(close, null);
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
this.prepareShow();
}
private void prepareShow(){
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Restaurant;user=user;password=user");
ps=conn.prepareStatement("select name from supplier");
rs=ps.executeQuery();
while(rs.next())
producer.addItem(rs.getString("name"));
ps=conn.prepareStatement("select name from foodType");
rs=ps.executeQuery();
while(rs.next())
foodType.addItem(rs.getString("name"));
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if(rs!=null)try{rs.close();}catch(SQLException ignore){}
if(ps!=null)try{ps.close();}catch(SQLException ignore){}
if(conn!=null)try{conn.close();}catch(SQLException ignore){}
}
}
void addFood_actionPerformed(ActionEvent e) {
//驗證輸入數據的合法性
if(foodName.getText().trim().equals("")||price.getText().trim().equals("")||retailPrice.getText().trim().equals("")||foodUnit.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(this, "食品名稱,單價和單位不能為空", "錯誤",JOptionPane.ERROR_MESSAGE);
return;
}
try{
Float.parseFloat(price.getText().trim());
Float.parseFloat(retailPrice.getText().trim());
}
catch(NumberFormatException ex)
{
JOptionPane.showMessageDialog(this, "輸入的數據不合法", "錯誤",JOptionPane.ERROR_MESSAGE);
return;
}
//檢查是否已存在該名稱的酒菜
if(this.foodExist(foodName.getText()))
{
JOptionPane.showMessageDialog(this, "已存在該名稱的酒菜", "錯誤",JOptionPane.ERROR_MESSAGE);
return;
}
//添加酒菜信息
Connection conn=null;
PreparedStatement ps=null;
try{
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Restaurant;user=user;password=user");
ps=conn.prepareStatement("insert into food(name,proPlace,unit,foodType,price,retailPrice,producer,remark) values(?,?,?,?,?,?,?,?)");
ps.setString(1,foodName.getText());
ps.setString(2,producePlace.getText());
ps.setString(3,foodUnit.getText());
ps.setString(4,(foodType.getSelectedItem()).toString());
ps.setFloat(5,Float.parseFloat(price.getText()));
ps.setFloat(6,Float.parseFloat(retailPrice.getText()));
ps.setString(7,producer.getSelectedItem().toString());
ps.setString(8,note.getText());
ps.executeUpdate();
//刷新列表顯示
JButton refreshButton=DBNavToolBar.getRefreshButton();
refreshButton.doClick();
JOptionPane.showMessageDialog(this, "已成功增加該食品", "操作",JOptionPane.PLAIN_MESSAGE);
}
catch(SQLException ex){
ex.printStackTrace();
}
finally{
if(ps!=null)try{ps.close();}catch(SQLException ignore){}
if(conn!=null)try{conn.close();}catch(SQLException ignore){}
}
}
private boolean foodExist(String food)
{
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
boolean exist=false;
try{
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Restaurant;user=user;password=user");
ps=conn.prepareStatement("select name from food");
rs=ps.executeQuery();
if(rs.next())
exist=true;
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if(rs!=null)try{rs.close();}catch(SQLException ignore){}
if(ps!=null)try{ps.close();}catch(SQLException ignore){}
if(conn!=null)try{conn.close();}catch(SQLException ignore){}
}
return exist;
}
void deleteFood_actionPerformed(ActionEvent e) {
Connection conn=null;
PreparedStatement ps=null;
try{
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Restaurant;user=user;password=user");
ps = conn.prepareStatement("delete from food where name=?");
ps.setString(1,foodName.getText());
if(ps.executeUpdate()==1)
{
//刷新列表顯示
JButton refreshButton = DBNavToolBar.getRefreshButton();
refreshButton.doClick();
JOptionPane.showMessageDialog(this, "已成功刪除該食品", "操作",
JOptionPane.PLAIN_MESSAGE);
}
}
catch(SQLException ex){
JOptionPane.showMessageDialog(this, "系統中有與該酒菜相關的數據,不能刪除", "錯誤",
JOptionPane.WARNING_MESSAGE);
}
finally{
if(ps!=null)try{ps.close();}catch(SQLException ignore){}
if(conn!=null)try{conn.close();}catch(SQLException ignore){}
}
}
void close_actionPerformed(ActionEvent e) {
this.dispose();
}
void modify_actionPerformed(ActionEvent e) {
JButton postButton=DBNavToolBar.getPostButton();
postButton.doClick();
JButton saveButton=DBNavToolBar.getSaveButton();
saveButton.doClick();
JOptionPane.showMessageDialog(this, "已成功提交修改信息!", "提示",
JOptionPane.PLAIN_MESSAGE);
}
}
class FoodConfiguration_addFood_actionAdapter implements java.awt.event.ActionListener {
FoodConfiguration adaptee;
FoodConfiguration_addFood_actionAdapter(FoodConfiguration adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.addFood_actionPerformed(e);
}
}
class FoodConfiguration_deleteFood_actionAdapter implements java.awt.event.ActionListener {
FoodConfiguration adaptee;
FoodConfiguration_deleteFood_actionAdapter(FoodConfiguration adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.deleteFood_actionPerformed(e);
}
}
class FoodConfiguration_close_actionAdapter implements java.awt.event.ActionListener {
FoodConfiguration adaptee;
FoodConfiguration_close_actionAdapter(FoodConfiguration adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.close_actionPerformed(e);
}
}
class FoodConfiguration_modify_actionAdapter implements java.awt.event.ActionListener {
FoodConfiguration adaptee;
FoodConfiguration_modify_actionAdapter(FoodConfiguration adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.modify_actionPerformed(e);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -