Accesing SQL DB from servlet ... Class error [modified]
-
I am trying to accessing SQL database from this given below code ... that i found on net. But there is problem at
Class.forName
It shows error at browser ... Error is "Exception: com.mysql.jdbc.Driver". I've also tried one another example roseindia.com ... same error. :( I've also tried another simple example like this from begining to class.form but again same error. :(
package DatabaseExample; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DatabaseExample extends HttpServlet { @Override public void doGet(HttpServletRequest hsReq, HttpServletResponse hsRes) throws ServletException, IOException { hsRes.setContentType("text/html"); PrintWriter objPrintWriter = hsRes.getWriter(); Connection con = null; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager.getConnection("jdbc:mysql:///test","root", "secret"); if(!con.isClosed()) objPrintWriter.println("Successfully connected to " + "MySQL server using TCP/IP..."); } catch(Exception e) { objPrintWriter.println("Exception: " + e.getMessage()); } finally { try { if(con != null) con.close(); } catch(SQLException e) {} } } }
modified on Monday, July 12, 2010 8:45 AM
-
I am trying to accessing SQL database from this given below code ... that i found on net. But there is problem at
Class.forName
It shows error at browser ... Error is "Exception: com.mysql.jdbc.Driver". I've also tried one another example roseindia.com ... same error. :( I've also tried another simple example like this from begining to class.form but again same error. :(
package DatabaseExample; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DatabaseExample extends HttpServlet { @Override public void doGet(HttpServletRequest hsReq, HttpServletResponse hsRes) throws ServletException, IOException { hsRes.setContentType("text/html"); PrintWriter objPrintWriter = hsRes.getWriter(); Connection con = null; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager.getConnection("jdbc:mysql:///test","root", "secret"); if(!con.isClosed()) objPrintWriter.println("Successfully connected to " + "MySQL server using TCP/IP..."); } catch(Exception e) { objPrintWriter.println("Exception: " + e.getMessage()); } finally { try { if(con != null) con.close(); } catch(SQLException e) {} } } }
modified on Monday, July 12, 2010 8:45 AM
khurram_shahzad wrote:
Error is "Exception: com.mysql.jdbc.Driver".
Is there nothing more in the exception message? Also the line
Class.forName("com.mysql.jdbc.Driver").newInstance();
makes no sense as you do not save the object created by
newInstance()
.It's time for a new signature.
-
khurram_shahzad wrote:
Error is "Exception: com.mysql.jdbc.Driver".
Is there nothing more in the exception message? Also the line
Class.forName("com.mysql.jdbc.Driver").newInstance();
makes no sense as you do not save the object created by
newInstance()
.It's time for a new signature.
No its just this little exception
("Exception: com.mysql.jdbc.Driver")
message only .... i think class may not found ...but i dodnt know why .. because in other example i include class library (import java.sql.DriverManager) but here's again same excaption message. I'vs this example also,import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/**
*
* @author KHURRAM SHAHZAD
*/
public class getDataServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest hsReq, HttpServletResponse hsRes) throws ServletException, IOException {
hsRes.setContentType("text/html");
PrintWriter objPrintWriter = hsRes.getWriter();String conURL = "jdbc:H:\\\\employee"; Connection connection=null; try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); /\*connection = DriverManager.getConnection(conURL); //i've comment out this area for testing/debuging Statement st = connection.createStatement(); ResultSet rs = st.executeQuery("Select \* from emp\_sal"); while(rs.next()){ objPrintWriter.println("EmpName" + " " + "EmpSalary" + "<br>"); objPrintWriter.println(rs.getString(1) + " " + rs.getString(2) + "<br>"); }\*/ } catch (Exception e){ objPrintWriter.println(e); } }
}
in this program it will show this message little change but seems.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
-
No its just this little exception
("Exception: com.mysql.jdbc.Driver")
message only .... i think class may not found ...but i dodnt know why .. because in other example i include class library (import java.sql.DriverManager) but here's again same excaption message. I'vs this example also,import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/**
*
* @author KHURRAM SHAHZAD
*/
public class getDataServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest hsReq, HttpServletResponse hsRes) throws ServletException, IOException {
hsRes.setContentType("text/html");
PrintWriter objPrintWriter = hsRes.getWriter();String conURL = "jdbc:H:\\\\employee"; Connection connection=null; try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); /\*connection = DriverManager.getConnection(conURL); //i've comment out this area for testing/debuging Statement st = connection.createStatement(); ResultSet rs = st.executeQuery("Select \* from emp\_sal"); while(rs.next()){ objPrintWriter.println("EmpName" + " " + "EmpSalary" + "<br>"); objPrintWriter.println(rs.getString(1) + " " + rs.getString(2) + "<br>"); }\*/ } catch (Exception e){ objPrintWriter.println(e); } }
}
in this program it will show this message little change but seems.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver