The following describes how to use SQL Server to implement a dynamic FAQ (FAQs and answers) website.
First, create a database faq. The table faqs contains the field id (int, auto increment, and set as the primary keyword), subject (varchar, 200), and answers (text ). This table contains 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 the faq database. Create a JavaBean named faq. java and save it in the C: \ JBuilder4 \ tomcat \ webapps \ test 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;
}
}
After compiling faq. java using the method described in the previous section, create the JSP file faq. jsp In the jC: \ JBuilder4 \ tomcat \ webapps \ test directory. The content is as follows:
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> my FAQ! </Title>
</Head>
<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.exe cuteQuery ("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 ();
%>
In the address bar of the browser, type http: // localhost: 8080/test/faq. jsp. The faq. jsp calls JavaBean to read and output the content from the database. Expected result 5 is displayed.