Skip to content

Commit

Permalink
Actividad 11
Browse files Browse the repository at this point in the history
  • Loading branch information
luisagalva committed Jan 12, 2018
1 parent 5a3dcc8 commit f597772
Show file tree
Hide file tree
Showing 16 changed files with 409 additions and 183 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<classpathentry kind="lib" path="/Users/luisa/myRepo/Java101/src/lib/log4j-1.2-api-2.9.1.jar"/>
<classpathentry kind="lib" path="/Users/luisa/myRepo/Java101/src/lib/log4j-api-2.9.1.jar"/>
<classpathentry kind="lib" path="/Users/luisa/myRepo/Java101/src/lib/log4j-iostreams-2.9.1.jar"/>
<classpathentry kind="lib" path="/Users/luisa/myRepo/Java101/src/lib/sqlite-jdbc-3.20.0.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Empty file added Chinook
Empty file.
Binary file added bin/db/Chinook.class
Binary file not shown.
Binary file added bin/db/Customer.class
Binary file not shown.
Binary file added bin/db/CustomerAdmin.class
Binary file not shown.
Binary file removed bin/io/AAdmin.class
Binary file not shown.
Binary file removed bin/io/NIO2RecursiveDir.class
Binary file not shown.
2 changes: 1 addition & 1 deletion bin/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</File>
</Appenders>
<Loggers>
<Logger name="io.AAdmin" level="info">
<Logger name="db.CustomerAdmin" level="info">

</Logger>
<Root level="error"><AppenderRef ref="Logfile" /></Root>
Expand Down
Binary file added database/Chinook.db
Binary file not shown.
107 changes: 107 additions & 0 deletions src/db/Chinook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class Chinook {
private String database;
private Connection connection;
private Statement statement;

public Chinook(String db) throws ClassNotFoundException, SQLException{
this.database = db;
// 1. Obtener el Driver correcto para la base de datos
// Escribe tu código aquí{
Class.forName("org.sqlite.JDBC");
// }
this.connection = DriverManager.getConnection("jdbc:sqlite:" + database);
this.statement = connection.createStatement();
}

public Connection getConnection() {
return connection;
}

public ArrayList<Customer> getCustomers() throws SQLException{
ResultSet rs = this.statement.executeQuery("select * from customer");
ArrayList<Customer> customers = new ArrayList<Customer>();
while (rs.next()){
Customer temp = new Customer();
temp.setCustomerId(rs.getInt("CustomerId"));
temp.setFirstName(rs.getString("FirstName"));
temp.setLastName(rs.getString("LastName"));
temp.setCompany(rs.getString("Company"));
temp.setAddress(rs.getString("Address"));
temp.setCity(rs.getString("City"));
temp.setState(rs.getString("State"));
temp.setCountry(rs.getString("Country"));
temp.setPostalCode(rs.getString("PostalCode"));
temp.setPhone(rs.getString("Phone"));
temp.setFax(rs.getString("Fax"));
temp.setEmail(rs.getString("Email"));
temp.setSupportRepId(rs.getInt("SupportRepId"));
customers.add(temp);
}
return customers;
}

public boolean addCustomer(Customer customer) throws SQLException{
String sql = "insert into Customer(FirstName, LastName, Company, Address, "
+ "City, State, Country, PostalCode, Phone, Fax, Email, SupportRepId) "
+ "values (?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement prepStmt = this.connection.prepareStatement(sql);
// 2. Agregar los atributos del objeto customer al PreparedStatement
// Escribe tu código aquí{
prepStmt.setString(1,customer.getFirstName());
prepStmt.setString(2, customer.getLastName());
prepStmt.setString(3,customer.getCompany());
prepStmt.setString(4, customer.getAddress());
prepStmt.setString(5, customer.getCity());
prepStmt.setString(6, customer.getState());
prepStmt.setString(7, customer.getCountry());
prepStmt.setString(8, customer.getPostalCode());
prepStmt.setString(9, customer.getPhone());
prepStmt.setString(10, customer.getFax());
prepStmt.setString(11, customer.getEmail());
prepStmt.setInt(12, customer.getSupportRepId());
// }
return prepStmt.execute();
}

public int deleteCustomer(Customer customer) throws SQLException{
String sql = "delete from Customer where CustomerId = ? ";
PreparedStatement prepStmt = this.connection.prepareStatement(sql);
prepStmt.setInt(1, customer.getCustomerId());
prepStmt.execute();
return prepStmt.getUpdateCount();
}

public int updateCustomer(Customer customer) throws SQLException{
String sql = "update customer set FirstName=?, LastName=?, Company=?, Address=?, City=?, "
+ "State=?, Country=?, PostalCode=?, Phone=?, Fax=?, Email=?, SupportRepId=?"
+ "where CustomerId = ?";
PreparedStatement prepStmt = this.connection.prepareStatement(sql);
// 3. Agregar los atributos del objeto customer al PreparedStatement
// Escribe tu código aquí{
prepStmt.setString(1,customer.getFirstName());
prepStmt.setString(2, customer.getLastName());
prepStmt.setString(3,customer.getCompany());
prepStmt.setString(4, customer.getAddress());
prepStmt.setString(5, customer.getCity());
prepStmt.setString(6, customer.getState());
prepStmt.setString(7, customer.getCountry());
prepStmt.setString(8, customer.getPostalCode());
prepStmt.setString(9, customer.getPhone());
prepStmt.setString(10, customer.getFax());
prepStmt.setString(11, customer.getEmail());
prepStmt.setInt(12, customer.getSupportRepId());
prepStmt.setInt(13, customer.getCustomerId());
// }
prepStmt.execute();
return prepStmt.getUpdateCount();
}
}
146 changes: 146 additions & 0 deletions src/db/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package db;

public class Customer {
// 1. Crear los atributos de la clase Customer en base a la definición de la base de datos
// Escribe tu codigo aqui {
private int customerId;
private String firstName;
private String lastName;
private String company;
private String address;
private String city;
private String state;
private String country;
private String postalCode;
private String phone;
private String fax;
private String email;
private int supportRepId;

// }

//2. Crear los métodos de acceso
// Escribe tu codigo aqui {


public int getCustomerId(){
return this.customerId;
}

public void setCustomerId(int customerId) {
this.customerId = customerId;
}

public String getFirstName() {
return this.firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}



public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public String getCompany(){
return this.company;
}

public void setCompany(String company){
this.company = company;
}


public String getAddress(){
return this.address;
}

public void setAddress(String address){
this.address = address;
}

public String getState() {
return this.state;
}

public void setState(String state) {
this.state = state;
}
public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getPostalCode() {
return this.postalCode;
}

public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}

public String getFax() {
return fax;
}

public void setFax(String fax) {
this.fax = fax;
}

public String getLastName(){
return this.lastName;
}

public void setLastName(String lastName){
this.lastName = lastName;
}

public String getEmail(){
return this.email;
}

public void setEmail(String email){
this.email = email;
}

public int getSupportRepId(){
return this.supportRepId;
}

public void setSupportRepId(int supportRepId){
this.supportRepId = supportRepId;
}



// }


@Override
public String toString(){
return String.format("%-5s %-10s %-20s %-25s %-15s %-5s", this.customerId, this.firstName,
this.lastName, this.company, this.email, this.supportRepId);
}




}
Loading

0 comments on commit f597772

Please sign in to comment.