String url = "jdbc:mysql://db.kangruicloud.com:23306/village";
String account = "root";
String passeword ="123456"
String tableName= "user"
JSONArray userArray= getDeviceData(url, account, passeword ,tableName);
List<UserModel> userModel=
JSON.parseArray(JSON.toJSONString(fw), UserModel.class);
private JSONArray getDeviceData(String url, String username , String password , String tableName) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, username, password );
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM "+ tableName;
ResultSet rs = stmt.executeQuery(sql);
JSONArray jsonArray = new JSONArray();
ResultSetMetaData metaData = rs.getMetaData();
int colCnt = metaData.getColumnCount();
while (rs.next()) {
JSONObject jsonObject = new JSONObject();
for(int i=1;i<=colCnt;i++){
jsonObject.put(metaData.getColumnName(i),rs.getObject(i));
}
jsonArray.add(jsonObject);
}
rs.close();
stmt.close();
conn.close();
return jsonArray;
}