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

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

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 insert JSON data to sql server without converting into Java beans

My requirement is like, i have a json file data as below.,
{Key1: value1, key2: value 2,....} with file name example.json
It should be inserted into sql server as,
Take name: example
Data as below,
Id key1 key2
1 value 1 value 2
I'm searching for a solution such that, there won't be any Java layer complexities, like first converting this data into Java beans then inserting into sql using java database drivers...
Implementation should be language and database independent. In future if i change my database server, then it should be a minimal change.
The coupling should be very loose. Like if i introduce a new key in JSON file, that should be minimal change in the solution.
Tia
So, use some library that can do http request to the server. For eg, if you want insert your json data to server in android app , then use simple libraries like Volley , OkHttp etc.
Problem with your question is , we dont know which server you r using and what application you are doing,, and which programming language you wanna use,, So, im assuming is java android,,

Can we set one database between Java and python?

I created python script that sends notifications when result declared but I want to make website that takes data of student email id and store in database.
Now here problem is that I don't know django framework so it takes time to make website.
Java, database connection, Data insertion,
Servelet calling easily do that by me.
Want to know way that java html css takes input from user and stores in database and then python program retrieves that data.
Hope you understand my question.
If you know python, I think Django might be a good choice for you. Django itself attempts to support as many features as possible on the backend. You only need to configure the database parameters in settings.py and Django ORM can automate the transfer of data stored in tables into objects. You don't have to implement the features yourself and it saves you tons of time.
Django has its own ModelForm class. It maps to the Models(eg. Student), it is quite easy to submit your Student info and store it in db.
# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/path/to/my.cnf',
},
}
}
# my.cnf
[client]
database = NAME
user = USER
password = PASSWORD
default-character-set = utf8
If you insist using Java to implement the database backend, for sure you can share it with python. Some choices like JDBC, Hibernate and Mybatis.
Yes you can share the DB, you'll have to install the corresponding dependencies for connecting python to the DB as well as for Java. I've done this with postgresql, mysql and mssql

Retrieving full objects from a query done via Bolt protocol

In Neo4J, I want to use the bolt protocol.
I installed the 3.1 version of Neo4J.
I my Java project, that already works well with normal HTTP Rest API of Neo4J, I integrate with Maven the needed drivers and achieve to perform request with BOLT.
The problem is everywhere you make a search about bolt they give example like this one :
MATCH (a:Product) return a.name
But I don't want the name, I want all the data of all product, what ever i know or not before what are these columns, like here:
MATCH (a:Product) return * --> here I retrieve only the ids of nodes
I found there https://github.com/neo4j-contrib/neo4j-jdbc/tree/master/neo4j-jdbc-bolt we can "flatten" the result but it seems to not work or I didn't understand how it works:
GraphDatabase.driver( "bolt://localhost:7687/?flatten=-1", AuthTokens.basic( "neo4j", "......." ) );
I put the ?flatten=-1 at the end of my connection address... but that changed nothing.
Anyone can help? Or confirm it's not possible or not working ?
Thanks
Ok I understood my error, I didn’t dig enough in the object returned. So used to have a JSON formatted response, I didn’t see that I have to search in the StatementResult object to find the wanted object with its properties. In fact Eclipse in the “expressions” shows “in fly” only the ids, but inside the object data are there.
Record oneRecord = rs.next();
String src = oneRecord.get("m").get("source");
That way I can reconstruct my object

Java mysql using url patterns

I'm trying to build a java http server connected to a database (mysql) where one of my objectives are to NOT use a gui.
Instead, to let the "user" insert data by simply entering the desired input on top of a URL
For example:
in this url, the user is inserting his id number = (123) and his name = (JOE)
localhost/employee/add?id=123&name=JOE
or a remove example, removing a row by id only..
localhost/employee/remove?id=123
I was searching the web for a code example for over 4 hours and got to early morning hours with no luck :/
All I found was that it's called "url patterns" which you configure via java EE & net beans or similar platforms (all that I've encountered so far..)
but none of the tutorials/explanations wont demonstrate how to implement that by sending queries and configuring multiple url patterns together.
Can someone explain, demonstrate this technique ?
Thousand thankyous in advance, Iguana.

Categories