gpt4 book ai didi

java - 使用setter getter类通过MVC模型将数据插入数据库

转载 作者:行者123 更新时间:2023-12-01 11:19:40 27 4
gpt4 key购买 nike

我想以有组织的方式将数据插入数据库,我有一个 jsp接受输入 id 的页面name salary来自用户并发送到数据库表员工。表有三列 eid , ename , esalary .

在我的编码部分我有三个包:com.mvc.javaclassbeans , com.mvc.javaclasses , com.mvc.servlets .

myservlet类(class) com.mvc.servlets包从jsp页面获取参数(eid,ename,esalary)并处理以创建employee对象来设置参数类并插入 eid ename esalary使用 new insertclass().insertfunction(obj); 的值

我的代码没有显示任何错误,但没有插入值。我没有遇到问题,因为我已将代码分成不同的包以使其作为 mvc 工作。

我的 com.mvc.javaclasses 包包含

  • 数据库连接器
  • 插入类

com.mvc.javaclassbeans 组成

  • 员工类别

com.mvc.servlets 组成

  • myservlet 类

下面是我的代码:

插入.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="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>

mysevlet

 package com.mvc.servlets;
import com.mvc.javaclassbeans.employee;
import com.mvc.javaclasses.dbconnector;
import com.mvc.javaclasses.insertclass;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "myservlet", urlPatterns = {"/myservlet"})
public class myservlet 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(myservlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(myservlet.class.getName()).log(Level.SEVERE, null, ex);
}

out.print("sucess");
}
catch (Exception ex) {
Logger.getLogger(myservlet.class.getName()).log(Level.SEVERE, null, ex);
}

}

}

请帮助我找到我遗漏的问题。

最佳答案

这段代码对我有用。为了测试我的配置是:

主要代码(只需将此代码放入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>

关于java - 使用setter getter类通过MVC模型将数据插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31398396/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com