Access the ms SQL Server database in JSP

Source: Internet
Author: User
Tags how to use sql server how to use sql


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.

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.