Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes HTML-Servlet-MYSQL problem ? help needed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HTML-Servlet-MYSQL problem ? help needed?" Watch "HTML-Servlet-MYSQL problem ? help needed?" New topic
Author

HTML-Servlet-MYSQL problem ? help needed?

Raghav
Greenhorn

Joined: Jul 24, 2001
Posts: 14
Hi,
I am trying to insert data from html page through post operation into a MYSQL database through a servelt, every time i enter a value in the html page and submit , i get a IllegalAccessException , can any body help me , i have attached the code below and the error below ,
servlet code:
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
class insert extends HttpServlet
{
Connection conn=null;
Statement st=null;
ResultSet rs=null;
PreparedStatement pst=null;
public void init(ServletConfig cfg) throws ServletException
{
try
{
super.init(cfg);
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/mysql?user=root&password=******","root","******");

}
catch(SQLException se)
{
}
catch(ClassNotFoundException ce)
{
}
}
public void service(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
doPost(req,res);
}
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
try
{
String aged = req.getParameter("age");
String query = "insert into rag(age) values(?)";
pst=conn.prepareStatement(query);
pst.setString(1,aged);
pst.executeUpdate();
PrintWriter pw = res.getWriter();
pw.println("done");
pw.close();
}
/*catch(Exception e)
{
System.out.println("class not found");
//e.printStackTrace();
}*/
catch (SQLException se) {
System.out.println("SQLException: " + se.getMessage());
System.out.println("SQLState: " + se.getSQLState());
System.out.println("VendorError: " + se.getErrorCode());
}
}
public void destroy()
{
try
{
rs.close();
conn.close();
}
catch(Exception e){}
}
}
error when invoked from a html page:
Error: 500
Internal Servlet Error:
java.lang.IllegalAccessException: insert
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Unknown Source)
at com.sun.web.core.ServletWrapper.loadServlet(ServletWrapper.java:90)
at com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:109)
at com.sun.web.core.InvokerServlet.service(InvokerServlet.java:169)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:840)
at com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:140)
at com.sun.web.core.Context.handleRequest(Context.java:375)
at com.sun.web.server.ConnectionHandler.run(ConnectionHandler.java:135)
thanks
raghav
Vlad Patryshev
Ranch Hand

Joined: Jun 30, 2001
Posts: 61
I would at least try to catch this exception in init(); while it is kind of obvious that the exception is thrown when trying to load the JDBC driver for mySQL, it is better to make sure that this is the case.


Thanks,<br />Vlad
 
 
subject: HTML-Servlet-MYSQL problem ? help needed?
 
Similar Threads
How to store and retrieve images to MS access and MYsql database
to enter Date in Oracle from Servlets
error in connectivity with database
JavaMail and Servlets
error in connectivity with database