How to access the database in JSP
JSP is a dynamic web page technical standard proposed by Sun and established by many companies. With JSP technology, Web page developers can use HTML or XML tags to design and format the final page. Use JSP Tags or Scriptlet to generate dynamic content on the page. The logic of the generated content is encapsulated in the identifiers and JavaBeans components and bundled in small scripts. All scripts run on the server.
Database Connection is the most important part for dynamic websites. The connection technology in Java is JDBC (Java Database Connectivity ). Many database systems have JDBC Drivers. Java programs connect to the database through the JDBC driver and perform operations such as querying and extracting data. Sun also developed JDBC-ODBC bridge, with this technology Java program can access the database with ODBC driver, currently most database systems with ODBC driver, therefore, Java programs can Access databases such as Oracle, Sybase, ms SQL Server, and MS Access. This article uses an example to introduce the use of JavaBeans in JSP to Access the customer information database through JDBC-ODBC Bridge.
1. first, create an Access database Customers. mdb, in which the table Customers has the field id (automatic incremental type, and set as the primary keyword), name (text type, length 10), address (text type, length 30), info (remark type ).
2. Add System DSN to the ODBC Datasource module of the Control Panel, name MERS, and point to Customers. mdb.
3. Create a JavaBeans named DBconn. java and save it in the default document root directory of the Web server that supports JSP. DBconn. java is mainly used to encapsulate connection operations with databases. The content is as follows:
Import java. SQL .*;
Public class DBconn {
String DBDriver = "sun. jdbc. odbc. JdbcOdbcDriver ";
String ConnStr = "jdbc: odbc: MERS ";
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 );
// Establish a connection with the DBMS
Statement stmt = conn. createStatement ();
Rs = stmt.exe cuteQuery (SQL );
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.