There is a MySQL database with the mytable table (column ID, column NAME), I am writing an application to output the results of stored procedures to the HTML file (display the table, insert and delete the value) I wrote a class for working with the database:
public class Main { private static final String URL = "jdbc:mysql://localhost:3306/test"; private static final String USERNAME = "root"; private static final String PASSWORD = "root"; private static Connection con; private Main() throws Exception { try { Driver driver = new FabricMySQLDriver(); DriverManager.registerDriver(driver); con = getConnection(URL, USERNAME, PASSWORD); } catch (SQLException e) { System.err.println("Не удалось загрузить класс драйвера!"); } } public List<User> getUsers(Connection con) throws SQLException { List users = new ArrayList(); ResultSet rs = null; try (CallableStatement stmt = con.prepareCall("{CALL sel}")) { boolean hadResults = stmt.execute(); while (hadResults) { try (ResultSet resultSet = stmt.getResultSet()) { while (resultSet.next()) { int id = rs.getInt("ID"); String name = rs.getString("NAME"); users.add(new User(id, name)); } } catch (SQLException e) { e.printStackTrace(); } } stmt.close(); return users; } } public static void ins_upd (Connection con, int id, String name) { try(CallableStatement stmt = con.prepareCall("{CALL ins_upd(?,?)}")){ stmt.setInt(1, id); stmt.setString(2, name); stmt.close(); } catch(SQLException e) { e.printStackTrace(); } } public static void del (Connection con, int ID) { try(CallableStatement stmt = con.prepareCall("{CALL del(?)}")){ stmt.setInt(1, ID); stmt.close(); } catch(SQLException e) { e.printStackTrace(); } } } and user entity
import java.util.ArrayList; public class User { private static ArrayList<User> users; private int id; private String name; public User (int id, String name) { super(); this.id = id; this.name = name; } public static ArrayList<User> getUsers() { return users; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return id + " " + name; } } As far as I understand, it remains to write a servlet for communication between client requests and server responses. I don’t know how I can bring the methods in the Main class to an HTML form and connect this form with a servlet? While all that is in the sevlet is:
public class MainServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("index.html"); } }
diapatcher.forward(req, resp);(there is another optiondiapatcher.include(req, resp);). But that's not all. So you only give your index.html to the client. The rest is a theme for a servlet book. Read the waste paper. - Sergey