Saturday, 24 August 2013

How to insert values into to mysql DataBase using Java servlet?

How to insert values into to mysql DataBase using Java servlet?

(Sorry for the amoount Of code I have, but I really don't know how much is
enough)
I'm having trouble inserting values into the mysql database. I'm using Jsp
files instead of html files. I keep getting this error and I'm not sure
what's causing it.
public class AddTo extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException,
SQLException {
response.setContentType("text/html;charset=UTF-8");
{
String name = request.getParameter("name");
String dbURL = "jdbc:mysql://localhost:3306/movieDB";
String username = "root";
String password = "sund ";
try {
Connection conn = (Connection) DriverManager.getConnection(
dbURL, username, password);
}
catch (SQLException ex) {
Logger.getLogger(AddTo.class.getName()).log(Level.SEVERE,
null, ex);
}
String query = "INSERT INTO table1 " + "VALUES ('" + name + "')";
Statement statement = null;
statement = (Statement) conn.createStatement();
statement.executeUpdate(query);
Movie aMovie = new Movie(request.getParameter("name"));
request.setAttribute("user", aMovie);
String cartRadio = request.getParameter("cartRadio");
if ( cartRadio.equalsIgnoreCase("cartSelect") ) {
String url = "/jsp2.jsp";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request,response);
}
if ( cartRadio.equalsIgnoreCase("listSelect")) {
String url = "/jsp3.jsp";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request,response);
}
if ( cartRadio.equalsIgnoreCase("none")) {
String url = "/jsp4.jsp";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request,response);
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(AddTo.class.getName()).log(Level.SEVERE, null, ex);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(AddTo.class.getName()).log(Level.SEVERE, null, ex);
}
}
private Object getServletContext() {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}
}
One error I keep getting is "incompatible typesrequired:
com.mysql.jdbc.Statement found: java.sql.Statement" under where "statement
= conn.createStatement();" is.

No comments:

Post a Comment