I'm working on a JSP page with the following structure:
https://i.sstatic.net/3aFoz.png
In updating the table in my database, I've added a new username column that accepts any string of text. Once the 'update user' button is clicked, it should trigger the database update.
The database table has a unique column named AUSERNAME where the usernames are stored as seen in the screenshot. After clicking the update button, the goal is to pass the new username value and update the corresponding field in the table.
To achieve this, I understand that an AJAX call needs to be made within the JSP page. Below is the snippet of code for the AJAX call:
<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();
}
%>
Currently, my update.jsp page looks like this:
<% 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");
%>
My challenge lies in specifying the constraint on the current username itself for the update process. The current username values are fetched using a result set loop. For instance, I need to instruct the table to update the username to test
with the constraint being USER1.
Is there a way for me to retrieve the Username value in my update function?