Instances connecting to databases using JSP

Source: Internet
Author: User

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. The Java program connects to the database through the JDBC driver and performs 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. The following describes how to use Access to implement a dynamic FAQ (FAQs and answers) website. First, create an Access database faq. mdb, where the table faqs has the field id (automatic incremental type, and set as the primary keyword), subject (text type, length 200), and answers (Remarks type ). This table can store some common programming knowledge questions and answers. Then, add the System DSN to the ODBC Datasource module of the Control Panel, name the faq, and point to faq. mdb. Create a JavaBean named faq. java and save it under the jswdk-1.0.1webpagesWEB-INFjspeans est directory. Faq. java:

Package test;
Import java. SQL .*;
Public class faq {
String sDBDriver = "sun. jdbc. odbc. JdbcOdbcDriver ";
String sConnStr = "jdbc: odbc: faq ";
Connection conn = null;
ResultSet rs = null;
Public faq (){
Try {
Class. forName (sDBDriver );
}
Catch (java. lang. ClassNotFoundException e ){
System. err. println ("faq ():" + e. getMessage ());
}
}
Public ResultSet executeQuery (String SQL ){
Rs = null;
Try {
Conn = DriverManager. getConnection (sConnStr );
Statement stmt = conn. createStatement ();
Rs = stmt.exe cuteQuery (SQL );
}
Catch (SQLException ex ){
System. err. println ("aq.exe cuteQuery:" + ex. getMessage ());
}
Return rs;
}

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.