A database connection is the most important part of a dynamic web site, and the technology for connecting to the database in Java is JDBC (Java Database Connectivity). Many database systems have JDBC drivers, and Java programs connect to databases through JDBC drivers, execute queries, extract data, and so on. Sun also developed JDBC-ODBC Bridge, which allows Java programs to access databases with ODBC drivers, most of which currently have ODBC drivers, so Java programs can access Oracle, Sybase, MS SQL Server and MS Access, and so on. Here's how to implement a Dynamic FAQ (FAQ and Answer) Web site with access.
First, create an Access database (Faq.mdb), where the Design tables (table) FAQs, with the field ID (AutoIncrement, and set as the primary key), subject (type, length), Answers (memo type). This table can contain some common questions and answers about programming knowledge. See Figure 4.
Then, in the ODBC DataSource module of Control Panel, add system DSN, name FAQ, and point to Faq.mdb.
Create a Javabean,faq.java and save it in the \jswdk-1.0.1\webpages\web-inf\jsp\beans\test directory. The contents of Faq.java are as follows:
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.executequery (SQL);
}
catch (SQLException ex) {
System.err.println ("Aq.executequery:" + ex.getmessage ());
}
Return RS;
}
}
After compiling Faq.java with the method described in the previous section, create the JSP file faq.jsp under the \jswdk-1.0.1\webpages\test directory, as follows:
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> My FAQ!</title>
<body>
<p><b> this is my faq!</b></p>.
<%@ page language= "java" import= "java.sql.*"%>
<jsp:usebean id= "workm" scope= "page" class= "Test.faq"/>
<%
ResultSet RS = Workm.executequery ("SELECT * from FAQs");
String tt;
while (Rs.next ()) {
tt = rs.getstring ("Answer");
Out.print ("<LI>" + rs.getstring ("Subject") + "</LI>");
Out.print ("<pre>" + TT + "</pre>");
}
Rs.close ();
%>
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