我想将我的文本框值(“ 20”)插入数据库。我该怎么做 ?而且,如果用户从未在文本框中输入任何值,它将通知用户。
我已经创建了一个js文件,但是它不起作用...
assign.js
document.getElementById("assignBtn").onclick = function () {
var errmessage = "";
var na = document.forms["assignTo"]["Number"].value;
if (na == "") {
//if(document.getElementById("ExerciseArm").value=="") {
errmessage += "*Please enter the number of time for arm<br>";
}
var nl = document.forms["assignTo"]["Number"].value;
if (nl == "") {
//if(document.getElementById("ExerciseTime1").value=="") {
errmessage += "*Please enter the number of time for leg <br>";
}
if (na != "" && nl == "") {
document.assignTo.submit();
alert('Exercise has been assigned succesfully.');
} else if (na == "" && nl != "") {
document.assignTo.submit();
alert('Exercise has been assigned succesfully.');
} else if (na != "" && nl != "") {
document.assignTo.submit();
alert('Exercise has been assigned succesfully.');
} else if (errmessage.length > 0) {
errmessage = errmessage.fontcolor("red");
errmessage = errmessage.fontsize("2");
document.getElementById("divMessage").innerHTML = errmessage;
}
}
assignTo.jsp
String[] id = request.getParameterValues("assignchkbox");
String armID = request.getParameter("ArmID");
String number = request.getParameter("Number");
int count = 0;
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
// Step 2: Define Connection URL
String connURL = "jdbc:mysql://localhost/medicloud?user=root&password=root";
// Step 3: Establish connection to URL
conn = DriverManager.getConnection(connURL);
if (id != null) {
for (int i = 0; i < id.length; i++) {
String sqlStr = "Insert into assign(ArmID,Number) Value(?,?)";
PreparedStatement pstmt = conn.prepareStatement(sqlStr);
pstmt.setInt(1, Integer.parseInt(id[i]));
pstmt.setString(2, number);
//pstmt.setInt(1, Integer.parseInt(ids[i]));
int rec = pstmt.executeUpdate();
if (rec == 1) {
count++;
response.sendRedirect("assign.jsp");
}
}
}
}
忽略所有jsp文件夹中的ID(它们按照我想要的方式工作)。现在唯一的问题是,文本框值未插入数据库,并且Idk错误在哪里
答案 0 :(得分:0)
请尝试您的jsp文件是否接受来自表单的所需值:
String username = "<your username>";
String pass = "<your pass>";
Class.forName("com.mysql.jdbc.Driver");
String connURL = "jdbc:mysql://localhost:<port>/medicloud";
conn = DriverManager.getConnection(connURL,username,pass);
代替您的代码。 在某些情况下,您必须将mysqljdbc.jar导入WEB-INF / lib,我不太清楚该路径,请在您的项目中检查它。
别忘了证明您的数据库实例端口,在mysql中应该是3306。
您的代码根本不干净,您应该捕获异常以获取更多调试所需的信息。