Creating a web service that connects to the database

Creating a web service that connects to the database

View Answers

November 9, 2009 at 3:54 PM

Hi,

Please tell me the table structure and other details. I will provide you the example tomorrow.

Regards
Deepak Kumar

November 9, 2009 at 7:00 PM

Thank You Deepak for your quick response.

Like I mentioned, I have an access database. This particular operation is only affecting the customers table which has the following fields:

Username (PK)
Firstname
Lastname
Location (FK)
Phonenumber
Physical address
CreditCardNumber
DrivingLicenceNumber

When registering online, they supply these details through a webservice. Let me know if you need further details. Again, am very grateful for you help!

Kind Regards,

November 10, 2009 at 10:24 PM

HI Deepak, my sincere apologies for sounding so desperate maybe I am and you not the one to blame.

Just want to find out if by any chance you had time to look into my problem.

Than you once again for your help.

Kind Regards,

Mwamba.

November 11, 2009 at 1:03 AM

Hi Deepak,

U though we could start from somewhere so I followed one of the tutorials I found on this awesome site. I followed it quite literally and went through all the processes and procedures. However I was an unable to insert the record I had created.

I have appended the results I got.



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package pack1;

import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.annotation.Resource;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.sql.DataSource;

/**
*
* @author Ba Mwamba
*/
@WebService()
public class customer {
@Resource(name = "customer")
private DataSource customer;

/**
* Web service operation
*/
@WebMethod(operationName = "Create")
public String Create(@WebParam(name = "username")
String username, @WebParam(name = "drivinglicence")
String drivinglicence, @WebParam(name = "creditcard")
String creditcard, @WebParam(name = "firstname")
String firstname, @WebParam(name = "lastname")
String lastname, @WebParam(name = "physicaladdress")
String physicaladdress, @WebParam(name = "phonenumber")
String phonenumber, @WebParam(name = "password")
String password, @WebParam(name = "location")
String location, @WebParam(name = "Registrationdate")
String Registrationdate) {
//TODO write your implementation code here:
String status = "Record not inserted";
try
{
Connection con= customer.getConnection();
PreparedStatement ps=con.prepareStatement("INSERT INTO Customer VALUES(?,?,?,?,?,?,?,?,?,?)");
ps.setString(1, username);
ps.setString(2, drivinglicence);
ps.setString(3, creditcard);
ps.setString(4, firstname);
ps.setString(5, lastname);
ps.setString(6, physicaladdress);
ps.setString(7, phonenumber);
ps.setString(8, password);
ps.setString(9, location);
ps.setString(10, Registrationdate);
int i = ps.executeUpdate();
if(i!=0)
{
status="Record Inserted";
}
}catch(Exception e){
System.out.println("Error in storing data"+e);
}
return status;
}

}




Results from the above operation:





create Method invocation


Method parameter(s)

Type Value
java.lang.String mwamba
java.lang.String 123456789
java.lang.String 98765443
java.lang.String mwamba
java.lang.String chishimba
java.lang.String 48/50A Makeni
java.lang.String 098765433
java.lang.String mwamba
java.lang.String L002
java.lang.String 10/11/2009
Method returned

java.lang.String : "Record not inserted"
SOAP Request

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">;
<S:Header/>
<S:Body>
<ns2:Create xmlns:ns2="http://pack1/">;
<username>mwamba</username>
<drivinglicence>123456789</drivinglicence>
<creditcard>98765443</creditcard>
<firstname>mwamba</firstname>
<lastname>chishimba</lastname>
<physicaladdress>48/50A Makeni</physicaladdress>
<phonenumber>098765433</phonenumber>
<password>mwamba</password>
<location>L002</location>
<Registrationdate>10/11/2009</Registrationdate>
</ns2:Create>
</S:Body>
</S:Envelope>
SOAP Response

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">;
<S:Body>
<ns2:CreateResponse xmlns:ns2="http://pack1/">;
<return>Record not inserted</return>
</ns2:CreateResponse>
</S:Body>
</S:Envelope>


Where I am going wrong?

Kind Regards,

November 11, 2009 at 5:05 PM

Hi,

Please check the complete tutorial at http://roseindia.net/webservices/web-services-database.shtml

Let me know if you face any problem.

Regards
Deepak Kumar

November 11, 2009 at 7:13 PM

Thank you Deepak am sure this will be very helpful.

Just a quick question. I already have a connection to my database through a DAO class which is controlling the system interaction with the database and it is working just fine. Do I still need to create the connection in the webservice class as you are suggesting.
here is the code from my dao class:

import java.sql.*;
public class DAO
{
Connection con=null;
Statement st=null;
ResultSet rs=null;
public DAO()
{

try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url= new String("jdbc:odbc:CarHire");
con=DriverManager.getConnection(url,"","");
st=con.createStatement();

}
catch(Exception e)
{
System.out.print("Error:" + e);
}
}

with this dao in place all am doing is call the dao from my my servlets and pass appropriate parameters to it and it does what i have defined in the dao class.

I have tried to call the dao in the webservice class but gives an error owing to the fact that it's in another package.

is it possible for me to use this connection i have already defined or do i need to create one specifically for the webservice?

Kind Regards,

Mwamba.


November 12, 2009 at 2:23 AM

Hi Deepak,

I want to sincerely thank you for your kindness and patience you shuold towards me!! Trust me, I can't thank you enough for what you have done for me.

So the code did manage to work but only after I changed from the preparedstatement to just statement and it worked perfectly well.

May God continue to prosper you in all what you do and I hope we can be "programming friends :)".

thank you one again!!

with kind regards,

Mwamba Chishimba.

September 17, 2013 at 1:09 AM

Hi All,

This is an interesting post which is more useful for me as well. I need to show the results on UI by calling the service which interacts with the DB. The results are Name, ID, Address, Contact Number etc. Someone please provide me the same Axis2 service.

Best Regards Varma









Related Tutorials/Questions & Answers:
Creating a web service that connects to the database - WebSevices
Creating a web service that connects to the database  Hello, Good... DataSource customer; /** * Web service operation... be achieved especially the creation of accounts and then saving to the database
web service & database
web service & database  Hi, I need help with importing data from web service to ms sql/my sql. I developed a web service using Axis2 (v1.6.2) and Tomcat (v7.0.27) running on Eclipse Indigo. I can connect to the web services
Advertisements
Creating a service - JSP-Servlet
Creating a service  I created a database for username and password nd...) to get the data back from the database.(Service is a seperate class not like... page.Then I created a DAO class which would access the database and return
Creating Database - SQL
ciao.co.uk. we are creating the database in mysql. 1> category->subcategory->...Creating Database  Hi I am Guru I am having the confusion in creating the database.Actually Just I joined one small company. I am
creating windows service - RMI
creating windows service  Hi, I have created a project using spring RMI and have some server.jar file which contians all the classes of server... service for this server so that it can start/stop at any time. please help me
creating web services - WebSevices
stopped publish WindowADS_TO_REPLACE_11 II. Steps for Creating Web Service...creating web services  Actually i am new for web services so Plz tell... we will create a simple web service and client application using eclipse
creating web page for form registration
creating web page for form registration  I am creating web page for form registration to my department ..I have to Reprint the Application Form (i.e Download the PDf File from the Database ) , when the user gives the Application
Creating Database Tables and Indexes
Creating Database Tables and Indexes   ... the allowable syntax for identifiers in MySQL. The identifiers are Database, table, index...; Database 64 table 64 column 64 Index 
Create a Web Service in Java using Apache Axis2 and Eclipse with Oracle database driven
Create a Web Service in Java using Apache Axis2 and Eclipse with Oracle... a requirement of connecting an external system using web service developed which uses eclipse ID and Apache axis 2 services. The web service should be based on bottom-up
web service
web service  i want to make an web service example so please help me out thanks  
creating a friendly url for web application
creating a friendly url for web application  Hi all, I don't know how to create a friendly url for a web application. Please help me to resolve this problem. Thanks, Suresh
web service
would be grateful :):) So i want to creat a web service that display the bus... coordinate will be send to my web service in a array list but i cant translate this into a code; my problem is in the web service side : in wich class i will do
Web Service
Web Service  Dear Sir, My code is :- For webService:- import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService..."; ServiceFactory factory = ServiceFactory.newInstance(); Service service
Web Service
Web Service  Dear Sir, My code is :- For webService:- import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService..."; ServiceFactory factory = ServiceFactory.newInstance(); Service service
Web service
Web service  webservices in JSP
web service
web service  How servlet class create in web services or how html code used in webservice. pls give example which contain servlet code
web service
web service  How servlet class create in web services or how html code used in webservice. pls give example which contain servlet code
Web Service Tutorial - WebSevices
the attachment of a step by step procedure of creating web service. Thanks and Regards...Web Service Tutorial  hi, i am asking you again for my previous question of web service. you told me about a link but there is no link in your
Web Service Tutorial - WebSevices
the steps of creating a simple web service. Thanks and Regards Nishi...Web Service Tutorial  hi, i am new to web service and not much have idea about web services. right now i am using myeclipse blue 7.1 as my java IDE
xcode web service example
xcode web service example  xcode web service example
JSP Database Example
This example shows you how to develop JSP that connects to the database and retrieves the data from database. The retrieved data is displayed on the browser. Read Example JSP Database Example Thanks
connect a web page to a database
connect a web page to a database  how to connect a web page to a database
Creating a database
Creating database At first create a database in MySql database named studentadmissionprocess then create a table stud_admission as Use the SQL code to create table stud_admission stud_admission CREATE TABLE stud_admission
Web service protocol stack
Web service protocol stack  hii, What is the Web service protocol stack?   hello,ADS_TO_REPLACE_1 The Web service protocol stack is an evolving set of protocols used to define, discover, and implement Web services
While creating a jar how to add MySQL database to project
While creating a jar how to add MySQL database to project  Hi, Please tell me how to attach MySQL database to the Java project while building a jar or their is any other process
Web Service - WebSevices
Web Service  Hi All I have web service in .net , and i have to call it from java spring. How can i do it? Thanks
web service frameworks
Web service Frameworks         Web service Frameworks: There are many frameworks for web services. Here is the list of some frameworks
web service protocols
Web service Protocols         Web service protocols: Here is the list of some protocols used for Web Services:ADS_TO_REPLACE_1
web service client
web service client  i have implement web service client in netbeans and it work will using wsdl but know i want to Develop a webserivce (or a Java RMI server/client) for one of the e-services but i do not know the wsdl link how
web service - JSP-Servlet
web service  How to call a web service from JSP post method?  Method in JSPHi! In this JSP Example you'll see, how to call web service from JSP methodName the file as usingMethod.jsp JSP Code- - - - -<%@ page
web service problem - WebSevices
web service problem  I have created web service client and server for File transfering using axis2/java.I want to add JAX-RPC client Handler...; Thank u....Actually web service without client handler is working. I want
Spring web service - Spring
Spring web service   Respected service, I have found an error in this manner(Connecting to tomcat then coming this error at first time only) and then working correctly. error coming only at first time. What
How to Convert SOAP Web service to REST Web service In Java ?
How to Convert SOAP Web service to REST Web service In Java ?  I want to Convert SOAP Web service(WSDL) to REST web service in JSON Format with GET... service. What I know is that JAX-RS api is used to generate REST Web service
Creating a Web Application with the JSF Framework
Creating a Web Application with the JSF Framework          This example illustrates how to add JSF supporting file while creating a new web application
ModuleNotFoundError: No module named 'sql-database-service'
ModuleNotFoundError: No module named 'sql-database-service'  Hi...: No module named 'sql-database-service' How to remove the ModuleNotFoundError: No module named 'sql-database-service' error? Thanks   Hi
Java - Web Service
Java - Web Service code that allow the user to input file name with path  Hi, I wanted a simple java program for web services development as per below: ------ IN REQUEST ------ <FileName> FileName - Path
Creating Database using B+Tree in Java - Java Beginners
Creating Database using B+Tree in Java  I'm doing a project in which I have to create an Object Oriented Database using B+Tree in Java..It is not a Database connectivity..I have to create a seperate Database(like Oeacle)my-self
Fedex web service
Fedex web service  sir how can use fedex api
developing simple web service
Open Source web services tool in java   ...: The Web Services Invocation Framework (WSIF) is a simple Java API for invoking Web services, no matter how or where the services are provided
Java - Web Service
Java - Web Service  Hi, I wanted a simple java program for web services development as per below: ------ IN REQUEST ------ <FileName> FileName - Path of the file (For example: C/text.txt) ------ IN RESPONSE
Java - Web Service
Java - Web Service  Hi, I wanted a simple java program for web services development as per below: ------ IN REQUEST ------ <FileName> FileName - Path of the file (For example: C/text.txt) ------ IN RESPONSE
web service call in jsp page
web service call in jsp page  I am wandering on internet for hours... a web service when you have a WSDL. I am using tomcat web server and eclipse ,a simple example to demostrate the web service calling would be good for me... I
Creating a Database in MySQL
Creating a Database in MySQL       After establishing the connection with MySQL database by using...;   Above code is used for creating a new database. It takes a database
Security in Web Service
Security in Web Service   ... Develop a web service program for Square   area calculation. Make this web service secured using security mechanism of “Username
How sql Queries possible in DAO without creating Database connections - Java Beginners
How sql Queries possible in DAO without creating Database connections  In DAO we are writting sql queries , how it is possible without creating and closing database connections
MySQL Creating and Deleting Database
MySQL Creating and Deleting Database   ... creating a database firstly check the currently existing database in the server... database for which you don't have the privilege for.  Creating And Selecting
Creating a Database Table
Creating a Database Table      ... the connection with database and creating a table with some fields. If table name.... ADS_TO_REPLACE_3 JDBC Video Tutorial: Creating database table from Java
get images from web service
get images from web service  How to get images from web services in iPhone application?   to get images from web service xml? For Uploading NSString *encodedString=[self encode:(const uint8_t*)_secretData.bytes
Large File reading through Axis2 Web service
. This is regarding the web service help which i am looking for. I am currently working on axis2 web service and i am very new to this. so need your help... have to write a web service which would read the XML or JSON(format) data which
Attachement in Web Service + EJB 3 + Jboss
Attachement in Web Service + EJB 3 + Jboss  How to send attachements in Web Service using EJB3 with JBoss Application Server

Ads