Advertisement
Guest User

Untitled

a guest
Dec 15th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. public class Main {
  2.  
  3. private static final String URL = "jdbc:mysql://localhost:3306/test";
  4. private static final String USERNAME = "root";
  5. private static final String PASSWORD = "root";
  6. private static Connection con;
  7.  
  8. private Main() throws Exception {
  9. try {
  10. Driver driver = new FabricMySQLDriver();
  11. DriverManager.registerDriver(driver);
  12. con = getConnection(URL, USERNAME, PASSWORD);
  13.  
  14. } catch (SQLException e) {
  15. System.err.println("Не удалось загрузить класс драйвера!");
  16. }
  17. }
  18.  
  19.  
  20.  
  21. public List<User> getUsers(Connection con) throws SQLException {
  22. List users = new ArrayList();
  23. ResultSet rs = null;
  24.  
  25. try (CallableStatement stmt = con.prepareCall("{CALL sel}")) {
  26. boolean hadResults = stmt.execute();
  27.  
  28. while (hadResults) {
  29. try (ResultSet resultSet = stmt.getResultSet()) {
  30.  
  31. while (resultSet.next()) {
  32. int id = rs.getInt("ID");
  33. String name = rs.getString("NAME");
  34. users.add(new User(id, name));
  35. }
  36. } catch (SQLException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40.  
  41. stmt.close();
  42. return users;
  43.  
  44. }
  45. }
  46.  
  47. public static void ins_upd (Connection con, int id, String name) {
  48.  
  49. try(CallableStatement stmt = con.prepareCall("{CALL ins_upd(?,?)}")){
  50. stmt.setInt(1, id);
  51. stmt.setString(2, name);
  52. stmt.close();
  53. }
  54. catch(SQLException e) {
  55. e.printStackTrace();
  56. }
  57.  
  58.  
  59. }
  60.  
  61.  
  62. public static void del (Connection con, int ID) {
  63. try(CallableStatement stmt = con.prepareCall("{CALL del(?)}")){
  64. stmt.setInt(1, ID);
  65. stmt.close();
  66.  
  67. }
  68. catch(SQLException e) {
  69. e.printStackTrace();
  70. }
  71.  
  72. }
  73. }
  74.  
  75. import java.util.ArrayList;
  76. public class User {
  77. private static ArrayList<User> users;
  78. private int id;
  79. private String name;
  80.  
  81.  
  82.  
  83. public User (int id, String name) {
  84. super();
  85. this.id = id;
  86. this.name = name;
  87. }
  88. public static ArrayList<User> getUsers() {
  89. return users;
  90. }
  91.  
  92. public int getId() {
  93. return id;
  94. }
  95. public void setId(int id) {
  96. this.id = id;
  97. }
  98. public String getName() {
  99. return name;
  100. }
  101. public void setName(String name) {
  102. this.name = name;
  103. }
  104. public String toString() {
  105. return id + " " + name;
  106. }
  107.  
  108. }
  109.  
  110. public class MainServlet extends HttpServlet {
  111. public void doGet(HttpServletRequest req, HttpServletResponse resp)
  112. throws ServletException, IOException {
  113. RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("index.html");
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement