js| Data | database | database connection
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 the database through the JDBC driver, executing queries, fetching data, and so on. Sun also developed JDBC-ODBC Bridge, which uses Java programs to access databases with ODBC drivers, and most database systems now have ODBC drivers, so Java programs can access Oracle, Sybase, MS SQL Databases such as server and MS Access. Here's how to implement a Dynamic FAQ (FAQ and Answer) Web site with access. First, create an Access database Faq.mdb, where the table FAQs has a field ID (AutoIncrement, and set as the primary key), subject (type, length 200), Answers (memo type). This table can contain some common questions and answers about programming knowledge, and then add System DSN, name FAQ, and point to Faq.mdb in the ODBC DataSource module of Control Panel. Create a JavaBean, named Faq.java, and save it in the Jswdk-1.0.1webpagesweb-infjspeans est 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;
}