Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import DTO.Livro;
- import java.util.LinkedList;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import java.sql.*;
- public class coneccaoBD {
- private Connection conn;
- private Statement query;
- private ResultSet r;
- public coneccaoBD() {
- Driver driver = null;
- try{
- Class c = Class.forName("com.mysql.jdbc.Driver");
- driver = (Driver) c.newInstance();
- DriverManager.registerDriver((Driver)driver);
- iniciarConexao();
- }
- catch(Exception e){
- System.out.println("errro coneccao");
- }
- }
- // public void iniciarDriver() {
- // try {
- // Connection con =
- // } catch (Exception ex) {
- // System.out.println("Erro ao iniciar o Driver.");
- // }
- // }
- public void iniciarConexao() throws SQLException {
- String url = "jdbc:mysql://localhost/pr?" + "user=rootx&password=pass";
- conn = (Connection) DriverManager.getConnection(url);
- System.out.println(url);
- }
- public void iniciarStatement() throws SQLException {
- query = (Statement) conn.createStatement();
- }
- public void executaUpdate(String sql) throws SQLException {
- query.executeUpdate(sql);
- }
- public ResultSet executaQuery(String sql) throws SQLException {
- return query.executeQuery(sql);
- }
- public void fechaStatement() throws SQLException {
- query.close();
- }
- public void close() throws SQLException {
- conn.close();
- }
- public boolean verificaUser(String name, String pass) {
- try {
- this.iniciarStatement();
- r = query.executeQuery("SELECT * FROM admins");
- while (r.next()) {
- String Adminusername = r.getString("username");
- String Adminpassword = r.getString("password");
- if (Adminusername.equals(name) && Adminpassword.equals(pass)) {
- this.fechaStatement();
- return true;
- }
- }
- this.fechaStatement();
- return false;
- } catch (SQLException ex) {
- // Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
- }
- return false;
- }
- public LinkedList<Livro> getLivros() {
- LinkedList<Livro> livros = new LinkedList<Livro>();
- ResultSet result;
- Livro livro;
- try {
- this.iniciarStatement();
- String queryString = "select * from livro";
- result = query.executeQuery(queryString);
- while (result.next()) {
- livro = new Livro(result.getString("nome"), result.getString("autor"), result.getString("descricao"));
- livros.add(livro);
- }
- this.fechaStatement();
- } catch (SQLException ex) {
- Logger.getLogger(coneccaoBD.class.getName()).log(Level.SEVERE, null, ex);
- }
- return livros;
- }
- public void insereUser(String username, String name, String pass, String email) {
- try {
- this.iniciarStatement();
- String stringQuery = "INSERT INTO user (username, name, password, email, registerDate) VALUES (" + "'" + username + "'" + ", " + "'" + name + "'" + ", " + "'" + pass + "'" + ", " + "'" + email + "'" + "," + "curdate())";
- System.out.println(stringQuery);
- query.executeUpdate(stringQuery);
- this.fechaStatement();
- } catch (SQLException ex) {
- // Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public void alterarPassword(String username, String pass){
- //update ..
- }
- //Insere os mails na newsletter..
- public void insereEmail(String email) {
- try {
- this.iniciarStatement();
- String stringQuery = "INSERT INTO email VALUES (' "+ email + "')";
- query.executeUpdate(stringQuery);
- this.fechaStatement();
- } catch (SQLException ex) {
- // Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement