kendoui mysql_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.javapublic 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 listClients() {

List clients = new ArrayList();

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.javapublic 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文件?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值