Home Discussion Discuss: How to connect to MySql Database from Servlet?

  • How to connect to MySql Database from ServletNABEEL. AHMAD February 3, 2013 at 12:50 PM

    there is some error like file parsing error while compiling this file in netbeans..if u know any solution than plz let me know it______ import java.io.*; import java.util.*; import javax.sql.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class connection extends HttpServlet { @Override public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>Servlet JDBC</title></head>"); out.println("<body>"); out.println("<h1>Servlet JDBC</h1>"); out.println("</body></html>"); // connecting to database Connection con = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); con =DriverManager.getConnection ("jdbc:mysql://192.168.10.59:3306/example","root","root"); stmt = con.createStatement(); rs = stmt.executeQuery("SELECT * FROM servlet"); // displaying records while(rs.next()){ out.print(rs.getObject(1).toString()); out.print("\t\t\t"); out.print(rs.getObject(2).toString()); out.print("<br>"); } } catch (SQLException e) { throw new ServletException("Servlet Could not display records.", e); } catch (ClassNotFoundException e) { throw new ServletException("JDBC Driver not found.", e); } finally { try { if(rs != null) { rs.close(); rs = null; } if(stmt != null) { stmt.close(); stmt = null; } if(con != null) { con.close(); con = null; } } catch (SQLException e) {} } out.close(); } }

  • java servletankit October 20, 2011 at 11:46 AM

    want to know how to connect java servlet with mysql

  • Thanks dearjovian July 5, 2012 at 7:58 PM

    Thanks dear your code has helped me understand servlet to mysql database communication

  • mysql servletanurag November 22, 2012 at 4:49 PM

    very nice explanation of servlet, mysql interaction.

Post your Comment



Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Related Articles
JSP Servlet Questions

java servlets with database interaction
 

General

Using MYSQL Database with JSP & Servlets.
 

SQL Question

Mysql connect
 

Java Beginners

how to connect jsp to mysql - Java Beginners
 

JSP Servlet Questions

i can not connect to database in servlet - JSP-Servlet
 

JSP Servlet Questions

i can not connect to database in servlet - JSP-Servlet
 

JSP Interview Questions

connect to the database from JSP
 

Servlets

How to connect to MySql Database from Servlet?
 

Java Beginners

servlet7
 

JSP Tutorials

Connect JSP with mysql
 

Java Beginners

servlet3
 

Java Beginners

servlet4
 

Java Beginners

servlet5
 

JDBC Questions

MYSQL and SERVLETS - JDBC
 

JDBC Questions

how to connect mysql with JDBC - JDBC
 

Java Beginners

servlet2
 

Java Beginners

servlet6
 

Java Beginners

servlet6
 

General

Accessing Database from servlets through JDBC!
 

JDBC Questions

how to connect mySql drivers into eclipse(configuration)?
 

XML Questions

pls provide steps to connect mysql database in flex - XML
 

PHP Questions

mysql_connect arguments
 

PHP Questions

how to connect to database in php using mysql
 

JDBC Questions

how to connect mysql from gui?
 

JDBC Questions

TO connect multiple database
 

JDBC Questions

how can i create a mysql database to connect to this code - JDBC
 

JDBC Questions

how can i create a mysql database to connect to this code - JDBC
 

JDBC Questions

cannot connect to database - JDBC
 

JDBC Questions

Servlets mysql connectivity
 

Servlet Interview Questions

servlets
 

DMCA.com