Home | About Us | Services | Recent Work | Website Hosting | Contact Us | Tech-Support | Live Chat Support


MYSQL Database Connectivity using Servlets


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

Steps to compile the servlet 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());
}
}
}
 
Copyright © 2005-2007. Indian SEO All rights reserved.