【问题标题】:Not able to insert data into mysql by using jsp无法使用jsp将数据插入mysql
【发布时间】:2014-07-07 02:05:09
【问题描述】:

您好,我正在尝试将数据插入 mysql,但无法插入。这是我的 mysql 数据库

。数据库没有问题。

这是我的 2 个 jsp 文件的代码,一个是显示页面的 register.jsp,另一个是 insert.jsp,它用于插入数据背后的逻辑。

Register.jsp

   <html>
<form method="post" action="insert.jsp">
<table>
<tr><td>First Name:</td><td><input type="text" name="fname"></td></tr>
<tr><td>Last Name:</td><td><input type="text" name="lname"></td></tr>
<tr><td>Email:</td><td><input type="text" name="email"></td></tr>
<tr><td>Confirm Password:</td><td><input type="password" name="cpass"></td></tr>

<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</form>
</html>

插入.jsp

  <%@page import="java.sql.*,java.util.*"%>

<%
String fname=request.getParameter("fname");
String lname=request.getParameter("lname");
String email=request.getParameter("email");
String cpass=request.getParameter("cpass");

        try{
         Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/roseindia", "root", "");
           Statement st=con.createStatement();
           int i=st.executeUpdate("insert into student(firstname,lastname,email,confirm_pass,) values('"+fname+"','"+lname+"','"+email+"','"+cpass+"',')");
        System.out.println("Data is successfully inserted!");
        }
        catch(Exception e){
        System.out.print(e);
        e.printStackTrace();
        }
        %>

这段代码有什么问题?

【问题讨论】:

  • 为什么不使用 servlet 而不是 insert.jsp 并在执行前打印查询以避免错误
  • 你有什么异常吗?
  • 你在哪里使用ajax这里?
  • 你有什么错误吗?

标签: java mysql ajax jsp


【解决方案1】:

最后一个逗号有语法错误。尽量去掉最后一个逗号

int i=st.executeUpdate("insert into student(firstname,lastname,email,confirm_pass) values('"+fname+"','"+lname+"','"+email+"','"+cpass+")");

【讨论】:

    【解决方案2】:

    你在插入查询中有错误从最后删除它,'来自

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多