如何将Derby数据库与Servlet连接?

发布于 2024-08-23 06:16:46 字数 268 浏览 6 评论 0原文

我以前从未用java连接过数据库。我可以知道我是否应该使用 servlet 访问 derby 数据库吗?

我已经检查过:如何从 servlet 或 JSP 访问数据库?< /a> 但我看到这篇文章的评论说这是一种不好的联系方式。任何人都可以解释或向我展示我应该编写代码来访问我的德比数据库的最佳方法吗?

非常感谢。

I have never connected to a database in java before. May I know if I should go about accessing a derby database with servlet?

I have checked this: How do I access a database from my servlet or JSP?
But I saw comments on the article saying that this is a bad way to connect. Could any one explain or show me the best way to that I should code to access my derby database?

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嘿哥们儿 2024-08-30 06:16:46

他们的建议确实是对的。我们不会直接从 Servlet 或 JSP 访问数据库,这两者都应该是 Web 层,不是吗?

那么,该怎么办呢?获取 JDBC 教程。 官方的是一个很好的选择。这将使您对从 Java 连接到数据库有一个很好的了解,并掌握 JDBC API。之后,您应该去阅读DAO 模式,以及我们如何在实际应用程序中使用它

此外,我认为您还应该阅读 MVC 模式,因为在我看来,您是对此也不是很清楚。

一旦你理解了所有这些并使用所有这些东西想出了一个类似玩具的应用程序。下一步是研究连接池机制。

They are all right indeed, in suggesting that. We don't don't access database directly from Servlets or JSPs, these both are meant to be web tier, isn't it?

So, what to do? Grab a JDBC tutorial. The official one is an excellent choice here. That will give you a good idea about connecting to database from Java, and grasp over JDBC API. After that you should go and read about DAO pattern, and how we employ that in real apps.

Moreover, I think you also should read about MVC pattern, because it seems to me that you are not very clear on that as well.

Once you understand all these and come up with a toy like application using all these stuff. Next step would be to have a look into Connection Pooling mechanism.

-残月青衣踏尘吟 2024-08-30 06:16:46

由于您使用的是servlt,因此您必须使用容器系列Apache Tomcat。您应该像这样定义一个连接池 http: //tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html。如果您使用任何其他容器,那么它也将具有类似的设置。

其他选项是创建一个单独的 DBManager 类,它负责初始化和返回连接。您可以在 servlet 中使用该类。

Since you are using servelt you must be using a container line Apache Tomcat. You should look to define a connection pool like this http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html. If you are using any other container then that will also have similar setup.

Other option is to create a separate DBManager kind of class which looks after initializing and returning connection. This class you can use in the servlet.

等往事风中吹 2024-08-30 06:16:46

使用 JDBC 并拥有应用程序服务器的应用程序池是一个好的开始。您还可以使用一些 API 来让您的生活更轻松,例如 Hibernate。

Using JDBC and having your app server's application pool is a good start. You can also use some API to make your life easier like Hibernate.

叫思念不要吵 2024-08-30 06:16:46

这是一种“坏方法”,因为它不使用(JNDI 管理的)连接池来获取连接。尽管获取连接“仅”花费几百毫秒,但这在繁忙的多用户环境中会产生影响。连接池会担心打开和关闭连接,并在每次调用 getConnection() 时立即释放它们,因此实际上花费的时间几乎为零毫秒。如果您在繁忙的多用户环境中总结一下,那么差异是显而易见的。

连接池通常以 JNDI 数据源的形式进行配置,该数据源由相关 servlet 容器管理。由于您没有提到您正在使用哪一个,我最多可以指出我的一个答案,其中包含 Tomcat 6.0 目标示例:这里

希望这有帮助。

It is a "bad way", because it doesn't make use of a (JNDI-managed) connection pool to obtain connections. Although acquiring a connection costs "only" a few hundred milliseconds, this has impact in a busy multiuser environment. A connection pool will worry about opening and closing connections and release them immediately on every getConnection() call so that it effectively costs almost zero milliseconds. If you sum that up in a busy multiuser environment, then the differences are noticeable.

A connection pool is usually to be configured in flavor of a JNDI datasource which is managed by the servletcontainer in question. As you didn't mention which one you're using, I can at highest point to one of my answers which contains a Tomcat 6.0 targeted example: here.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文