?? commoditymanage.java
字號:
import java.io.*;
import java.sql.*;
//---------------------------------------------Here's Commodity Detail---------------------------------------------//
class Commodity
{
static int n;
static String ID;
public void CommodityMenu()throws IOException
{
System.out.println(" |--------Commodity Menu--------|");
System.out.println(" 1.Display Commodity Detail");
System.out.println(" 2.Insert Commodity Detail");
System.out.println(" 3.Update Commodity Detail");
System.out.println(" 4.Delete Commodity Detail");
System.out.println(" 0.Exit");
System.out.println(" |------------------------------|");
System.out.print(" Please choose a number(0~4):");
Commodity c=new Commodity();
c.choose();
}
public void again()throws IOException
{
System.out.println("Return to Main Menu ?");
System.out.print("Press '10' for Yes,Press '20' for No! ");
BufferedReader ar=new BufferedReader(new InputStreamReader(System.in));
String d=ar.readLine();
int m=Integer.parseInt(d);
Commodity c=new Commodity();
CommodityManage CM=new CommodityManage();
if(m==10)
{
c.CommodityMenu();
c.choose();
}
else if(m==20)
{
System.out.println("You have exited the Main Menu!");
System.exit(0);
}
else
{
System.out.println("Please Enter again!");
c.again();
}
}
public void choose()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String w=br.readLine();
n=Integer.parseInt(w);
Commodity c=new Commodity();
CommodityManage CM=new CommodityManage();
{
switch(n)
{
case 1: c.DisplayCommodity(); c.again(); break;
case 2: c.InsertCommodity() ; c.again(); break;
case 3: c.UpdateCommodity() ; c.again(); break;
case 4: c.DeleteCommodity() ; c.again(); break;
case 0: System.out.println("-------------You have returned to the Main Menu!-------------");
CM.DisplayMenu();
CM.chooseMenu();
break;
default:
System.out.println("Please Enter again:"); c.choose(); break;
}
}
}
public void DisplayCommodity()
{
System.out.println("-----------------1.DisplayCommodity--------------------");
Connection con=null;
Statement stat;
ResultSet res;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:Commodity");
stat=con.createStatement();
res=stat.executeQuery("select * from CommodityDetail");
while(res.next())
{
System.out.println(" Commodity ID : "+res.getString(1));
System.out.println(" Supplier ID : "+res.getString(2));
System.out.println("Commodity Name : "+res.getString(3));
System.out.println("Commodity Type : "+res.getString(4));
System.out.println(" Quantity : "+res.getString(5));
System.out.println("Thresold Value : "+res.getString(6));
System.out.println(" Store Date : "+res.getString(7));
System.out.println(" Need Flag : "+res.getString(8));
System.out.println(" Order Flag : "+res.getString(9));
System.out.println("\n-------------------Commodity Detail------------------\n");
}
}
catch(Exception e)
{
System.out.println("Could not query: "+e);
}
if (con!=null) con=null;
}
public void Display()
{
Connection con=null;
PreparedStatement stat;
ResultSet res=null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:Commodity");
stat=con.prepareStatement("select * from CommodityDetail where cCommodityID=?");
stat.setString(1,(String)ID);
res=stat.executeQuery();
while(res.next())
{
System.out.println(" Commodity ID : "+res.getString(1));
System.out.println(" Supplier ID : "+res.getString(2));
System.out.println("Commodity Name : "+res.getString(3));
System.out.println("Commodity Type : "+res.getString(4));
System.out.println(" Quantity : "+res.getString(5));
System.out.println("Thresold Value : "+res.getString(6));
System.out.println(" Store Date : "+res.getString(7));
System.out.println(" Need Flag : "+res.getString(8));
System.out.println(" Order Flag : "+res.getString(9));
System.out.println("\n-------------------Commodity Detail------------------\n");
}
}
catch(Exception e)
{
System.out.println("Could not display: "+e);
}
if (con!=null) con=null;
}
public void InsertCommodity()throws IOException
{
System.out.println("-----------------2.InsertCommodity--------------------");
Connection con=null;
PreparedStatement stat=null;
int res=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:Commodity");
stat=con.prepareStatement("insert into CommodityDetail(cCommodityID,cSupplierID,vCommodityName,vCommodityType,iCommodityQty,iCommodityThreshold) values(?,?,?,?,?,?)");
System.out.print("cCommodityID(6) = ");
String cCommodityID=br.readLine(); //從鍵盤讀入值存入字串變量中
ID=cCommodityID;
System.out.print("cSupplierID(6) = ");
String cSupplierID=br.readLine();
System.out.print("vCommodityName(50) = ");
String vCommodityName=br.readLine();
System.out.print("vCommodityType(20) = ");
String vCommodityType=br.readLine();
System.out.print("iCommodityQty(int) = ");
String iCommodityQty=br.readLine();
System.out.print("iCommodityThreshold(int) = ");
String iCommodityThreshold=br.readLine();
System.out.println(" ");
stat.setString(1,cCommodityID); //用值替換SQL語句中的問號參數
stat.setString(2,cSupplierID);
stat.setString(3,vCommodityName);
stat.setString(4,vCommodityType);
stat.setInt(5,Integer.parseInt(iCommodityQty));
stat.setInt(6,Integer.parseInt(iCommodityThreshold));
res=stat.executeUpdate();
if (res!=0) System.out.println("-------Your details have been registered-------");
}
catch(Exception ex)
{
System.out.println("Error registe: "+ex);
}
if (con!=null) con=null;
Log l=new Log();
l.GetDate();
FileWriter outF = new FileWriter("log.txt",true);
outF.write(" Inserted a commodity \r\n");
outF.write("-----------------------------------------------------\r\n");
outF.close();
System.out.println(" ");
System.out.println("-------------Insert Detail-------------------");
Commodity c=new Commodity();
c.Display();
}
public void UpdateCommodity()throws IOException
{
System.out.println("-----------------3.UpdateCommodity--------------------");
Connection con=null;
PreparedStatement stat=null;
int res=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:Commodity");
System.out.print("Please enter the cCommodityID(6):");
String cCommodityID=br.readLine();
ID=cCommodityID;
System.out.println();
System.out.println("Update what?");
System.out.println("1.Commodity ID");
System.out.println("2.Supplier ID");
System.out.println("3.Commodity Name");
System.out.println("4.Commodity Type");
System.out.println("5.Quantity ");
System.out.println("6.Thresold Value");
System.out.print("-------Please choose(1-5):");
String w=br.readLine();
int n=Integer.parseInt(w);
switch(n)
{
case 1: stat=con.prepareStatement("update CommodityDetail set cCommodityID=? where cCommodityID=?");
System.out.print("Enter the new cCommodityID(6) : ");break;
case 2: stat=con.prepareStatement("update CommodityDetail set cSupplierID=? where cCommodityID=?");
System.out.print("Enter the new cSupplierID(6) : ");break;
case 3: stat=con.prepareStatement("update CommodityDetail set vCommodityName=? where cCommodityID=?");
System.out.print("Enter the new vCommodityName(50) : ");break;
case 4: stat=con.prepareStatement("update CommodityDetail set vCommodityType=? where cCommodityID=?");
System.out.print("Enter the new vCommodityType(20) : ");break;
case 5: stat=con.prepareStatement("update CommodityDetail set iCommodityQty=? where cCommodityID=?");
System.out.print("Enter the new iCommodityQty(int) : ");break;
case 6: stat=con.prepareStatement("update CommodityDetail set iCommodityThoreshold=? where cCommodityID=?");
System.out.print("Enter the new iCommodityThoreshold(int) : ");break;
default:break;
}
String newdata=br.readLine();
if(n>4)
{
stat.setInt(1,Integer.parseInt(newdata));
}
else stat.setString(1,(String)newdata);
stat.setString(2,(String)cCommodityID);
res=stat.executeUpdate();
if (res!=0) System.out.println("-------Your details have been updata-------");
}
catch(Exception e)
{
System.out.println("Could not Updata: "+e);
}
if (con!=null)con=null;
Log l=new Log();
l.GetDate();
FileWriter outF = new FileWriter("log.txt",true);
outF.write(" Updated table commodity detail \r\n");
outF.write("-----------------------------------------------------\r\n");
outF.close();
Commodity c=new Commodity();
System.out.println("--------------Update Detail-------------------");
c.Display();
}
public void DeleteCommodity()throws IOException
{
System.out.println("-----------------4.DeleteCommodity--------------------");
Connection con=null;
PreparedStatement stat=null;
int res=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:Commodity");
stat=con.prepareStatement("delete CommodityDetail where cCommodityID=?");
System.out.print("Enter cCommodityID(6):");
String cCommodityID=br.readLine(); //從鍵盤讀入值存入字串變量中
stat.setString(1,cCommodityID); //用值替換SQL語句中的問號參數
res=stat.executeUpdate();
if (res!=0) System.out.println("-------Your details have been registered-------");
}
catch(Exception ex)
{
System.out.println("Could not Delete: "+ex);
}
if (con!=null) con=null;
Log l=new Log();
l.GetDate();
FileWriter outF = new FileWriter("log.txt",true);
outF.write(" Deleted a commodity \r\n");
outF.write("-----------------------------------------------------\r\n");
outF.close();
Commodity c=new Commodity();
System.out.println(" ");
System.out.println("----------The Detail that after delete----------");
c.DisplayCommodity();
}
public static void main(String args[])throws IOException
{
Commodity c=new Commodity();
c.CommodityMenu();
}
}
//-----------------------------------------------------------------------------------------------------------------//
//---------------------------------------------Here's Supplier Detail----------------------------------------------//
class Supplier
{
static int n;
static String ID;
public void SupplierMenu()throws IOException
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -