2013-08-25 138 views
0

(對不起,我有的代碼很多,但我真的不知道有多少)如何使用Java servlet將值插入到mysql數據庫?

我無法將值插入mysql數據庫。我正在使用Jsp文件而不是HTML文件。我不斷收到這個錯誤,我不確定是什麼原因造成的。

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. 
} 
} 

一個錯誤我不斷收到是 「不相容typesrequired:com.mysql.jdbc.Statement發現:java.sql.Statement中」,其中 「聲明= conn.createStatement();」 下是。

回答

1

看樣子你有import語句

import com.mysql.jdbc.Statement; 

代替

import java.sql.Statement; 

這已多次表示對這個網站,但你應該考慮使用PreparedStatement,而不是防止SQL Injectionattacks

+0

我通常會在詢問之前嘗試找到類似的問題。感謝您的額外鏈接。 –

1

你不應該需要投,所以不要。還要刪除mysql Statement類的import語句 - 你不需要這樣做。

相反,先簡單地使用對象而不引用它:

conn.createStatement().executeUpdate(query); 

一旦你得到的工作,才考慮持有到Statement對象的引用,如果你真的需要一個。

相關問題