我正好得到这个错误?在jsp,mysql和tomcat中使用'字段列表'中的未知列'国籍'?

时间:2014-01-28 04:20:26

标签: mysql jsp tomcat

我在jsp和mysql中创建了创建,插入,更新,删除,退出程序。我得到了插入,删除,退出和创建的解决方案,但是,更新部分只有我收到错误。在我的数据库名称中是员工,表名是员工,字段的顺序如1.员工姓名,2。employee_id ,3。国籍,。在更新部分i中默认显示使用Employee_id,它也显示但是我在3个国籍字段中出错。如果你发现我的错误,我会发送我的代码。提前致谢。编码如下:

<%@ page import="java.sql.*" %>
<%@ page import="java.sql.DriverManager.*" %>
<%@ page import="java.sql.Statement.*" %>
<%@ page import="java.io.*" %>

<html>
<head>
<title>
UPDATING DETAILS ABOUT EMPLOYEE
</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h3>ENTER THE EMPLOYEE DETAILS</h3><br>

<form action="editEmployee.jsp" name="UpdateForm" method="post">
<table cellspacing="1" cellpadding="1" border="1" WIDTH=90%>
<tr>
<td align="center">YOUR EMPLOYEE ID:</td><td align="center"><B><%= request.getParameter("emp_id")%></B></td>
</tr>
<tr>
<td align="center">EMPLOYEE NAME:</td>
<td align="center"><input type="text" name="EmployeeName"></td>
</tr>
<tr>
<td align="center"> EMPLOYEE NATIONLITY:</td>
<td align="center"><input type="text" name="Nationality"></td>
</tr>

<%
String EmployeeName=request.getParameter("EmployeeName");

String Nationality=request.getParameter("Nationality");

    PreparedStatement ps;
    Connection con=null;

    String driverName="com.mysql.jdbc.Driver";
    String connectionUrl="jdbc:mysql://localhost:3306/";
    String user="root";
    String password="sarakrish";
    String dbName="employee";
try{
    Class.forName(driverName);
    con=DriverManager.getConnection(connectionUrl+dbName,user,password);
    ps=con.prepareStatement("update employees set EmployeeName=?,Nationality=?");
    ps.setString(1, EmployeeName);
    ps.setString(2, Nationality);
    ps.executeUpdate();
    }
catch(Exception e)
    {
    out.print(e.getMessage());
    e.printStackTrace();
    }
%>


</table><br><br>
<input type="submit" value="UPDATE" name="method" class="button" >
<input type="reset" value="CLEAR">
<input type="button" name="method" value="BACK TO LIST" class="button" onclick="location.href='Employeelist.jsp'"/>
</form>

</body>
</html>         

2 个答案:

答案 0 :(得分:0)

嘿哥们感谢您的小谈话。我在我的程序中做了一些更改并获得了输出。,因为代码如下:

<%@ page import="java.sql.*" %>
<%@ page import="java.sql.DriverManager.*" %>
<%@ page import="java.sql.Statement.*" %>
<%@ page import="java.io.*" %>

<%

    PreparedStatement ps;
    Connection con=null;

    ResultSet rs=null;
    Statement st=null;


    String driverName="com.mysql.jdbc.Driver";
    String connectionUrl="jdbc:mysql://localhost:3306/";
    String user="root";
    String password="sarakrish";
    String dbName="employee";

    String emp_id = "";
    String emp_name = "";
    String nationality = "";
    try{
        Class.forName(driverName);
        con=DriverManager.getConnection(connectionUrl+dbName,user,password);

        if(request.getParameter("edit")!=null && request.getParameter("edit").toString().equals("yes")){
            String EmployeeId = request.getParameter("emp_id");
            String EmployeeName=request.getParameter("EmployeeName");
            String Nationality=request.getParameter("Nationality");

            ps=con.prepareStatement("update employees set EmployeeName=?,Nationlity=? where Employee_id=?");
            ps.setString(1, EmployeeName);
            ps.setString(2, Nationality);
            ps.setString(3, EmployeeId);
            ps.executeUpdate();
            ps.close();
        }
        st = con.createStatement();
        String sql="SELECT * FROM employees where employee_id='"+request.getParameter("emp_id")+"'";
        rs=st.executeQuery(sql);
        while (rs.next()) {
            emp_id = rs.getString(2);
            emp_name = rs.getString(1);
            nationality = rs.getString(3);
            break;
        }       
        rs.close();
        st.close();

        con.close();        
    }catch(Exception e){
        out.print(e.getMessage());
        e.printStackTrace();
    }

%>

<html>
<head>
<title>
UPDATING DETAILS ABOUT EMPLOYEE
</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h3>ENTER THE EMPLOYEE DETAILS</h3><br>
<%=request.getParameter("edit")%>
<form action="editEmployee.jsp" name="UpdateForm" method="post">
<input type="hidden" name="edit" id="edit" value="yes">
<input type="hidden" name="emp_id" id="emp_id" value="<%=emp_id %>">
<table cellspacing="1" cellpadding="1" border="1" WIDTH=90%>
<tr>
<td align="center">YOUR EMPLOYEE ID:</td><td align="center"><B><%= request.getParameter("emp_id")%></B></td>
</tr>
<tr>
<td align="center">EMPLOYEE NAME:</td>
<td align="center"><input type="text" name="EmployeeName" value="<%=emp_name %>"></td>
</tr>
<tr>
<td align="center"> EMPLOYEE NATIONLITY:</td>
<td align="center"><input type="text" name="Nationality" value="<%=nationality %>"></td>
</tr>



</table><br><br>
<input type="submit" value="UPDATE" name="method" class="button" >
<input type="reset" value="CLEAR">
<input type="button" name="method" value="BACK TO LIST" class="button" onclick="location.href='Employeelist.jsp'"/>
</form>

</body>
</html>         

答案 1 :(得分:-1)

假设这是字符串,因为您的表详细信息不清楚。将参数传递为String

ps=con.prepareStatement("update employees set EmployeeName=?,Nationality=? where employee_id=? ");
ps.setString(1,'EmployeeName');
ps.setString(2,'Nationality');
ps.setString(3,1);

同时避免使用scriptlet,请阅读Aniket建议的链接。

希望它有所帮助!!