Copyright © RDSIndia.com,
All Rights Reserved
RDS SUPPORTHOMESERVER BASICSCONFIGURING FTP

RDS SUPPORTCONFIGURING E-MAILSTRANSFERRING DATABASETRANSFER TO RDS SERVERDOMAIN STATISTICSPOST YOUR QUERYSAMPLE SCRIPTS

Sample Scripts - 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.


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