简体   繁体   English

JSP表单处理

[英]JSP form handling

I am currently making a jsp project called phonebook online. 我目前正在制作一个名为电话簿的jsp在线项目。 Its a phonebook on a browser. 它是浏览器上的电话簿。 I'm still a beginner . 我还是一个初学者。 I am having a slight difficulty adding the users input to my sql database. 我在将用户输入添加到我的sql数据库时遇到了一些困难。 when i press the button to add the users input- null -shows to my database as firstname and lastname instead of the input of the user. 当我按下按钮添加用户输入时,null-显示为我的数据库的名字和姓氏,而不是用户的输入。

This are some of the line code for the input field from my Welcomeform.jsp file. 这是我的Welcomeform.jsp文件中输入字段的一些行代码。

<td>First Name:</td>                            
<td><input type = "text" name = "fname" id="name">
</td>
<td>Last Name:</td>             
<td><input type = "text" name ="lname"  id ="name"></td>

This is the entire code in my adduser.jsp file. 这是我的adduser.jsp文件中的完整代码。

<%@page language="java"%>
<%@page import="java.sql.*"%>
<%
    String firstname = request.getParameter("fname");
    String lastname = request.getParameter("lname");
    String phoneno = request.getParameter("phonenumber");

    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/phonebook", "**", "**");

        Statement st = conn.createStatement();

        st.executeUpdate("insert into  contaclist(firstname,lastname) values('"
                + firstname + "','" + lastname + "')");
        out.println("Record is added successfully");
    } catch (Exception e) {
        out.println("error adding");
    }
%>

Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/phonebook", " ", " "); 连接conn = DriverManager.getConnection(“ jdbc:mysql:// localhost:3306 / phonebook”,“ ”,“ ”);

Please make sure that you have right access to the database you can use 请确保您有权使用可以使用的数据库

System.out.println(conn) ;

to check your connection 检查您的连接

ResultSet rs ;
Statement st = conn.createStatement("insert into  contaclist(firstname,lastname) values("?,?")");
st.setString(1,firstname);
st.setString(2,lastname);
int k= st.executeUpdate();
System.out.println(k);

If the output is 1 then record is inserted sucessfully .. 如果输出为1,则成功插入记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM