记录未通过JSP MySQL添加到数据库中。

huangapple 未分类评论54阅读模式
英文:

Record is not Added in to the databse using Jsp Mysql

问题

以下是您提供的内容的翻译:

表单设计:

<div class="form-group" align="left">
    <label class="form-label">学生姓名</label>
    <input type="text" class="form-control" placeholder="学生姓名" id="stname" name="stname" size="30px" required>
</div>
<div class="form-group" align="left">
    <label class="form-label">课程</label>
    <input type="text" class="form-control" placeholder="课程" id="course" name="course" size="30px" required>
</div>
<div class="form-group" align="left">
    <label class="form-label">费用</label>
    <input type="text" class="form-control" placeholder="费用" id="fee" name="fee" size="30px" required>
</div>
<div class="card" align="right">
    <button type="button" class="btn btn-info" id="save" onclick="addProject()">注册</button>
    <button type="button" class="btn btn-warning" id="clear" onclick="clear()">关闭</button>
</div>

Student.java:

public class Student {
    private String stname;
    private String course;
    private int fee;

    public Student(String stname, String course, int fee) {
        this.stname = stname;
        this.course = course;
        this.fee = fee;
    }

    public String getStname() {
        return stname;
    }

    public void setStname(String stname) {
        this.stname = stname;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

    public int getFee() {
        return fee;
    }

    public void setFee(int fee) {
        this.fee = fee;
    }
}

StudentDAO.java:

public class StudentDAO {
    Connection connection;
    PreparedStatement pst;

    public StudentDAO() {
        String jdbcURL = "jdbc:mysql://localhost/studcrud";
        String dbUser = "root";
        String dbPassword = "";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(jdbcURL, dbUser, dbPassword);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public int insertUser(Student stud) throws SQLException, ClassNotFoundException {
        String sql = "insert into records(name,course,fee)values(?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, stud.getStname());
        statement.setString(2, stud.getCourse());
        statement.setInt(3, stud.getFee());
        int row = statement.executeUpdate();
        return row;
    }
}

Add.jsp:

<%@page import="org.json.simple.JSONArray"%>
<%@page import="org.json.simple.JSONObject"%>
<%@page import="java.sql.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%
    String stname = request.getParameter("stname");
    String course = request.getParameter("course");
    int fee = Integer.parseInt(request.getParameter("fee"));

    // 填充到学生 Bean 中
    Student stud = new Student(stname, course, fee);

    StudentDAO stu = new StudentDAO();
    int rows = stu.insertUser(stud);
    String message;

    if (rows == 0) {
        message = "成功";
    } else {
        message = "失败";
    }
%>

请注意,以上内容为您提供的代码的中文翻译,仅包括翻译后的代码部分,不包括额外的信息。

英文:

i am creating a simple crud using jsp mysql but record is not added in to the database.i tried this for 3 days but i couldn't solve the problem.i don't know what was a problem what i tried so far i attached below. Form design and Student.java class consist of all data and StudentDAO database is there add.jsp add the data in to the database.

Form

&lt;div class=&quot;form-group&quot; align=&quot;left&quot;&gt;
                    &lt;label class=&quot;form-label&quot;&gt;Student Name&lt;/label&gt;
                    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; placeholder=&quot;Student Name&quot; id=&quot;stname&quot; name=&quot;stname&quot; size=&quot;30px&quot; required&gt;
                &lt;/div&gt;
                &lt;div class=&quot;form-group&quot; align=&quot;left&quot;&gt;
                    &lt;label class=&quot;form-label&quot;&gt;Course&lt;/label&gt;
                    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; placeholder=&quot;Course&quot; id=&quot;course&quot; name=&quot;course&quot; size=&quot;30px&quot; required&gt;
                &lt;/div&gt;
                &lt;div class=&quot;form-group&quot; align=&quot;left&quot;&gt;
                    &lt;label class=&quot;form-label&quot;&gt;Fee&lt;/label&gt;
                    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; placeholder=&quot;Fee&quot; id=&quot;fee&quot; name=&quot;fee&quot; size=&quot;30px&quot; required&gt;
                &lt;/div&gt;

                  &lt;div class=&quot;card&quot; align=&quot;right&quot;&gt;
                    &lt;button type=&quot;button&quot; class=&quot; btn btn-info&quot; id=&quot;save&quot; onclick=&quot;addProject()&quot;&gt;Registation
                    &lt;/button&gt;
                    &lt;button type=&quot;button&quot; class=&quot;btn btn-warning&quot; id=&quot;clear&quot;   onclick=&quot;clear()&quot;&gt;Close&lt;/button&gt;
                &lt;/div&gt;

Student.java

 public class Student {
        
        private String stname;
         private String course;
         private int fee;
    
        public Student(String stname, String course, int fee) {
            this.stname = stname;
            this.course = course;
            this.fee = fee;
        }
    
        public String getStname() {
            return stname;
        }
    
        public void setStname(String stname) {
            this.stname = stname;
        }
    
        public String getCourse() {
            return course;
        }
    
        public void setCourse(String course) {
            this.course = course;
        }
    
        public int getFee() {
            return fee;
        }
    
        public void setFee(int fee) {
            this.fee = fee;
        }

StudentDAO.java

public class StudentDAO {
    
    Connection connection;
    PreparedStatement pst;
    
	public StudentDAO() {
		String jdbcURL = &quot;jdbc:mysql://localhost/studcrud&quot;;
		String dbUser = &quot;root&quot;;
		String dbPassword = &quot;&quot;;
		try {
			Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
			connection = DriverManager.getConnection(jdbcURL, dbUser, dbPassword);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}
public int insertUser(Student stud) throws SQLException, 
	    ClassNotFoundException {

		String sql = &quot;insert into records(name,course,fee)values(?,?,?)&quot;;
		PreparedStatement statement = connection.prepareStatement(sql);
		statement.setString(1, stud.getStname());
		statement.setString(2, stud.getCourse());
                statement.setInt(3, stud.getFee());
               
                 int row=  statement.executeUpdate();
                 return row;
	}

Add.jsp

&lt;%@page import=&quot;org.json.simple.JSONArray&quot;%&gt;
&lt;%@page import=&quot;org.json.simple.JSONObject&quot;%&gt;
&lt;%@page import=&quot;java.sql.*&quot; %&gt;
&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;


        &lt;%
 
       String stname = request.getParameter(&quot;stname&quot;);
       String course = request.getParameter(&quot;course&quot;);
       int  fee = Integer.parseInt(request.getParameter(&quot;fee&quot;));
       
       //fill it up in a Student Bean
       Student stud = new Student(stname,course,fee);
    
       
       StudentDAO stu = new StudentDAO();
       int rows = stu.insertUser(stud); 
        
      String message;
      
      
      if(rows==0)
      {
           message = &quot;success&quot;;
      
      }
      else
      {
          message = &quot;Failed&quot;;
      }

huangapple
  • 本文由 发表于 2020年3月15日 18:42:55
  • 转载请务必保留本文链接:https://java.coder-hub.com/60692028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定