I am trying to add or insert values into table of database using servlet and jsp and MySQL Workbench as database. These are the following details:
我试图使用servlet和jsp以及MySQL Workbench作为数据库在数据库表中添加或插入值。这些是以下细节:
1.> Register.java
1.> Register.java
package register.com;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import javax.servlet.*;
/**
* Servlet implementation class Register
*/
@WebServlet("/register")
public class Register extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Register() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
//String connectionURL = "jdbc:mysql://127.0.0.1:3306/newData";// newData is the database
//Connection connection;
Connection conn=null;
String url="jdbc:mysql://localhost:3306/";
String dbName="userlogindb";
String driver="com.mysql.jdbc.Driver";
//String dbUserName="root";
//String dbPassword="root";
try{
String Fname = request.getParameter("fname");
String Mname = request.getParameter("mname");
String Lname = request.getParameter("lname");
String Uname = request.getParameter("username");
String Emailid = request.getParameter("emailid");
String Mobno = request.getParameter("mobno");
String Address = request.getParameter("address");
String Password1 = request.getParameter("password1");
String Password2 = request.getParameter("password2");
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,"root", "root");
PreparedStatement pst =(PreparedStatement) conn.prepareStatement("insert into 'userlogindb'.'registerutable'(fname,mname,lname,username,emailid,mobno,address,password1,password2) values(?,?,?,?,?,?,?,?,?)");//try2 is the name of the table
pst.setString(1,Fname);
pst.setString(2,Mname);
pst.setString(3,Lname);
pst.setString(4,Uname);
pst.setString(5,Emailid);
pst.setString(6,Mobno);
pst.setString(7,Address);
pst.setString(8,Password1);
pst.setString(9,Password2);
int i = pst.executeUpdate();
conn.commit();
String msg=" ";
if(i!=0){
msg="Record has been inserted";
pw.println("<font size='6' color=blue>" + msg + "</font>");
}
else{
msg="failed to insert the data";
pw.println("<font size='6' color=blue>" + msg + "</font>");
}
pst.close();
}
catch (Exception e){
pw.println(e);
}
}
}
2.> index.jsp
2.> index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="registrationform" action="register" method="post">
<p>
Enter your first name: <input type="text" name="fname"><br>
Enter your middle name: <input type="text" name="mname"><br>
Enter your last name: <input type="text" name="lname"><br>
</p><br>
<p>
Enter username: <input type="text" name="username"><br>
</p><br>
<p>
Enter email id: <input type="text" name="emailid"><br>
Enter mobile number: <input type="text" name="mobno"><br>
Enter address: <textarea rows="5" cols="5" name="address"></textarea><br>
</p><br>
<p>
Enter the password: <input type="password" name="password1"><br>
Reenter the password: <input type="password" name="password2"><br>
</p><br>
<p>
<input type="submit">
</p><br>
</form>
</body>
</html>
3.> web.xml
3.> web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>RegisterExample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>Register Servlet</description>
<display-name>Register</display-name>
<servlet-name>Register</servlet-name>
<servlet-class>register.com.Register</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
</web-app>
4.> I have added mysql-connector-java-5.0.8-bin.jar and servlet-api-3.0.jar is anything else needed to add it. I have created a connection to database in Data Source Explorer.
4.>我添加了mysql-connector-java-5.0.8-bin.jar和servlet-api-3.0.jar是添加它所需的任何东西。我在Data Source Explorer中创建了与数据库的连接。
When I m compiling or debuging it shows no error. During runtime also there is no error. When I fill in the form and submit it shows only blank screen :( No output is displayed. :( Also in the database values are not updated.
当我编译或调试时,它没有显示任何错误。在运行期间也没有错误。当我填写表格并提交它只显示空白屏幕:(没有输出显示。:(也在数据库中的值不更新。
Please help me. I m getting hangover with this snippet.
请帮帮我。我用这个片段得到了宿醉。
7 个解决方案
#1
2
Check that doPost()
method of servlet is called from the jsp form and remove conn.commit
.
检查从jsp表单调用servlet的doPost()方法并删除conn.commit。
#2
0
Can you check value of i
by putting logger or println(). and check with closing db conn at the end. Rest your code looks fine and it should work.
你可以通过放置logger或println()来检查i的值。并检查最后关闭db conn。休息你的代码看起来很好,它应该工作。
#3
0
Same problem fetch main problem in PreparedStatement
use simple statement then you successfully insert record same use below.
同样的问题在PreparedStatement中使用简单语句获取主要问题然后你成功插入记录同样使用如下。
String st2="insert into
user(gender,name,address,telephone,fax,email,
destination,sdate,edate,Participant,hcategory,
Culture,Nature,People,Cities,Beaches,Festivals,username,password)
values('"+gender+"','"+name+"','"+address+"','"+phone+"','"+fax+"',
'"+email+"','"+desti+"','"+sdate+"','"+edate+"','"+parti+"',
'"+hotel+"','"+chk1+"','"+chk2+"','"+chk3+"','"+chk4+"',
'"+chk5+"','"+chk6+"','"+user+"','"+password+"')";
int i=stm.executeUpdate(st2);
#4
0
In your JSP at line <form>
tag, try this code
在您的JSP
<form name="registrationform" action="Register" method="post">
#5
0
String user = request.getParameter("uname");
out.println(user);
String pass = request.getParameter("pass");
out.println(pass);
Class.forName( "com.mysql.jdbc.Driver" );
Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/rental","root","root" ) ;
out.println("hello");
Statement st = conn.createStatement();
String sql = "insert into login (user,pass) values('" + user + "','" + pass + "')";
st.executeUpdate(sql);
#6
0
Remove conn.commit from Register.java
从Register.java中删除conn.commit
In your jsp change action to :<form name="registrationform" action="Register" method="post">
在您的jsp更改操作中:
#7
0
I had a similar issue and was able to resolve it by identifying which JDBC driver I intended to use. In my case, I was connecting to an Oracle database. I placed the following statement, prior to creating the connection variable.
我有一个类似的问题,并能够通过识别我打算使用哪个JDBC驱动程序来解决它。就我而言,我正在连接到Oracle数据库。在创建连接变量之前,我放置了以下语句。
DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver());
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
#1
2
Check that doPost()
method of servlet is called from the jsp form and remove conn.commit
.
检查从jsp表单调用servlet的doPost()方法并删除conn.commit。
#2
0
Can you check value of i
by putting logger or println(). and check with closing db conn at the end. Rest your code looks fine and it should work.
你可以通过放置logger或println()来检查i的值。并检查最后关闭db conn。休息你的代码看起来很好,它应该工作。
#3
0
Same problem fetch main problem in PreparedStatement
use simple statement then you successfully insert record same use below.
同样的问题在PreparedStatement中使用简单语句获取主要问题然后你成功插入记录同样使用如下。
String st2="insert into
user(gender,name,address,telephone,fax,email,
destination,sdate,edate,Participant,hcategory,
Culture,Nature,People,Cities,Beaches,Festivals,username,password)
values('"+gender+"','"+name+"','"+address+"','"+phone+"','"+fax+"',
'"+email+"','"+desti+"','"+sdate+"','"+edate+"','"+parti+"',
'"+hotel+"','"+chk1+"','"+chk2+"','"+chk3+"','"+chk4+"',
'"+chk5+"','"+chk6+"','"+user+"','"+password+"')";
int i=stm.executeUpdate(st2);
#4
0
In your JSP at line <form>
tag, try this code
在您的JSP
<form name="registrationform" action="Register" method="post">
#5
0
String user = request.getParameter("uname");
out.println(user);
String pass = request.getParameter("pass");
out.println(pass);
Class.forName( "com.mysql.jdbc.Driver" );
Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/rental","root","root" ) ;
out.println("hello");
Statement st = conn.createStatement();
String sql = "insert into login (user,pass) values('" + user + "','" + pass + "')";
st.executeUpdate(sql);
#6
0
Remove conn.commit from Register.java
从Register.java中删除conn.commit
In your jsp change action to :<form name="registrationform" action="Register" method="post">
在您的jsp更改操作中:
#7
0
I had a similar issue and was able to resolve it by identifying which JDBC driver I intended to use. In my case, I was connecting to an Oracle database. I placed the following statement, prior to creating the connection variable.
我有一个类似的问题,并能够通过识别我打算使用哪个JDBC驱动程序来解决它。就我而言,我正在连接到Oracle数据库。在创建连接变量之前,我放置了以下语句。
DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver());
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());