Brief Introduction to JSP database links

Source: Internet
Author: User

Use the Jdbc-odbc bridge to connect, but this sacrifices the speed. If you use the Jdbc-odbc bridge, it is the same as connecting to access. You must first set a data source DNS and then use:
◆ Class. forName ("sun. Jdbc. odbc. JdbcOdbcDriver ");
◆ Connection conn = DriverManager. getConnection ("Jdbc: odbc: strurl ","
◆ Username "," password ");

It is time-consuming to link the database. If you frequently refresh the page, the database will be accessed continuously, greatly consuming the database resources. JSP provides such a function, JSP Init), JSP Destory). If you want to initialize some data at the beginning of execution on the JSP webpage, you can use the JSP Init function. This function will be called when the JSP page is executed and will not be executed again when the JSP page is reorganized. When the server is shut down, the JSP Destory function is called, which can be used to complete the aftermath of the data.

You can use JSP Init and JSP Destory functions to link and close databases. Connecting to the database in JSP Init can avoid linking to the database every time the page is refreshed, improving work efficiency.

The following is an example of code:

 
 
  1. <%!
  2. ConnectionConn=Null;
  3. StatementSt=Null;
  4. ResultSetRs=Null;
  5. Public void jspInit ()
  6. {
  7. Try
  8. {
  9. // Load the Driver Class
  10. Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
  11. // Connect to the database
  12. ConnectionConn=DriverManager. GetConnection ("jdbc: odbc: strurl ","
  13. Username "," password ");
  14. // Create a Statement object
  15. St=Conn. CreateStatement ();
  16. }
  17. Catch (Exception ex)
  18. {
  19. System. out. println (ex. toString ());
  20. }
  21. }
  22. Public void jspDestroy ()
  23. {
  24. Try
  25. {
  26. Rs. close ();
  27. St., close ();
  28. Conn. close ();
  29. }
  30. Catch (Exception ex)
  31. {
  32. System. out. println (ex. toString ());
  33. }
  34. }
  35. %> 

When a JSP page retrieves data from a database, the most time-consuming server is to establish a database link. Use JSP Init
And JSP Destory functions are not very good, after all, every time you browse a new page, you need to establish a database chain
. In this case, you can create a database link for an online user. Here we use the Bean object to create a number
Data Library Link.

The following is an example of code:

 
 
  1. Conn. java
  2. // Define the suite to which the bean belongs
  3. Package com. test;
  4. Import java. io .*;
  5. Import java. SQL .*;
  6. Import javax. servlet. http .*;
  7. Public class conn implements HttpSessionBindingListener
  8. {
  9. Private ConnectionCon=Null;
  10. Public conn () // complete the database link in the constructor
  11. {
  12. Bucket connection ();
  13. }
  14. Private void bucket connection ()
  15. {
  16. Try
  17. {
  18. // Load the driver
  19. Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
  20. }
  21. Catch (java. lang. ClassNotFoundException e1)
  22. {
  23. System. out. println ("failed to load the database driver<Br>");
  24. }
  25. Try
  26. {
  27. // Create a database link
  28. Con=DriverManager. GetConnection ("jdbc: odbc: test", "test", "test ");
  29. }
  30. Catch (SQLException e2)
  31. {
  32. System. out. println ("database connection failed ");
  33. }
  34. }
  35. // Obtain the Connection object
  36. Public Connection getConnection ()
  37. {
  38. If (Con= Null)
  39. Bucket connection ();
  40. Return this. con;
  41. }
  42. Public void close ()
  43. {
  44. Try
  45. {
  46. Con. close ();
  47. Con=Null;
  48. }
  49. Catch (SQLException sex)
  50. {
  51. System. out. println (sex. toString ());
  52. }
  53. }
  54. // This function is automatically executed when an object is added to a session.
  55. Public void valueBound (HttpSessionBindingEvent event ){}
  56. // This function is automatically executed when the session object is deleted.
  57. Public void valueUnbound (HttpSessionBindingEvent event)
  58. {
  59. If (con! = Null)
  60. Close (); // call the close Method
  61. }
  62. }
  1. JSP bean code optimization
  2. Detailed introduction to JSP environment configuration Scheme
  3. Use stored procedures in JSP JSTL
  4. Which of ASP. NET, JSP, and PHP is better?
  5. JSP-related software

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.