Last active
June 6, 2016 15:38
-
-
Save EricsonWillians/12ea690d84a44da5158ecc0cebdc70f7 to your computer and use it in GitHub Desktop.
Gaby Faculdade
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
import java.sql.*; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
public class Estabelecimento<T> { | |
final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; | |
final String DB_URL = "jdbc:mysql://localhost/bd_gopg"; // Mudar o host / endereço do banco de dados aqui. | |
final String USER = "root"; // Mudar o usuário do banco de dados aqui. | |
final String PASS = ""; // Mudar a senha do banco de dados aqui. | |
T input = null; | |
Integer fk = 0; | |
String searchKeyword = null; | |
Connection conn = null; | |
final String[][] dbtc = { // DB Tables and Columns | |
{"email_estabelecimento", "nm_email"}, | |
{"endereco_estabelecimento", "nm_rua_estabelecimento"}, | |
{"endereco_estabelecimento", "cd_numero_estabelecimento"}, | |
{"endereco_estabelecimento", "nm_predio_estabelecimento"}, | |
{"endereco_estabelecimento", "cd_apto_estabelecimento"}, | |
{"endereco_estabelecimento", "ds_complemento_estabelecimento"}, | |
{"facebook_estabelecimento", "nm_facebook_estabelecimento"}, | |
{"facebook_estabelecimento", "nm_link_facebook_estabelecimento"}, | |
{"googlemais_estabelecimento", "nm_googlemais_estabelecimento"}, | |
{"googlemais_estabelecimento", "nm_link_googlemais_estabelecimento"}, | |
{"tb_estabelecimento", "nm_estabelecimento"}, | |
{"tb_estabelecimento", "nm_site_estabelecimento"}, | |
{"tb_sub_categoria", "nm_sub_categoria"}, | |
{"telefone_estabelecimento", "cd_telefone_estabelecimento"}, | |
{"twitter_estabelecimento", "nm_twitter_estabelecimento"}, | |
{"twitter_estabelecimento", "nm_link_estabelecimento_twitter"} | |
}; | |
public Estabelecimento(T input) throws SQLException { | |
this.input = input; | |
if (this.input instanceof Integer || this.input.getClass().getSimpleName() == "Integer") { | |
this.fk = (Integer) this.input; | |
} else if (this.input instanceof String) { | |
this.searchKeyword = (String) this.input; | |
} | |
try { | |
Class.forName(this.JDBC_DRIVER); | |
this.conn = DriverManager.getConnection(this.DB_URL, this.USER, this.PASS); | |
for (String[] s : dbtc) { | |
findFk(s[0], s[1]); | |
} | |
} catch(SQLException se) { | |
se.printStackTrace(); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private void findFk(String table, String column) throws SQLException { | |
if (this.searchKeyword != null && this.fk == 0) { | |
String sql; | |
sql = "SELECT fk FROM " + table + " WHERE " + column + " = ?"; | |
PreparedStatement stmt = this.conn.prepareStatement(sql); | |
stmt.setString(1, this.searchKeyword); | |
ResultSet rs = stmt.executeQuery(); | |
while(rs.next()) { | |
this.fk = rs.getInt("fk"); | |
} | |
rs.close(); | |
stmt.close(); | |
} | |
} | |
private String queryData(String table, String column) throws SQLException { | |
String sql; | |
sql = "SELECT " + column + " FROM " + table + " WHERE fk = ?"; | |
PreparedStatement stmt = this.conn.prepareStatement(sql); | |
stmt.setInt(1, this.fk); | |
ResultSet rs = stmt.executeQuery(); | |
String data = null; | |
while(rs.next()) { | |
data = rs.getString(column); | |
} | |
rs.close(); | |
stmt.close(); | |
return data; | |
} | |
public String getEmail() throws SQLException { | |
return this.queryData("email_estabelecimento", "nm_email"); | |
} | |
public String getFacebookPageName() throws SQLException { | |
return this.queryData("facebook_estabelecimento", "nm_facebook_estabelecimento"); | |
} | |
public String getFacebookAddress() throws SQLException { | |
return this.queryData("facebook_estabelecimento", "nm_link_facebook_estabelecimento"); | |
} | |
public String getGooglePlusPageName() throws SQLException { | |
return this.queryData("googlemais_estabelecimento", "nm_googlemais_estabelecimento"); | |
} | |
public String getGooglePlusAddress() throws SQLException { | |
return this.queryData("googlemais_estabelecimento", "nm_link_googlemais_estabelecimento"); | |
} | |
public String getName() throws SQLException { | |
return this.queryData("tb_estabelecimento", "nm_estabelecimento"); | |
} | |
public String getWebsite() throws SQLException { | |
return this.queryData("tb_estabelecimento", "nm_site_estabelecimento"); | |
} | |
public String getSubCategory() throws SQLException { | |
return this.queryData("tb_sub_categoria", "nm_sub_categoria"); | |
} | |
public String getPhone() throws SQLException { | |
return this.queryData("telefone_estabelecimento", "cd_telefone_estabelecimento"); | |
} | |
public String getTwitterPageName() throws SQLException { | |
return this.queryData("twitter_estabelecimento", "nm_twitter_estabelecimento"); | |
} | |
public String getTwitterAddress() throws SQLException { | |
return this.queryData("twitter_estabelecimento", "nm_link_estabelecimento_twitter"); | |
} | |
public HashMap<String, String> getMapAddress() throws SQLException { | |
String sql; | |
sql = "SELECT nm_rua_estabelecimento, cd_numero_estabelecimento, nm_predio_estabelecimento, cd_apto_estabelecimento, ds_complemento_estabelecimento FROM endereco_estabelecimento WHERE fk = ?"; | |
PreparedStatement stmt = this.conn.prepareStatement(sql); | |
stmt.setInt(1, this.fk); | |
ResultSet rs = stmt.executeQuery(); | |
HashMap<String, String> address = new HashMap<String, String>(); | |
while(rs.next()) { | |
address.put("Rua", rs.getString("nm_rua_estabelecimento")); | |
address.put("Número", rs.getString("cd_numero_estabelecimento")); | |
address.put("Prédio", rs.getString("nm_predio_estabelecimento")); | |
address.put("Apartamento", rs.getString("cd_apto_estabelecimento")); | |
address.put("Complemento", rs.getString("ds_complemento_estabelecimento")); | |
} | |
rs.close(); | |
stmt.close(); | |
return address; | |
} | |
public ArrayList<String> getListAddress() throws SQLException { | |
String sql; | |
sql = "SELECT nm_rua_estabelecimento, cd_numero_estabelecimento, nm_predio_estabelecimento, cd_apto_estabelecimento, ds_complemento_estabelecimento FROM endereco_estabelecimento WHERE fk = ?"; | |
PreparedStatement stmt = this.conn.prepareStatement(sql); | |
stmt.setInt(1, this.fk); | |
ResultSet rs = stmt.executeQuery(); | |
ArrayList<String> address = new ArrayList<String>(); | |
while(rs.next()) { | |
address.add(rs.getString("nm_rua_estabelecimento")); | |
address.add(rs.getString("cd_numero_estabelecimento")); | |
address.add(rs.getString("nm_predio_estabelecimento")); | |
address.add(rs.getString("cd_apto_estabelecimento")); | |
address.add(rs.getString("ds_complemento_estabelecimento")); | |
} | |
rs.close(); | |
stmt.close(); | |
return address; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment