In JSP database access methods

  Abstract: In the JSP methods of accessing database 

  In JSP database access methods 

  JSP (JavaServer Pages) is from the Sun's advocacy, many companies involved in the establishment of a dynamic web page technical standards.    Use JSP technology, Web page developers can use HTML or XML format and logo design to the final pages.    Use JSP logo (tag) or small script (Scriptlet) to generate dynamic content on the page.    Content generated by the logic of the logo and packaging JavaBeans components, and bundled in a small script, all of the server-side script in the operation. 

  Dynamic Database Connection website is the most important part of the Java technology is connecting to the database JDBC (Java Database Connectivity).    Many database system with JDBC driver, a Java program through JDBC Driver connected with the database, execute a query, extract data, and other operations.    Sun Microsystems has developed a JDBC-ODBC bridge, using this technology Java programs can access ODBC drivers with the database, at present, most database systems come with ODBC drivers, Java programs can visit such as Oracle, Sybase, MS SQL Server and MS Access databases such as.    This paper will be introduced in JSP an example of the use of JavaBeans through JDBC-ODBC Bridge Access to customer information databases. 

  1. First establish a Customers.mdb Access database, which has fields Customers table id (automatic incremental type, as well as the main keyword), the name (for the text, type, length 10), address (for the text, type, length 30), info (Note type). 

  2. Control Panel (control panel) ODBC Datasource join the System DSN module, named Customers, and pointed Customers.mdb. 

  3. Create a JavaBeans named DBconn.java, and the preservation in support of JSP Web server default document root directory.    DBconn.java Packaging is the main link with the database operation, as follows: 

  Import java.sql .*; 

  (Public class DBconn 

  String DBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; 

  String ConnStr = "jdbc: odbc: Customers"; 

  Connection conn = null; 

  ResultSet rs = null; 

  (Public DBconn 

  Try ( 

  Class.forName (DBDriver); 

  / / Load the database driver 

  ) 

  Catch (java.lang.ClassNotFoundException e) ( 

  System.err.println ( "DBconn ():" + e.getMessage ()); 

  ) 

  ) 

  Public ResultSet executeQuery (String sql) ( 

  Rs = null; 

  Try ( 

  Conn = DriverManager.getConnection (ConnStr); 

  / / Connect with the DBMS 

  Statement stmt = conn.createStatement (); 

  Rs = stmt.executeQuery (sql); 

  ) 

  Catch (SQLException ex) ( 

  System.err.println ( "aq.executeQuery:" + ex.getMessage ()); 

  ) 

  Return rs; 

  ) 

  ) 

  4.DBconn.java good editor, in the DOS mode, then use the JDK javac compiler DBconn.java ordered the formation of the corresponding class document. 

  5. Establish Customers.jsp documents in the above call in JSP compiler good JavaBeans, which reads as follows: 

  <html> 

  <head> 

  <meta Http-equiv="Content-Type" content="text/html; charset=gb2312"> 

  <title> Customer information survey </ title> 

  </ Head> 

  <body> 

  <p> <b> Customer information survey </ b> </ p> 

  <% @ Page language = "java" import = "java.sql .*"%> 

  <jsp:useBean Id="DBconn1" scope="page" class="DBconn" /> 

  <% 

  ResultSet RS = DBconn1.executeQuery ( "SELECT * FROM Customers"); 

  While (RS.next ()) ( 

  Out.print ( "<LI>" + RS.getString ( "name") + "</ LI>"); 

  Out.print ( "<LI>" + RS.getString ( "address") + "</ LI>"); 

  Out.print ( "<LI>" + RS.getString ( "info") + "</ LI>"); 

  ) 

  RS.close (); 

  %> 

  </ Body> 

  </ Html> 

  In "jsp: useBean> marker in the definition of several attributes, which is the id of the JSP pages Bean logo, the definition of the scope Bean attributes the survival time, the class attribute of the Bean class files. 

  Facts have proved that the JSP is an ideal Web application development framework, the use of cross-platform runtime components JavaBeans, JSP for separation processing logic and display modes provide an excellent solution. 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply