我有一个看起来像这样的JSP页面:
新的用户名列允许我输入任何字符串,当我点击“更新用户”按钮时,它将更新数据库中的表。
我的数据库结构表中有一个唯一的列AUSERNAME,用于存储用户名,如屏幕截图所示。单击更新按钮后,它将传递新的用户名值并更新表中的该字段。这就是我要实现的目标。
我知道我必须在我的JSP页面中进行如下所示的AJAX调用:
<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.
有什么方法可以在函数更新中获取Username值吗?
答案 0 :(得分:0)
更新功能需要微小的更改才能传递值。
您需要像这样通过javascript函数传递值。
<button onclick=update('abc','bcd')></button>
检查以下代码。
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<script>
function update(param, param1) {
var newuser = document.getElementsByName('NewUserName' + param)[0].value;
var currentuser = param1;
$.ajax({
type : "POST",
url : "update.jsp",
data : {
name : newuser,
current : currentuser
},
success : function(msg) {
alert("Data Updated: " + msg);
location.reload();
},
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/test";
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();
}
%>