Not directly. In fact, you should look at the DAO (Data Access Object) template.
the model classes themselves are responsible only for the transfer of information from one logical instance to another and should contain only geter and setter methods.
DAO classes are responsible for saving the update or retrieving information from some data source (database). Here is an example DAO pattern:
public class BookDAO { private PreparedStatement saveStmt; private PreparedStatement loadStmt; public DBBookDAO(String url, String user, String pw) { Connection con = DriverManager.getConnection(url, user, pw); saveStmt = con.prepareStatement("INSERT INTO books(isbn, title, author) " +"VALUES (?, ?, ?)"); loadStmt = con.prepareStatement("SELECT isbn, title, author FROM books " +"WHERE isbn = ?"); } public Book loadBook(String isbn) { Book b = new Book(); loadStmt.setString(1, isbn); ResultSet result = loadStmt.executeQuery(); if (!result.next()) return null; b.setIsbn(result.getString("isbn")); b.setTitle(result.getString("title")); b.setAuthor(result.getString("author")); return b; } public void saveBook(Book b) { saveStmt.setString(1, b.getIsbn()); saveStmt.setString(2, b.getTitle()); saveStmt.setString(3, b.getAuthor()); saveStmt.executeUpdate(); } }
source share