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:
- <%!
- ConnectionConn=Null;
- StatementSt=Null;
- ResultSetRs=Null;
- Public void jspInit ()
- {
- Try
- {
- // Load the Driver Class
- Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
- // Connect to the database
- ConnectionConn=DriverManager. GetConnection ("jdbc: odbc: strurl ","
- Username "," password ");
- // Create a Statement object
- St=Conn. CreateStatement ();
- }
- Catch (Exception ex)
- {
- System. out. println (ex. toString ());
- }
- }
- Public void jspDestroy ()
- {
- Try
- {
- Rs. close ();
- St., close ();
- Conn. close ();
- }
- Catch (Exception ex)
- {
- System. out. println (ex. toString ());
- }
- }
- %>
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:
- Conn. java
- // Define the suite to which the bean belongs
- Package com. test;
- Import java. io .*;
- Import java. SQL .*;
- Import javax. servlet. http .*;
- Public class conn implements HttpSessionBindingListener
- {
- Private ConnectionCon=Null;
- Public conn () // complete the database link in the constructor
- {
- Bucket connection ();
- }
- Private void bucket connection ()
- {
- Try
- {
- // Load the driver
- Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
- }
- Catch (java. lang. ClassNotFoundException e1)
- {
- System. out. println ("failed to load the database driver<Br>");
- }
- Try
- {
- // Create a database link
- Con=DriverManager. GetConnection ("jdbc: odbc: test", "test", "test ");
- }
- Catch (SQLException e2)
- {
- System. out. println ("database connection failed ");
- }
- }
- // Obtain the Connection object
- Public Connection getConnection ()
- {
- If (Con= Null)
- Bucket connection ();
- Return this. con;
- }
- Public void close ()
- {
- Try
- {
- Con. close ();
- Con=Null;
- }
- Catch (SQLException sex)
- {
- System. out. println (sex. toString ());
- }
- }
- // This function is automatically executed when an object is added to a session.
- Public void valueBound (HttpSessionBindingEvent event ){}
- // This function is automatically executed when the session object is deleted.
- Public void valueUnbound (HttpSessionBindingEvent event)
- {
- If (con! = Null)
- Close (); // call the close Method
- }
- }
- JSP bean code optimization
- Detailed introduction to JSP environment configuration Scheme
- Use stored procedures in JSP JSTL
- Which of ASP. NET, JSP, and PHP is better?
- JSP-related software