我有一个JSP文件,它从HTML文件中获取表单并将它们发送到SQL数据库。这是文件:
attempt.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="attempt.jsp" method="post">
<input type="text" name="fname"></input> name
<input type="text" name="age"></input> age
<input type="submit"></input>
</form>
</body>
</html>
&#13;
attempt.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/DB","root","060901");
PreparedStatement st = con.prepareStatement("insert into tb values(?,?);");
st.setString(1,request.getParameter("fname"));
int a = Integer.parseInt(request.getParameter("age"));
st.setInt(2,a);
int num = st.executeUpdate();
ResultSet res = st.executeQuery("select * from tb;");
res.beforeFirst();
%>
<table>
<%
while(res.next()) {
%>
<tr>
<td><%=res.getString("name") %></td>
<td><%=res.getInt("age") %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
&#13;
我对服务器和数据库这个主题很陌生,所以我很难解决这个问题。 MySQL连接器jar已经在buildpath中了 已经尝试将其导入JSP页面。 有谁知道该怎么办?
提前致谢。