У меня есть страница JSP, которая выглядит следующим образом:

Новый столбец имени пользователя позволяет мне вводить любую строку текста и когда я нажимаюКнопка обновления пользователя, она должна обновить таблицу в моей базе данных.
Моя таблица в структуре базы данных имеет уникальный столбец AUSERNAME, в котором хранится имя пользователя, как показано на скриншоте.После нажатия кнопки обновления он должен передать новое значение имени пользователя и обновить это поле в таблице.Это то, чего я пытаюсь достичь.
Я знаю, что мне нужно сделать вызов AJAX на моей странице JSP, которая выглядит следующим образом:
<script >
function update(param,param1) {
var newuser = document.getElementsByName('NewUserName' + param)[0].value;
var currentuser = document.getElementsByName(param1).value;
$.ajax({
type: "POST",
url: "update.jsp",
data: { name: newuser,
current: currentuser
},
success:function( msg ) {
alert( "Data Updated: " + msg );
},error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
</script>
<form method="get" action="migratefolder.jsp">
<%
try {
Class.forName("com.mysql.jdbc.Driver");
String connectionURL = "jdbc:mysql://localhost:3306/gmc";
String username = "root";
String password = "root";
Connection conn = null;
conn = DriverManager.getConnection(connectionURL, "root", "root");
String query = "select ausername, ausertype from auser where AUSTATE='Y'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
String str = "<table border=2><tr><th>Username</th><th>New Username</th><th>User Type</th><th>Update</th></tr>";
int rowid = 0;
String currentuser=null;
while (rs.next()) {
rowid ++;
currentuser=rs.getString("ausername");
String autype = rs.getString("ausertype");
str += "<tr><td><input readonly type=\"value\" name=\""+currentuser+"\" value="+rs.getString("ausername")+" </td><td><input type=\"text\" name=\"NewUserName"+rowid+"\""+
"value=\"\"></td> <td>";
if (autype.equalsIgnoreCase("Superuser")) {
str += "<input type=\"radio\" name=\"usertype"+rowid+"\" value=\"Superuser\" checked> Superuser ";
}else{
str += "<input type=\"radio\" name=\"usertype"+rowid+"\" value=\"Superuser\" > Superuser ";
}
if(autype.equalsIgnoreCase("Basic")) {
str += " <input type=\"radio\" name=\"usertype"+rowid+"\" value=\"Basic\" checked > Basic ";
}else {
str += " <input type=\"radio\" name=\"usertype"+rowid+"\" value=\"Basic\" > Basic ";
}
if(autype.equalsIgnoreCase("View")){
str += " <input type=\"radio\" name=\"usertype"+rowid+"\" value=\"View\" checked> View ";
}else {
str += " <input type=\"radio\" name=\"usertype"+rowid+"\" value=\"View\" > View ";
}
str += "</td><td><button type=\"button\" onclick=\"update("+rowid+","+currentuser+")\">Update User</button></td> </tr>";
}
out.println(str);
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
%>
Для моей страницы update.jsp до сих порвыглядит следующим образом:
<% response.setContentType("text/xml");%>
<%
String userid=request.getParameter("current");
String user=request.getParameter("name");
//database connection
try{
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection con=null;
ResultSet resultSet=null;
Statement stmt=null;
int row=0;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gmc", "root", "root");
String sql = "UPDATE `auser` SET `AUSERNAME`=? WHERE `AUSERNAME`=? ";
PreparedStatement statement = con.prepareStatement(sql);
statement.setString(1, user);
statement.setString(2, userid);
statement.executeUpdate();
}
catch (Exception ex) {
ex.printStackTrace();
}
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
%>
Проблема в том, что я могу передать значение нового имени пользователя в базу данных, но мне нужно указать ограничение на само текущее имя пользователя для обновления.Как показано в моем коде, значения имени пользователя извлекаются с использованием набора результатов.Например, в первой строке я должен указать таблице обновить имя пользователя до test
, где the constraint itself is USER1.
Можно ли каким-либо образом получить значение имени пользователя в обновлении моей функции?