JSP Multiple SQL Selects - java

I am getting familiar with Java through involvement on a servlet web service project that is now up and running. But the stakeholders want a front end that will allow them to examine the audit table set up in a SQL server database. I am trying to quickly put one together in JSP with initial success but now stuck.
I want a drop down list to provide a list of all the users (Unique). I have acheived this using a JSTL taglib method following the link below with the following SQL;
"SELECT DISTINCT(USER_ID) FROM AUDIT_MESSAGE".
http://www.apekshit.com/t/205/JSTL-SQL-Query-Tag-Example
Now I want, when the user clicks on one of the users names it automatically populates another drop down list with a list of times that user logged in with the following SQL;
SELECT SESSION_ID, EventTIME FROM dbo.AUDIT_MESSAGE
WHERE OPERATION = 'loginResponse' AND RESULTS = 'OK'
AND USER_ID = 'firstdropdownlistselection'
Then when an item from the second drop down list option is selected a third SQL statement of;
Select * FROM dbo.AUDIT_MESSAGE
WHERE SESSION_ID = 'seconddropdownlistselection'
This is then used to populate a list box of the Users events at the selected time.
I have found the following link below offering another method but I am unsure whether to continue with the method I have started with or is there a better way. Can someone please advise me on what method would work best?
Multiple ResultSets
Thanks in advance
AJF

You could display the whole database table information(minus the ids) if this is what they want.
If you want to do it in these steps (select user, select session, display information) what you are doing is correct.

Related

Hiding information for a particular user

I am using RESTFUL webservices using Spring framework.Some information is displayed on the user interface using the data returned by webservices. There is a webservice, which gets the usernames and their roles from the database. For a particular user , I would like to have all the webservices display data in the form of ####,#### for first name,lastname ; ##/##/#### for date of birth etc. Since I am using JDBC to connect to the database, here's what I was thinking of doing:
Should I consider passing an additional parameter (maybe sending a value 0 or 1 ; 0 for all other users and 1 for user for which I want to hide the information) to each and every GET webservice so that when it comes to getting data from the database in the JDBC code part,I could check whether the flag is set to 1 or 0 and based on this, I could do something like this in the JDBC code :
// Code for a case when flag is set to `0`. Hence retrieving information from the database.
while(rs.next()) {
EmployeeList empList = new EmployeeList();
empList.setEmpId(empId);
empList.setEmployeeName(rs.getString("name"));
employeeList.add(empList);
}
// Code for a case when flag is set to `1`. Hence hiding information and not retrieving information from the database.
while(rs.next()) {
EmployeeList empList = new EmployeeList();
empList.setEmpId(empId);
empList.setEmployeeName("####,######");
employeeList.add(empList);
}
I am wondering, if this is an appropriate way to achieve my task or is there some other way around?
Edit:More clarifications on my requirements:
I am using jqxWidget in the UI to display the information I am getting from a RESTFUL webservice in JSON format. For example, let's consider this example and the screenshot for better understanding of my requirement:
1) Let's say I am getting all the information from the JSON response which I am populating in the jQXWidget as shown in the screenshot above.
2) In the above widget, I would like to hide say for example, First Name, Last Name and Quantity like the following:
First Name = XXXXX
Last Name = XXXXX
Quantity = ####
In my application, if a user clicks on a particular row , a new page is displayed with some additional information. After click, new sets of web services are called and those web services takes First Name, Last Name and Quantity as input parameters. My concern is that, if I somehow replace the First Name with XXXXX, Last Name with XXXXX and Quantity with #### using any approach, when a user clicks on any of the row of the widget, the next set of web services
are going to get XXXX and #### as input and eventually will fail. Please correct me if my understanding until this point is not correct.
Thanks
I am using Spring 4.2.5 version.
This depends on what sort of information hiding you want to achieve. Typically you shouldn't do this manually.
You can use, for example, role-based authorization. Exact details depends on the web-service framework you are using.
For spring MVC, you can use something similar to this:
Custom authorization in Spring MVC

How to display data from sql server with java [android studio]

I wish to display some data from my SQL server into my application.
Let me explain throw an example what have i done until now, and please tell me how should i go on with my work:
(1) I have a database named: 'DB1'
(2) In my database i have table named 'users':
Id, Name, LastName
1 Dave Stone
2 Rose Mary
3 Gray Lone
(3) I created 3 php files: 1/ config.php (sets up my database log in information like password...)
2/ DBConnection.php : `
// Connecting to database
public function connect() {
require_once 'Config.php';
// Connecting to mysql database
$this->conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
// return database handler
return $this->conn;
}
}
?>
3/ GetUsers.php which i dont know how should i write it - this would be the code which will echo whatever i want from the table.
(4) Inside my android studio project i have a MainActivity.java and i want to get all the LastNames of all the users in the database and do whatever with them... like print them into a text box it doesn't really matter (let's say i don't know how many users I have).
I need some one to help me get stages (3)3/ + (4) done / explained.
I think what you mean is a good method to retrieve and set values from app to database, the best and most prominent way is using basic REST services that convert your db values to JSON then you load the json using your api key as authentication, it might sound really stressful but is in fact very easy and flexible to implement, the only way you can directly set and get db values on android is using sqlite (android local inbuilt db)
The best tutorial and major explanation for exactly what you want to achieve is here
http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-12-2/
Then this one for further works
http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/
It covers how you make a rest service yourself

How to know which data is from which table when you select data from multiple tables?

I have a table of client contain the client id, name and last name, job ..etc...
also I have 16 tables of the grants of client each table contain a different data(different grant) relate with the client by a foreign key of "id".......
for example this is costumer table"clients":
enter image description here
and this is an example for an grant of client called order:
enter image description here
So when I search for client, I search also for all the grants that he benefited them, and maybe a client can benefit from the same grant more than once........that's means we can find more than one row in the same table relate with the same client:
As you see in the table Of order we have two row relate with the client who have id=2.: So I want to make one query to select all data from the first table and the second of the client "Where id =2"
the problem is how I can get all the information relate with that client and to which row from which table ??
If you want to access data from particular Table you can see the following code It may help you
public Cursor getInformation(databaseOperations dbo){
SQLiteDatabase Db = dbo.getReadableDatabase();
String [] columns = {TableData.TableInfo.USER_NAME, TableData.TableInfo.CONTACT_NO, TableData.TableInfo.EMAIL_ID,TableData.TableInfo.PROFILE_PATH};
Cursor CR =Db.query(TableData.TableInfo.TABLE_NAME,columns,null,null,null,null,null);
return CR;
}
If you need data from particular row or column then you can pass different argument instead of null in DB.query.
To know about different parameters you can see just this answer click here
I hope it will help you. To know more about parameter details of db.query() function you can go through google search.

Moving particular row from one table to another in mysql

I am developing a web app.... in which there will be a list of users requesting registration ...Wen ever we click on accept all the details must be moved from register table to login table...
PreparedStatement ps=conn.prepareStatement("insert into login(id,FirstName,LastName,Gender,Category,Dateofbirth,Age,Address,Country,State,city,PinCode,EmailId,ContactNo,MobileNo)select * from register ");
i have to insert all data from register table to login table when i click accept. But when i run this the whole table gets copied from register to login table...When i click accept only that particular users details must be moved.... Id must be fetched of that particular user... How to do that. Please someone help me fix this... Thanks in advance....
You should add a WHERE clause to the SQL statement:
INSERT INTO LOGIN (id,FirstName,LastName,Gender, ...)
SELECT * FROM REGISTER WHERE ID=?
then bind the id parameter to the PreparedStatement:
PreparedStatement ps=conn.prepareStatement( ... );
ps.setInt(1, id);
ps.executeUpdate();
This way you are limiting your SELECT to return only the row you are interested in and feeding it into the INSERT.

how to delete multiple data using check box in google app engine (JAVA)

In Google app engine, how can i delete multiple data selected in check box. For a sample i had attached the image below. here i had selected the multiple check boxes and the data shown are stored in the Google app engine.
i have my jsp code for check box like this,
<input name="delete" type="checkbox"/>
can anyone suggest me how to select the data and delete it from Google app engine.
Edited:
For storing the data i used,
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity employee = new Entity("Employee");
employee.setProperty("First Name", fname);
datastore.put(employee);
For retrieving the data i used,
Query query = new Query("Employee");
List<Entity> emp = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(20));
for (Entity user : emp){
// inside the table
user.getProperty("File Name")}
By this i can retrieve the data.
First of all, I recommend that you use two frameworks to make your life easier:
Stripes
Objectify
OK, here you go: I'm not sure to which degree your question is a basic JSP/Servlet question or a specific Appengine question. However, you have to do the follwoing:
Have a list of checkboxes on your JSP page, like this: <stripes:checkbox name="employees[${loop.index}].id" value="${employee.id}"/> (see: http://www.stripesframework.org/display/stripes/Indexed+Properties)
Pass a list of IDs (of the entities that should be deleted) to the Servlet.
Delete the entities based on these IDs.
Here's the catch: Appengine can terminate the request before all entities are deleted. That can happen if the request takes too long. Therefore you should delete the entities asynchronously, using chunks of data. See this answer for further information.

Categories