Sample scripts - MYSQL Database connectivity using servelets
 
           
Perl mail script  ASP mail script ASP Hit Counter Script  SQL connectivity  MySQL connectivity

 

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.

                             Sample Code

 

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());
}
}
}

 
| Buy domain names | Windows Hosting | Linux Hosting | Email | Acceptable Use Policy |       
Copyright © 2000 - 2005 websuvidha.com. All Rights Reserved.