Hi,
I am currently doing a mini project using Eclipse with Java language.. i needed to submit some values into the DB, but i am having some trouble to do so... i would like to find out if i need to create variables for all the values i want to submit? and please do help me with the code below. btw, i am using date/time datatype for the date and time and autonum for invoice num... THanks
public class Payment {
private int InvoiceNo;
private String Date;
private String Time;
private int TableNo;
private String PayMode;
private String ProductName;
private int Qty;
private String Size;
private double UnitPrice;
private double TotalPrice;
private double SubTotal;
private float GST;
private float SurCharge;
private float MemDisc;
private float DelCharge;
private double GrandTotal;
private double Tendered;
private double Changed;
private String Name;
private int CreditCardNo;
private String CreditCardType;
private String ExpDate;
private int CVC;
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
public int getCreditNo() {
return CreditCardNo;
}
public void setCreditNo(int creditNo) {
this.CreditCardNo = creditNo;
}
public String getExpiryDate() {
return ExpDate;
}
public void setExpiryDate(String expiryDate) {
ExpDate = expiryDate;
}
public String getCreditType() {
return CreditCardType;
}
public void setCreditType(String creditType) {
this.CreditCardType = creditType;
}
public int getCVC() {
return CVC;
}
public void setCVC(int cvc) {
CVC = cvc;
}
public float getGST() {
return GST;
}
public void setGST(float gst) {
GST = gst;
}
public Payment() {
// TODO Auto-generated constructor stub
}
public boolean retrieveProduct(){
// declare local variables
boolean success = false;
ResultSet rs = null;
DBController db = new DBController();
// step 1 of using DBcontroller, passing data source name setup during last practical
db.setUp("SweetParadise");
// declare the SQL
String dbQuery = "SELECT * FROM PAYMENT WHERE InvoiceNo =" + InvoiceNo;
// step 2 of using DBcontroller, for retrieve SQL use readRequest method
rs = db.readRequest(dbQuery);
try{
if (rs.next()){
InvoiceNo = rs.getInt("InvoiceNo");
/*name = rs.getString("ProductName");
image = rs.getString("ProductImage");
unitPrice = rs.getDouble("ProductUnitPrice");
*/
success = true;
}
}
catch (Exception e) {
e.printStackTrace();
}
// step 3 of using DBcontroller
db.terminate();
return success;
}
public boolean createPayment(){
// declare local variables
boolean success = false;
ResultSet rs = null;
DBController db = new DBController();
// step 1 of using DBcontroller, passing data source name setup during last practical
db.setUp("SweetParadise");
// declare the SQL
String dbQuery = "INSERT INTO PAYMENT(InvoiceNo, Date, Time, TableNo, PayMode, ";
dbQuery = dbQuery + "ProductName, Qty, Size, UnitPrice, TotalPrice, ";
dbQuery = dbQuery + "SubTotal, GST, SurCharge, MemDisc, DelCharge, GrandTotal, ";
dbQuery = dbQuery + "Tendered, Changed, Name, CreditCardNo, CreditCardType, ExpDate, CVC) ";
dbQuery = dbQuery + "VALUES (" + InvoiceNo + ",'" + Date + "', " + Time + ", ";
dbQuery = dbQuery + "'" + TableNo + "', '" + PayMode + "' , '" + ProductName + "', '" + Qty + "',";
dbQuery = dbQuery + "'" + Size + "', '" + UnitPrice + "', '" + TotalPrice + "', ";
dbQuery = dbQuery + "'" + SubTotal + "', '" + GST + "', '" + SurCharge + "', '" + MemDisc + "', " ;
dbQuery = dbQuery + "'" + DelCharge + "', '" + GrandTotal + "', '" + Tendered + "', '" + Changed + "', '" + Name + "',";
dbQuery = dbQuery + "'" + CreditCardNo + "', '" + CreditCardType + "', '" + ExpDate + "', '" + CVC + "')";
// step 2 of using DBcontroller, use updateRequestKey method if you want the value of
// the key back (usually, if the key is autoNumber)
rs = db.updateRequestKey(dbQuery);
try{
if (rs.next()){
// this will return the ID which is a autoNumber primary key
InvoiceNo = rs.getInt("InvoiceNo");
success = true;
}
}
catch (Exception e) {
e.printStackTrace();
}
// step 3 of using DBcontroller
db.terminate();
return success;
}
/*public static ArrayList<Product> getProductByCategory(int inCatID){
// declare local variables
ArrayList<Product> pdtList = new ArrayList<Product>();
ResultSet rs = null;
DBController db = new DBController();
// step 1 of using DBcontroller, passing data source name setup during last practical
db.setUp("myDatabase");
String dbQuery = "SELECT * FROM PRODUCTS WHERE ProductCategoryID = " + inCatID;
// step 2 of using DBcontroller, use updateRequest method
rs = db.readRequest(dbQuery);
try{
while (rs.next()){
int pdtID = rs.getInt("ProductID");
int pdtCatID = rs.getInt("ProductCategoryID");
String pdtName = rs.getString("ProductName");
String pdtImage = rs.getString("ProductImage");
double pdtUnitPrice = rs.getDouble("ProductUnitPrice");
Product pdt = new Product(pdtID, pdtCatID, pdtName, pdtImage, pdtUnitPrice);
pdtList.add(pdt);
}
}
catch (Exception e) {
e.printStackTrace();
}
// step 3 of using DBcontroller
db.terminate();
return pdtList;
}
*/
public static void main(String[] args) {
Payment p1 = new Payment();
p1.setName("test");
p1.setCreditNo(3567843);
System.out.println(p1.createPayment());
}
}