package BD;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class PersisteCompra {
	
	public boolean NovaFatura(String cliente, int valor) throws SQLException {
		try {
			Connection con = ConnectionFactory.getConnection();
			PreparedStatement stmt = con.prepareStatement("CREATE TABLE "
					+ "compra (`cliente` VARCHAR( 255 ) NOT NULL ,"
					+ "`valor` INT( 12 ) NOT NULL );");					

			stmt.execute();
			stmt.close();				
			PreparedStatement stmt2 = con.prepareStatement("insert into compra"
					+ "(cliente, valor) values (?,?)");

			String a1 = cliente;
			int a2 = valor;
			stmt2.setString(1, a1);
			stmt2.setInt(2, a2);

			stmt2.execute();			
			stmt2.close();
			con.close();
			return true;
		} catch (SQLException e) {
			try {				
				Connection con = ConnectionFactory.getConnection();
				PreparedStatement stmt = con.prepareStatement("insert into compra"
						+ "(cliente, valor) values (?,?)");

				String a1 = cliente;
				int a2 = valor;
				stmt.setString(1, a1);
				stmt.setInt(2, a2);

				stmt.execute();			
				stmt.close();
				con.close();
				return true;
			} catch (SQLException e1) {
				return false;
			}		
		}		
	}

}
