is it fine to have normal java class to handle database connection with servlet? [closed]

StackOverflow https://stackoverflow.com/questions/20779481

  •  21-09-2022
  •  | 
  •  

Вопрос

I am working on an web application using servlet jsp. now I am planning to write all database activity(queries and prepared statements and their execution) in normal java class and instantiate those class in servlets. just for reusability. could you please suggest me if it's a good approach? I am using : tomcat6, servlet2.5 jsp2,mysql

Это было полезно?

Решение

Yes u can use a separate plain java class to acess database...thats what the MVC architecture is, your java file will behave as model that handles..data and db connectivity, while your Servlet will behave as controller, that calls your business code..which is in a separate reusable java file..

Другие советы

If you are writing a web application accessing a DB, then I suggest that you use some Persistence API, such as JPA or Hibernate. if you are using Eclipse or another IDE, the ability to reverse engineer your Entities from the DB will save you a lot of errors and time.

See http://www.vogella.com/articles/JavaPersistenceAPI/article.html

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top