Check Domain Name


MYSQL Database Connectivity using Servelets

The following sample servelet script code that allows you connect to MYSQL Database

Steps to compile the servelet code for MYSQL database connectivity :

Save the code given below as . java file with relevant changes, viz., username,
password and web host address.

Compile the saved .java file and upload the class file to the host server.

import java.io.IOException;
import java.io.PrintStream;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DbTest extends HttpServlet
{
Connection connection =null;
public void service(HttpServletRequest httpservletrequest,
HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
httpservletresponse.setContentType("text/html");
ServletOutputStream servletoutputstream =
httpservletresponse.getOutputStream();
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
connection =

DriverManager.getConnection("jdbc:mysql://localhost/bharatdarpan?user=bharat
password=sqldarpan");
Statement statement = connection.createStatement();
String s2 = "SELECT * FROM user_profile";
ResultSet resultset = statement.executeQuery(s2);
String s3 = null;
String s4 = null;
while (resultset.next())
{
s3 = resultset.getString(1);
s4 = resultset.getString(2);
servletoutputstream.println("1: "+s3);
servletoutputstream.println("2: "+s4);
}
}
catch(Exception pp){
servletoutputstream.println("Exception ");
servletoutputstream.println(pp.toString());
}
}
}

Visit Indian SEO for Search Engine Optimization Website
Maintenance Services Bangalore India
Visit indianattire.com for Indian attire
Copy Right 2001 - 2005 All Rights Reserved Terms and Conditions