Unable to create endpoint with JDBC mssql in java - java

I am trying to create endpoint in jsp, i have been able to connect to the database using the following code but i need to understand the structure of it.
<%
String refNumb = request.getParameter("refNumb");
// String display = request.getParameter("display");
//String msg = request.getParameter("message");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://IPAddress;databaseName=FIRS",
"user", "pass");
%>

You have successfully done the first step which is to connect to your database via jdbc. You can follow these steps after you successful fetched records from the database.
Convert your records into json string in java, you can use json library and follow the steps on how to convert your records to json
Create a jsp file and use out.print to print the records on your browser.
If you can get to step two then final step is to give a use the url of your jsp which is the end point. As long as anyone can display the json data from your db then that data can easily be consume via js or any other programming language.
You can also research via google on how to implement and consume webservices in java(Soap or rest) and follow the tutorial step by step.

Related

How to iterate Json object from JDBC Request In Jmeter

I have used JDBC Request in JMeter to get the data from the MySQL table. The select query returns a JSON object like below:
{"HeaderKey":"BR023159537221170209370","ReceiptSequenceNumber":"6643182852","status":"Success"}
JDBC Request
I have to get the status key from the object. How I can achieve it using JDBC Request?
Thanks in advance.
According to JDBC Request sampler documentation the result will be stored into ${IHM_response_3_1} variable.
So you can extract the status from there using JSON Extractor configured like:
Once done you should be able to refer extracted value as ${status} where required

Conundrum with POST and GET on Android app

I am trying to gain some information from my localhost database via my android app. I am using the HttpUrlConnection class to create objects that read/write from/to the database.
I am having this problem now, because the HttpUrlConnection object am trying to create needs to read and write at the same time to a PHP file hosted on the localhost database. I see that the HttpUrlConnection class has a method setRequestMethod, which if I want to write to the database i will use setRequestMethod("POST") and if I want to read from the database i will use
setRequestMethod("GET"). I need to do both at the same time. I need to initialize a variable in a PHP file with the POST method, and then search and read row values on the database based on the initialized POSTED variable.
Any advice will be appreciated.
Thank you
The response from server is not depend on GET or POST method. So you can receive the response in both cases, just design your server like this:
$value1 = $_REQUEST["name"];
$value2 = $_REQUEST["name2"];
...
do_some_thing();
...
$sth = mysqli_query("SELECT ...");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
//don't care it's POST or GET request.
See different between GET and POST method: http://www.w3schools.com/tags/ref_httpmethods.asp

Why is an exception generated in java when I use the following code

Document doc1;
String url="http://www.google.com";
url= url +" and 1=1";
doc1=Jsoup.connect(url).get();
Here there is no problem with the connection as the following code gives no exception. The exception is generated only when I try to get the HTML code with the above code.
Document doc1;
String url="http://www.google.com";
url= url +" and 1=1";
Jsoup.connect(url);
Thanks!
JSoup.connect doesn't actually try to connect to the website. If you look through the documentation, you'll see that it only creates a Connection object. You can chain method calls on the Connection to set cookies, user agent, and other stuff before calling get, execute, post, or one of the other methods that will actually send the request.
(Here's another documentation link that might be easier to browse. Unfortunately, Javadoc's use of frames makes linking awkward.)
According to Jsoup javadoc:
Jsoup.connect(String url):
Creates a new Connection to a URL. Use to fetch and parse a HTML page.
Connection.get():
Execute the request as a GET, and parse the result.
So in the first sample you are querying google and getting an IOException because of the invalid URL but not in the second sample (no query is made)
Jsoup.connect doesn't actually connect to anything. It just creates a Connection object. If you want to set any special properties of the connection, you can do it before you call get() which is what actually connects.
As for why you get an exception: probably because http://www.google.com and 1=1 is not a valid URL.

How to parse serialized json from php web service in java play framework 1.2.x

I need to use some from from a php web service which rendering its data by serializing json in java play framework 1.2.x. What i am doing just using play WS function. and i am getting data from that service. But when I try to get it with JSONObject it throws excepiton which is so normal, because the returned data does not look a json format well. Any body who knows any workarounds or solution would be appreciated.
HttpResponse htp = WS.url("http://www.geoplugin.net/php.gp?ip=78.171.90.49").get();
System.out.println(htp.getContentType()+"\n"+htp.getStatusText()+"\n"+htp.getString());
The returned data :
a:18:{s:17:"geoplugin_request";s:12:"78.171.90.49";s:16:"geoplugin_status";i:200;s:16:"geoplugin_credit";s:145:"Some of the returned data includes GeoLite data created by MaxMind, available from <a href=\'http://www.maxmind.com\'>http://www.maxmind.com</a>.";s:14:"geoplugin_city";s:8:"Istanbul";s:16:"geoplugin_region";s:8:"Istanbul";s:18:"geoplugin_areaCode";s:1:"0";s:17:"geoplugin_dmaCode";s:1:"0";s:21:"geoplugin_countryCode";s:2:"TR";s:21:"geoplugin_countryName";s:6:"Turkey";s:23:"geoplugin_continentCode";s:2:"EU";s:18:"geoplugin_latitude";s:7:"41.0186";s:19:"geoplugin_longitude";s:9:"28.964701";s:20:"geoplugin_regionCode";s:2:"34";s:20:"geoplugin_regionName";s:8:"Istanbul";s:22:"geoplugin_currencyCode";s:3:"TRY";s:24:"geoplugin_currencySymbol";s:15:"YTL";s:29:"geoplugin_currencySymbol_UTF8";s:3:"YTL";s:27:"geoplugin_currencyConverter";s:6:"2.2669";}
You are accessing the PHP endpoint. You need to hit this URL instead:
http://www.geoplugin.net/json.gp?ip=78.171.90.49

Retrieving data from URL fails

I have been trying different ways to get data from the following link:
http://www.ensembl.org/Danio_rerio/Export/Output/Location?db=core;flank3_display=300;flank5_display=300;output=fasta;r=18:19408965-19409049;strand=feature;coding=yes;cdna=yes;peptide=yes;utr3=yes;exon=yes;intron=yes;genomic=unmasked;utr5=yes;_format=Text
Copy paste the link to a web browser works for me but I cannot get to it programmatically in java.
It seems that it doesn't follow the get protocol as the separation of parameters is not as expected.
I tried to use URL but it separates the link above into server path and query and results in HTTP 500.
I tried to use sockets but again failed.
I believe that what I need is a way to simply send the complete string unaltered and then read the result.
Any ideas?
This code reads first line from that URL successfully:
URL u = new URL("http://www.ensembl.org/Danio_rerio/Export/Output/Location?db=core;flank3_display=300;flank5_display=300;output=fasta;r=18:19408965-19409049;strand=feature;coding=yes;cdna=yes;peptide=yes;utr3=yes;exon=yes;intron=yes;genomic=unmasked;utr5=yes;_format=Text");
DataInputStream ds = new DataInputStream(u.openStream());
String s = ds.readLine();
System.out.println(s);
It prints out: >18 dna:chromosome chromosome:Zv9:18:19408665:19409349:1

Categories