-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
luisagalva
committed
Jan 12, 2018
1 parent
5a3dcc8
commit f597772
Showing
16 changed files
with
409 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
|
||
|
||
} |
Oops, something went wrong.