我正在尝试使用tomcat服务器在netbeans中使用.jsp脚本在MySQL数据库中插入数据,但无法正常工作,不给出任何错误,仅在运行process.jsp时显示空白页
index.html
<html>
<body>
<form method="post" action="process.jsp">
First name:<br>
<input type="text" name="first_name">
<br>
Last name:<br>
<input type="text" name="last_name">
<br>
City name:<br>
<input type="text" name="city_name">
<br>
Email Id:<br>
<input type="email" name="email">
<br><br>
<input type="submit" value="submit">
</form>
</body>
</html>
process.jsp代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*,java.util.*"%>
<%
String first_name=request.getParameter("first_name");
String last_name=request.getParameter("last_name");
String city_name=request.getParameter("city_name");
String email=request.getParameter("email");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jform", "root", "");
Statement st=conn.createStatement();
int i=st.executeUpdate("insert into users(first_name,last_name,city_name,email)values('"+first_name+"','"+last_name+"','"+city_name+"','"+email+"')");
out.println("Data is successfully inserted!");
}
catch(Exception e)
{
System.out.print(e);
e.printStackTrace();
}
%>
感谢您的帮助
答案 0 :(得分:0)
似乎您的代码很好。您可能在netbeans / tomcat控制台中遇到错误。例如,如果在mysql表中有一列用于唯一键(例如“ id”)的列,请确保将其设置为autoincrement。还要确保在类路径中有mysql-connector-java jar文件。