Accessing MS SQL Server database _jsp programming in a JSP

Source: Internet
Author: User
Tags getmessage odbc stmt tomcat

Here's how to implement a Dynamic FAQ (FAQ and Answer) Web site with SQL Server.
First, create a database FAQ, where the table FAQs has a field ID (int, autoincrement, and set as the primary key), subject (VARCHAR,200), Answers (text). This table can contain some common questions and answers about programming knowledge. Then, add system DSN to the ODBC DataSource module in 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.
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 a JSP file faq.jsp under the Jc:\jbuilder4\tomcat\webapps\test directory, which reads 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 ();
%>
Type http://localhost:8080/test/faq.jsp,faq.jsp call JavaBean in the address bar of the browser, read the content from the database and output, and get the results as shown in Figure 5.
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.