Call Stored Procedures in a java client application - java

So, my problem is: I've developed some REST services with Jersey, in Glassfish 3.1.2, that were generated from my MySQL database. Then I've implemented Basic-Authentication. That's all working fine.
Now, I'm developing a client application to consume those services. I've used Apache HttpClient to the authentication, and I've already tried the GET method, and the PUT method. I can obtain the values from my DataBase, and update others, with the aid of a JSON object.
But how can I use the Stored Procedures that I have in my database, to do a PUT/POST and update my DataBase??
Thanks

You will need to use JDBC, something like this
cs = this.con.prepareCall("{call SHOW_SUPPLIERS()}");
ResultSet rs = cs.executeQuery();
while (rs.next())
{
String supplier = rs.getString("SUP_NAME");
String coffee = rs.getString("COF_NAME");
System.out.println(supplier + ": " + coffee);
}
As an example, see following documentation (where I got the above code from)
http://docs.oracle.com/javase/tutorial/jdbc/basics/storedprocedures.html

Related

SpringBoot + AWS DynamoDB and atomic code block

For a SpringBoot project I'm using DynamoDB as a Database.
I'm not a DynamoDB DB expert.
Currently, into my service, I have this high level logic implemented:
result = service1.checkStatus() // here I have a query on the DB and I check the result of the query
if (result) {
saveNewEntity()
}
Using this pseudocode, I would like to give an overview of the logic in order to explain the issue:
Probably I need to have this code into a 'Transaction'. I mean, in a stress test session, I noticed that, using different requests in parallel, I'm able to save new entitles bypassing the check (checkStatus).
How can I have this?

Spring R2DBC + SQL Server: procedures query

I am required to execute a stored procedure in a SQL server to fetch some data, and since I will later save the data into a Mongo and this one is with ReactiveMongoTemplate and so on, I introduced Spring R2DBC.
implementation("org.springframework.data:spring-data-r2dbc:1.0.0.RELEASE")
implementation("io.r2dbc:r2dbc-mssql:0.8.1.RELEASE")
I see that I can do SELECT and INSERT and so on with R2DBC, but is it possible to EXEC prod_name? I tried it and it hangs forever and then the test terminates, without success but neither failure. The last line of log is:
io.r2dbc.mssql.QUERY - Executing query: EXEC "SCHEMA"."MY_PROCEDURE"
The code is like:
public Flux<Coupon> selectWithProcedure() {
return databaseClient
.execute("EXEC \"SCHEMA\".\"MY_PROCEDURE\" ")
.as(Coupon.class)
.fetch().all()
.doOnNext(coupon -> {
coupon.setCouponStatusRefFromId(coupon.getCouponStatusRefId());
});
}
And it seems that no data is retrieved.
If I test some other methods with simple queries like SELECT... it works. But the problem is, DBAs do not allow my app to read table data, instead, they create a procedure for me. If this query is not possible, I must go with traditional JPA way and going reactive at Mongo side has lost its sense.
Well. I just saw this:
https://github.com/r2dbc/r2dbc-mssql, version 0.8.1:
Next steps:
Execution of stored procedures
Add support for TVP and UDTs
And:
https://r2dbc.io/2019/05/13/r2dbc-0-8-milestone-8-released
We already have a few tickets lined up for the next milestone, and we know that they will require further SPI modifications:
Support for Auto-Commit
Connection Validation
Support for Stored Procedures

Connect to localhost database

I got a little question about databases and android. I got this code:
sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
SAMPLE_TABLE_NAME +
" (LastName VARCHAR, FirstName VARCHAR," +
" Country VARCHAR, Age INT(3));");
sampleDB.execSQL("INSERT INTO " +
SAMPLE_TABLE_NAME +
" Values ('Makam','Sai Geetha','India',25);");
and to read:
if (c != null ) {
if (c.moveToFirst()) {
do {
String firstName = c.getString(c.getColumnIndex("FirstName"));
int age = c.getInt(c.getColumnIndex("Age"));
results.add("" + firstName + ",Age: " + age);
}while (c.moveToNext());
}
}
With this code, I make and read the database, and insert some info in it. And print it on the screen, this all works :)
Now the part I can't figure out:
I use myPHPadmin (with xampp),
I made the exact database as I do in the code.
But how do I connect, so my code reads that database.
It is a local database for now (127.0.0.1).
Is it possible to connect a local database? (if so, could you tell me how to)
Do you need PHP, or can you do everything in (Android) Java code?
I am totally new with databases, so sometimes it confusing for me.
Please put me in the good direction.
If you need more information for the question or something else, please let me know.
It is a local database for now (127.0.0.1).
In Android you have to use 10.0.2.2 or System's Static IP.
Write a PHP script (You can also use other but PHP its easy to implement) to manage the database and run this script using HTTP protocol from the android system.
These Tutorials might help you:
Step-by-Step-Method-to-Access-Webservice-from-Andr
Web Services - An XML-RPC Client for Android
As far as I'm aware there is no MySQL library for android. But you can use the HttpPost to send data to a server side script (such as PHP) and then return it in a format you can parse in your Android application.
There's a nice tutorial on how to achieve this here: http://www.helloandroid.com/tutorials/connecting-mysql-database
Here's a link to the HttpPost Documentation: http://developer.android.com/reference/org/apache/http/client/methods/HttpPost.html
Hope this helps, this is a good way to get you started communicating with external MySQL databases within an Android application.

How to return a database resultset from a java web service

I am new to java web services. I am fetching records from a database and need to return my resultset from the web service. From what I have read, resultset is a interface so it cannot be used as a return type. I know that dotnet web services have a DataSet type used to return database records. Is there anything similar in java? If not, can anyone suggest what I should use instead of the resultset?
If you need to use a web service then do not use a ResultSet or any other Java specific construct e.g. ArrayList etc.
Just return an array of Records to the client where each array entry is a custom (JAXB) class Record which represents an entry/row in the database.
Just populate the array in your server with the result of the database query and return that array as a result to your clients

how to retrieve data in struts using PreparedStatements with the help of ArrayList?

I am a beginner in struts want to be practice in DAO. I want to know how to retrieve data from table using PreparedStatement and ArrayList?
A good lightweight and easy to use database access library is Jakarta Commons DBUtils.
It is just a thin wrapper on top of JDBC that takes care of finally cleaning up resources. No OR-mapping and things like that.
With it you can write code like
List<Object[]> result = new QueryRunner(dataSource)
.query("select * from the_table where x > ?",
new ArrayListHandler(), 1234);

Categories

Resources