|
|||||
|
Java Database Connectivity (JDBC) allows servlets to lookup data and generate dynamic HTML output on the fly. MMA includes the org.mm.mysql.Driver class which allows you to connect to and query tables in your MySQL database. If you want to try the following example with your account, you will need to create a table called "test" in your MySQL database. The definition for the "test" table can be seen here. import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
// MyServlet.java
public class MyServlet extends HttpServlet
{
public void doGet ( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
res.setContentType( "text/html");
PrintWriter out = res.getWriter();
out.println( "<html><body>" );
try {
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdbname",
"username","password");
stmt = con.createStatement();
rs = stmt.executeQuery("select * from test");
while (rs.next()) {
out.println("<LI> " + rs.getString("name") + " " +
In a similar manner, you could add a doPost() method for inserting data into the database through a JDBC query. For more information about accessing databases from Java, see the Java JDBC Tutorial.
Note: MMA technical support staff cannot provide troubleshooting of problems with third-party CGI scripts including but not limited to: Perl scripts, C/C++ binaries, PHP or web/database integration unless these services are specifically contracted. For these services, please see our Quote Request Form. |
|||||
|
||||||
Copyright
© 1995-2000
Motivational Marketing Associates, LLC
All Rights Reserved.