提问者:小点点

KendoUI for JSP-如何连接到MySQL


伙计们,我对剑道比较陌生,现在我正在通过他们的例子学习如何用数据库中的数据填充剑道网格。 但是,他们的例子是用SQLite,我想用MySQL试试。 以下是我到目前为止所做的工作:
clients.java

@WebServlet(description = "A servlet to return data about employees from the database", urlPatterns = {"/src.api/clients"})
public class Clients extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private ClientRepository _repository = null;
    private Gson _gson = null;

    public Clients() {
        super();
    
        _gson = new Gson();
    }

    public void init() throws ServletException {
        super.init();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {

            // get the clients from the database
            _repository = new ProductsRepository(this.getServletContext().getRealPath("data/sample.db"));
            // set the content type we are sending back as JSON
            response.setContentType("application/json");

            // convert the list to json and write it to the response
            response.getWriter().print(_gson.toJson(clients));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

ClientsRepository.java

public class ClientRepository {
    
    public ClientRepository() { }

    public ClientRepository(String path) {
        
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/clients_table?" +
                                   "user=*****&password=******");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

    }

    public List<Client> listClients() {         
        List<Client> clients = new ArrayList<Client>();

        try {
            
            PreparedStatement stmt = null;
            
            String query = "SELECT c.id, c.first_name, c.second_name, c.family_name, "
                    + "c.city_born, c.age "
                    + "From clients_db.clients_table c ";               
            
            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {
                
                Client client = new Client();
                
                client.setClientID("setClientID");
                client.setFirstName("setFirstName");
                client.setSecondName("setSecondName");
                client.setLastName("setLastName");
                client.setCityBorn("setCityBorn");
                client.setAge("setAge");
                
                clients.add(client);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // return the result list        
        return clients;
    }
}

DataSourceResult.java

public class DataSourceResult {

    private int Total;
    private List<?> Data;
    public int getTotal() {
        return Total;
    }
    public void setTotal(int total) {
        Total = total;
    }
    public List<?> getData() {
        return Data;
    }
    public void setData(List<?> data) {
        Data = data;
    }       
}

他们使用这个_repository并从本地。db文件中获取数据。 我如何重构他们的解决方案以使用MySQL而不是本地。db文件?


共1个答案

匿名用户

doget中,您需要创建ClientRepository类的新对象,然后调用ListClients()从数据库获取用户列表,然后返回到Ajax.您需要在servlet中进行的一些更改:

 ClientRepository  _repository = new ClientRepository();//create object
 List<Client> clients = _repository.listClients();//call method
 response.setContentType("application/json");
// convert the list to json and write it to the response
 response.getWriter().print(_gson.toJson(clients));

然后在ClientRepository中进行以下更改:

//to get connection
    public static Connection getConnection() {

     try {
      Class.forName("com.mysql.cj.jdbc.Driver");

      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/clients_table?" +
       "user=*****&password=******");

     } catch (ClassNotFoundException | SQLException e) {
      e.printStackTrace();
     }
     return conn; //return connection object
    }
    public List <Client> listClients() {
     List <Client> clients = new ArrayList <Client> ();

     try {
      //get connection
      Connection connection = getConnection();
      PreparedStatement stmt = null;

      String query = "SELECT c.id, c.first_name, c.second_name, c.family_name, " +
       "c.city_born, c.age " +
       "From clients_db.clients_table c ";
      stmt = connection.prepareStatement(query);
      ResultSet rs = stmt.executeQuery();

      while (rs.next()) {

       //adding code .. here
            client.setClientID(rs.getInt("id"));
            client.setFirstName(rs.getString("first_name"));
       //and so on..
           
      }
     } catch (SQLException e) {
      e.printStackTrace();
     }

     // return the result list        
     return clients;
    }