package BD;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.eclipse.swt.widgets.List;

public class CriaFatura {	

	public boolean NovaFatura(String cliente, List l3, int v_preco[],
			int v_qtde[]) {
		try {
			//a fatura cria uma tabela com o nome do cliente
			//com seus produtos
			Connection con = ConnectionFactory.getConnection();
			PreparedStatement stmt = con.prepareStatement("CREATE TABLE "
					+ cliente + "(`produto` VARCHAR( 255 ) NOT NULL ,"
					+ "`valor` INT( 12 ) NOT NULL ,"
					+ "`qtde` INT( 6 ) NOT NULL);");

			stmt.execute();
			stmt.close();			

			int cont = l3.getItemCount();
			for (int i = 0; i < cont; i++) {

				PreparedStatement stmt2 = con.prepareStatement("insert into "
						+ cliente + "" + "(produto,valor,qtde) values "
						+ "(?,?,?)");

				stmt2.setString(1, l3.getItem(i));
				stmt2.setInt(2, v_preco[i]);
				stmt2.setInt(3, v_qtde[i]);
				stmt2.execute();
				stmt2.close();
			}
			con.close();
			return true;
		} catch (SQLException e) {
			try {				
				Connection con = ConnectionFactory.getConnection();

				int cont = l3.getItemCount();
				for (int i = 0; i < cont; i++) {

					PreparedStatement stmt3 = con
							.prepareStatement("insert into " + cliente + ""
									+ "(produto,valor,qtde) values "
									+ "(?,?,?)");

					stmt3.setString(1, l3.getItem(i));
					stmt3.setInt(2, v_preco[i]);
					stmt3.setInt(3, v_qtde[i]);					
					stmt3.execute();
					stmt3.close();
				}				
				con.close();
				return true;
			} catch (SQLException e1) {
				return false;
			}
		}
		
	}
}
