我想以有条理的方式将数据插入数据库,我有一个jsp
页面,它从用户那里获取输入id
name
salary
并发送到数据库表雇员。表格包含三列eid
,ename
,esalary
。
在我的编码部分,我有三个包:com.mvc.javaclassbeans
,com.mvc.javaclasses
,com.mvc.servlets
。
myservlet
包中的 com.mvc.servlets
类从jsp页面获取参数(eid,ename,esalary),并设置创建employee
类对象的参数并插入{{ 1}} eid
ename
使用esalary
我的代码没有显示任何错误,但没有插入值。我没有遇到问题,因为我已经将我的代码分成不同的包以使其作为mvc工作。
我的com.mvc.javaclasses pakage包含
com.mvc.javaclassbeans由
组成com.mvc.servlets由
组成以下是我的代码:
insert.jsp
new insertclass().insertfunction(obj);
mysevlet
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="myservlet" method="post">
ID:<input type="text" name="eid">
NAME:<input type="text" name="ename">
SALARY:<input type="text" name="esalary">
<button type="submit" name="submit" value="submit" >Sign in</button>
</form>
</body>
</html>
请帮我找到我遗漏的问题。
答案 0 :(得分:1)
这段代码对我有用。为了测试我的配置是:
主要代码(只需将此代码放在Test
类中):
package sarah;
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.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
class dbconnector {
//String user = "system";
//String pass = "system";
//String driver = "oracle.jdbc.OracleDriver";
//String dbURL = "jdbc:oracle:thin:@localhost:1521:XE";
ResultSet rs;
Connection connection = null;
Statement stmt = null;
public Connection Open() {
System.out.println("-------- MySQL JDBC Connection Testing ------------");
try {
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "postgres", "root");
System.out.println("Succeed!");
// return connection;
} catch (Exception e) {
System.out.println("you have stucked with error!!!!!!");
}
System.out.println("MySQL JDBC Driver Registered!");
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
return connection;
}
public void db_Close(Connection con) throws SQLException {
con.close();
return;
}
}
class employee {
String eid;
String ename;
String esalary;
// public employee(String eid, String ename, String esalary) {
// this.eid = eid;
// this.ename = ename;
// this.esalary = esalary;
//}
public String getId() {
return eid;
}
public void setId(String eid) {
this.eid = eid;
}
public String getName() {
return ename;
}
public void setName(String ename) {
this.ename = ename;
}
public String getSalary() {
return esalary;
}
public void setSalary(String esalary) {
this.esalary = esalary;
}
}
class insertclass {
// Connection connection = null;
PreparedStatement ps;
public String query;
public boolean insertfunction(employee ob) throws ClassNotFoundException, SQLException {
try {
dbconnector dbc = new dbconnector();
Connection connection = dbc.Open();
query = "insert into employee (eid,ename,esalary) values(?,?,?)";
java.sql.PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, ob.getId());
ps.setString(2, ob.getName());
ps.setString(3, ob.getSalary());
ps.executeUpdate();
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
}
@WebServlet(name = "Test", urlPatterns = {"/Test"})
public class Test extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
employee obj = new employee();
obj.setId(request.getParameter("eid"));
obj.setName(request.getParameter("ename"));
obj.setSalary(request.getParameter("esalary"));
try {
new insertclass().insertfunction(obj);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
out.print("sucess");
} catch (Exception ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
表格定义:
CREATE TABLE employee
(
eid text,
ename text,
esalary text
)
WITH (
OIDS=FALSE
);
ALTER TABLE employee
OWNER TO postgres;
和test.jsp
档案:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="Test" method="post">
ID:<input type="text" name="eid">
NAME:<input type="text" name="ename">
SALARY:<input type="text" name="esalary">
<button type="submit" name="submit" value="submit" >Sign in</button>
</form>
</body>
</html>