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());
 
	}
 
}

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

Hello,

Well if you want to dynamically add data into DB then you have to use variables. Its good programming practice to use variable ( ofcourse for essential ones only ).

Please mention the problem you are facing while inserting data to DB.

Thanks.

1. I don't see anything wrong with the query code here.
2. If you want to send the data for just two variables mention just those variables in the insert into clause for e.g. if you want to insert just date and time write the query as follows : INSERT INTO tablename(date,time) values ('%datevalue%','%timevalue%'); this will work just as fine.
3. Instead of having two independent columns, one for date and the other for time you caould have a single datetime or timestamp column. Look into the specs of each to find out which one is more appropriate in your case.
4. Since InvoiceNum is an auto generated column, you need not pass data for it (I assume it is something like AUTO_INCREMENT in MySQL) you can omit this coulmn name and it's corresponding value from the query.

Alsoyou have not stated what error as such you are getting, mention that to get more specific help.

Oh, he's talking about variables in SQL Query.

Table structure :
-----------------------
Name : testTable
-----------------------
userID - int
username - varchar(20)
password - varchar(20)
-----------------------

Queries:
If you know the table attributes datatype and series.

insert into testTable values (1,'user','pass');

If you want to left any of attribute null (only if it can hold null value).

insert into testTable (username, password ) values ('user','pass');

if you only know the attributes name but dont know the series.

insert into testTable (username, userID, password ) values ('user',1,'pass');

Regards!

Hi, Thanks for all the replies.. I was facing some syntax issue earlier, but i tried retyping the Query over again and it works well now.. However, i still have another issue right now... If i have a form which i need the user to fill up and submit to the DB, should each textbox have a variable along with it as well? and also, i have some issue creating a button group for the Radiobutton option, could someone provide a simple code for a couple of radiobutton which are grouped.. Thanks... Appreciate all the help!!!

private JRadioButton male;
private JRadioButton female;
private ButtonGroup gender;

// create the radiobuttons
male = new JRadioButton();
female = new JRadioButton();
gender = new ButtonGroup();

// set the necessary parameters for the radio buttons
male.setText("M");
male.setSelected(true);
female.setText("F");
female.setSelected(false);

/*
*can be used to catch the button on which the event is generated  * (by checking the action commad)
*/
male.setActionCommand("Male");
female.setActionCommand("Female");

// finally add the two buttons to the 'gender group'
gender.add(male);
gender.add(female);

Hi Verruckt24, Thanks for being really helpful, i have tried the above coding, and it works well, however, if i would like to input the radiobutton into a dialog box, is there anything which i might need to tweak a little? Thanks once again. And for my insert query, i got a error saying "Number of query values and destination fields are not the same.". What does this mean?

And for my insert query, i got a error saying "Number of query values and destination fields are not the same.". What does this mean?

Probably the number of values you are using are not the same with the number of columns you have in your query.
Pay more attention to puneetkay's post.
Also try posting the query you are trying to run

Hi,

I have progressively overcome the above stages... However, i have met another issue regarding the Submission button, i would like to execute the above coding thru clicking of the button, may i find out how this can be done? Apologies as i am still very new to all this programming stuffs.. Hope you guys could help me out.. Thanks

Thanks for the helping tutorial above... i have yet another question to ask regarding the button thingy... i currently have a entity, which i would like to call using the click of a button, is this possible? Thanks

That is the main purpose behind action listener that on mouse click you want to get something done

JButton btn = new JButton();
btn.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent ae){
        //call which ever method or numerous methods to do something
        //retrieve data from form fields and send it for validation
       //get data from database and fill form fields
    }
}
This question has already been answered. Start a new discussion instead.