Package Helpers

Examples of Helpers.DbHelper


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String username = jTextField1.getText().toString();
        String password = new String(jPasswordField1.getPassword());

        DbHelper dbHelper = new DbHelper();
        AuthHelper authHelper = new AuthHelper();
        Connection conn = dbHelper.getConnection();

        if (username.length() == 0 || password.length() == 0) {
            JOptionPane.showMessageDialog(null, "Enter Both Username And Password.", "There's a Problem!", JOptionPane.INFORMATION_MESSAGE);

        } else {
View Full Code Here


        return author;
    }
   
    public int update(Author author){
        //how to update only changed fields ?
        DbHelper dbHelper = new DbHelper();
        Connection conn = dbHelper.getConnection();
        String query = "UPDATE `authors` SET `full_name` = ?, `location` = ? , `age` = ? ,`details` = ? WHERE `id` = ?;";
        int inserted_flag = 0;
        PreparedStatement prepStmt = null;
        try {
            prepStmt = conn.prepareStatement(query);
            prepStmt.setString(1, author.getFull_name());
            prepStmt.setString(2, author.getLocation());
            prepStmt.setInt(3, author.getAge());
            prepStmt.setString(4, author.getDetails());
            prepStmt.setInt(5,author.getId());
            inserted_flag = prepStmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(Author.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            dbHelper.closeAllConnections(conn, prepStmt, null);
        }
        return inserted_flag;
       
    }
View Full Code Here

    public BookModel() {
    }
   
    public int save(Book book){
        DbHelper dbHelper = new DbHelper();
        Connection conn = dbHelper.getConnection();
        String query = "INSERT INTO `" + table_name + "`(`name`,`author_id`,`price`,`total_pages`) VALUES (?,?,?,?)";
        int inserted_flag = 0;
        PreparedStatement prepStmt = null;
        try {
            prepStmt = conn.prepareStatement(query);
            prepStmt.setString(1, book.getName());
            prepStmt.setInt(2, book.getAuthor_id());
            prepStmt.setFloat(3, book.getPrice());
            prepStmt.setInt(4, book.getTotal_pages());
                   
            inserted_flag = prepStmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(Author.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            dbHelper.closeAllConnections(conn, prepStmt, null);
        }
        return inserted_flag;
    }
View Full Code Here

    }
   
    public List all(){
       
        books = new ArrayList<>();
        DbHelper dbHelper = new DbHelper();
        Connection conn = dbHelper.getConnection();
        ResultSet rs = null;
        String query = "SELECT * FROM "+table_name;
        Statement stmt = null;
       
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);
            while (rs.next()) {
                Book book = setterForBook(rs);
               
                books.add(book);
            }

        } catch (SQLException ex) {
            Logger.getLogger(Author.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("you are fuxckt");
        } finally {
                dbHelper.closeConnection(conn);
                dbHelper.closeStatement(stmt);
                dbHelper.closeResultSet(rs);              
        }
        return books;
       
    }
View Full Code Here

    }
   
    public List whereName(String book_name){
       
        books = new ArrayList<>();
        DbHelper dbHelper = new DbHelper();
        Connection conn = dbHelper.getConnection();
        ResultSet rs = null;
        String query = "SELECT * FROM "+table_name+" WHERE name LIKE ?";
          
        PreparedStatement prepStmt = null ;
        try {
            prepStmt = conn.prepareStatement(query);
            String book_name_query = book_name+"%";
            prepStmt.setString(1, book_name_query);
            rs = prepStmt.executeQuery();
            System.out.println("Book to search "+book_name);
            while (rs.next()) {
                Book book = setterForBook(rs);
               
                books.add(book);
            }

        } catch (SQLException ex) {
            Logger.getLogger(Author.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("you are fuxckt");
        } finally {
                dbHelper.closeConnection(conn);
                dbHelper.closePreparedStmt(prepStmt);
                dbHelper.closeResultSet(rs);              
        }
        return books;
       
    }
View Full Code Here

    public AuthorModel() {
    }
   
    public int save(Author author) {

        DbHelper dbHelper = new DbHelper();
        Connection conn = dbHelper.getConnection();
        String query = "INSERT INTO `" + table_name + "`(`full_name`,`location`,`age`,`details`) VALUES (?,?,?,?)";
        int inserted_flag = 0;
        PreparedStatement prepStmt = null;
        try {
            prepStmt = conn.prepareStatement(query);
            prepStmt.setString(1, author.getFull_name());
            prepStmt.setString(2, author.getLocation());
            prepStmt.setInt(3, author.getAge());
            prepStmt.setString(4, author.getDetails());
            inserted_flag = prepStmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(Author.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            dbHelper.closeAllConnections(conn, prepStmt, null);
        }
        return inserted_flag;
    }
View Full Code Here

    }
  
    public List all() {

        authors = new ArrayList<>();
        DbHelper dbHelper = new DbHelper();
        Connection conn = dbHelper.getConnection();
        ResultSet rs = null;
        Statement stmt = null;
        try {
            stmt = conn.createStatement();
            String query = "SELECT * FROM "+table_name;
            rs = stmt.executeQuery(query);
           
            while (rs.next()) {
                Author author = setterForAuthor(rs); // call to private function
          
                authors.add(author);
            }

        } catch (SQLException ex) {
            Logger.getLogger(Author.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("you are fuxckt");
        } finally {
                dbHelper.closeConnection(conn);
                dbHelper.closeStatement(stmt);
                dbHelper.closeResultSet(rs);              
        }
        return authors;
    }
View Full Code Here

  
    public List whereFullName(String authorName) {

        authors = new ArrayList<>();
        DbHelper dbHelper = new DbHelper();
        Connection conn = dbHelper.getConnection();
        ResultSet rs = null;
        String query = "SELECT * FROM "+table_name+" WHERE full_name LIKE ?";
          
        PreparedStatement prepStmt = null ;
        try {
            prepStmt = conn.prepareStatement(query);
            String authorName_query = authorName+"%";
            prepStmt.setString(1, authorName_query);
            rs = prepStmt.executeQuery();
           
            while (rs.next()) {
                Author author = setterForAuthor(rs);    //call to private function
          
                authors.add(author);
            }

        } catch (SQLException ex) {
            Logger.getLogger(Author.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("you are fuxckt");
        } finally {
                dbHelper.closeConnection(conn);
                dbHelper.closePreparedStmt(prepStmt);
                dbHelper.closeResultSet(rs);              
        }
        return authors;
    }
View Full Code Here

    }
   
    public Author whereId(int author_id){
       
        Author author = new Author();
        DbHelper dbHelper = new DbHelper();
        Connection conn = dbHelper.getConnection();
        ResultSet rs = null;
        String query = "SELECT * FROM "+table_name+" WHERE id = ?";
          
        PreparedStatement prepStmt = null ;
        try {
            prepStmt = conn.prepareStatement(query);
           
            prepStmt.setInt(1, author_id);
            rs = prepStmt.executeQuery();
            if(rs.first())
            {   
                 author = setterForAuthor(rs);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Author.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("you are fuxckt");
        } finally {
                dbHelper.closeConnection(conn);
                dbHelper.closePreparedStmt(prepStmt);
                dbHelper.closeResultSet(rs);              
        }
        return author;
    }
View Full Code Here

TOP

Related Classes of Helpers.DbHelper

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.