JSP connection to the Derby Database

Source: Internet
Author: User

Derby database is a pure memory database implemented in Java and is an open-source project of Apache. Because it is implemented in Java, it can be run on any platform. Another feature is that it is small in size and free of installation. You only need a few small jar packages to run it.
The Derby database has two running modes:
1) embedded mode. The Derby database shares the same JVM with the application, which is usually started and stopped by the application. It is invisible to other applications except the ones that start it, that is, other applications cannot access it;
2) network mode. The Derby database exclusively occupies one JVM and runs as an independent process on the server. In this mode, multiple applications are allowed to access the same Derby database.
On Apache, Derby has four release packages. Here we use the bin release package as an example. The bin release package contains script files, demos, and jar files for executing the Derby database tool, setting the Derby database environment.

Here I use the myeclipse development environment, and the derby driver is provided by myeclipse.

To enable the Derby server, you must first enable tomcat.

1. Create a derby Database

In the menu bar of myeclipse, choose Window> show View> other...

Select dB browser-> OK

In the DB browser window, right-click New...

Go to the following page:

Next or directly finish.

Then, myderby appears in the DB browser window. Right-click and open connection (or click the connection button next to it)

After the connection is successful, you can create a table in the database (two methods are available, as shown below)

1. Click "myderby" and the drop-down list "connected to myderby" appears.

Right-click connected to myderby and click New SQL editor. Then, you can write the corresponding SQL language in the SQL window.

2. In the connected to myderby drop-down list, there are apps and nullid. You can create data tables graphically in the drop-down list.

How can I use the database in JSP?

1. First import the C:/user/. myeclipse/libs/derby_10.2.2.0/derbyclient. Jar driver to your JSP project webroot/WEB-INF/

2. Compile the JSP file with the following code:

Code:
  1. <% @ Page contenttype = "text/html; charset = gb2312" %>
  2. <% @ Page import = "Java. SQL. *" %>
  3. <HTML>
  4. <Head>
  5. <Title> database operation </title>
  6. </Head>
  7. <Body>
  8. <%
  9. Connection conn = NULL;
  10. Try
  11. {
  12. Class. forname ("org. Apache. Derby. JDBC. clientdriver ");
  13. String url = "JDBC: Derby: // 127.0.0.1: 1527/mydb; Create = true ";
  14. Conn = drivermanager. getconnection (URL, "name", "password ");
  15. Statement ST = conn. createstatement ();
  16. Resultset rs=st.exe cutequery ("select * From loginmsg ");
  17. While (Rs. Next ())
  18. {
  19. Out. println ("" + Rs. getstring (1 ));
  20. Out. println ("" + Rs. getstring (2 ));
  21. }
  22. }
  23. Catch (exception E)
  24. {
  25. Out. println ("some error ");
  26. Out. println ("e. getmessage ()");
  27. }
  28. %>
  29. </Body>
  30. </Html>

Run the command to check whether the operation is successful.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.