实际上我试图将java servlet中的值插入到postgres DB中,但它们没有插入。 提前谢谢。
我的html页面是
<div class="container4">
<h2>Registration form</h2>
<form action="Register" method="get" >
<div class="form-group">
<label for="firstname">First Name:</label>
<input type="text" class="form-control" id="firstname" placeholder="Enter firstname" name="firstname">
</div><br><br>
<div class="form-group">
<label for="Username">Username:</label>
<input type="text" class="form-control" id="username" placeholder="Enter username" name="username">
</div><br><br>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
</div><br><br>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
</div><br><br>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" class="form-control" id="address" placeholder="Enter address" name="address">
</div><br><br>
<button type="submit" class="btn btn-default" id="submit">Submit</button>
</form>
</div>
我的dbconnection页面是
package dbConnection;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
/**
*
* @author java2
*/
public class Dbconn {
public static Connection getConnection() {
Connection con = null;
try {
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/classes","postgres", "admin123$");
System.out.println("db successfull");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("db not successfull");
}
return con;
}
}
Register.java
package register;
import dbConnection.Dbconn;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Register extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection con = null;
Statement st = null;
String n=request.getParameter("firstname");
String p=request.getParameter("username");
String e=request.getParameter("email");
String c=request.getParameter("pwd");
String d=request.getParameter("address");
try {
con = Dbconn.getConnection();
st = con.createStatement();
int i = st.executeUpdate(
"insert into register (firstname, username, email, pwd, address) values('"+n+"','"+p+"','"+e+"','"+c+"','"+d+"')");
con.commit();
System.out.println("hello successfull");
if (i == 0) {
response.sendRedirect("login.html?msg=success");
} else {
response.sendRedirect("register.html?msg=failed");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}}
当我运行这个程序时,我变得像
我收到了网址中的所有详细信息,并显示空白页面没有错误。我不明白我哪里出错了。