How to connect servlets with mysql?.Servlet program to connect to mysql database using jdbc Driver in Eclipse.
If you have problem in executing servlet code, see this tutorial.
Import external jar files
Hope you already configured webserver (I ve tomcat7) which is needed to execute Dynamic web projects ....if not go here .
Related post :
servlet program to get html form data
Servlet with doget method
If you have problem in executing servlet code, see this tutorial.
Import external jar files
- javax.servlet-api-3.0.1.jar
- mysql connector / J
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.*;
import java.sql.*;
@WebServlet("/dbconnect")
public class dbconnect extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String URL = "jdbc:mysql://localhost:3306/mysql";
Connection connection = null;
// Load JBBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(URL, "root", "v");
if(!connection.isClosed())
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
else
out.println("No connection");
}
catch(Exception excp)
{
excp.printStackTrace();
}
}}
Hope you already configured webserver (I ve tomcat7) which is needed to execute Dynamic web projects ....if not go here .
Related post :
servlet program to get html form data
Servlet with doget method
0 comments:
Post a Comment