I'm trying to display data in the select tag, where the data are coming form the database. I'm using servlet to fetch data from database. but I'm not getting how to call the servlet from angularjs and display that data in the select tag.
I'm here sharing my code please help me out.
servlet1.java code:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/apm", "root", "root12345");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select machine_name from machines");
int i = 1;
String res = "[",separator = "";
while (rs.next()) {
res += separator + rs.getString(1) ;
separator = ",";
}
res += "]" ;
out.println(res);
}}
servlet2.java code:
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/apm", "root", "root12345");
Statement st = con.createStatement();
ResultSet rs;
String start = request.getParameter("start");
String index = request.getParameter("index");
String tablename = request.getParameter("tablename");
if (start == null) {
start = "0";
}
if (index == null) {
index = "1";
}
if (tablename == null) {
tablename = "data";
}
String column = "";
boolean first = true;
rs = st.executeQuery("show columns from" + tablename );
int i = 1;
String res = "[",separator = "";
while (rs.next()) {
res += separator + rs.getString(1) ;
separator = ",";
}
res += "]" ;
out.println(res);
}
angular js code:
myApp.controller('MyCtrl', ['$scope', function ($scope) {
$scope.blocs = [];
$scope.fields = [];
$scope.addNewField = function (index) {
alert(index);
$scope.blocs[index].fields.push({
sensor : "",
sensor1 : "",
sensor2 : ""
});
};
$scope.addBloc = function () {
$scope.blocs.push({
fields : []
});
};
}
]);
jsp code:
<div ng-controller="MyCtrl">
<button ng-click="addBloc()">
Add Machines
</button>
<div ng-repeat="bloc in blocs">
<div>
<select>
<option>---select machine--</option>
</select>
<button ng-click="addNewField($index)">
Add sensors
</button>
</div>
<div ng-repeat="field in bloc.fields">
<select ng-model="field.sensor">
<option value="sensor1">sensor1</option>
<option value="sensor2">sensor2</option>
</select>
<input type="text" ng-model="field.sensor1">
<input type="text" ng-model="field.sensor2"> {{field.sensor}} {{field.sensor1}} {{field.sensor2}}
</div>
</div>
</div>
in the 1st select tag I need to get data from 1st servlet code (i.e servlet1.java).
in the 2nd select tag I need to get data from the 2nd servlet code (i.e servlet2.java).
Please any1 help me out ... :(