Есть БД MySQL с таблицей mytable (column ID, column NAME), пишу приложение для вывода в файл HTML результатов выполнения хранимых процедур (вывести таблицу, вставить и удалить значение) Написал класс для работы с бд:
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();
}
}
}
и сущность user
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;
}
}
Насколько я понимаю осталось написать сервлет для связи запросов клиента и ответов сервера. Не знаю как мне вывести методы в классе Main в HTML форму и соединить эту форму с сервлетом? Пока всё что в севрлете есть :
public class MainServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("index.html");
}
}
Сборка персонального компьютера от Artline: умный выбор для современных пользователей