Reputation: 1001
I need to create a web service in Java using Eclipse. My requirement is to connect to a database with that web service and retrieve some values. Please send me at least one example program that shows how to achieve this. I have Googled, but the results I got didn't make things clear to me.
Upvotes: 0
Views: 24840
Reputation: 812
with getting started to web services springboot is a margic. just dive in to http://start.spring.io then create a project with the dependencies (web, mysql, and jpa)
they also have sample projects that will help you get started with ease.
consider checking youtube tutorials there are alot of resources there.
Upvotes: 0
Reputation: 1388
Hi @Rohith this is anatomy Web Services for connect to database,
@WebService(serviceName = "your_service_name")
public class YourClass {
public YourClass() {
super();
}
@WebMethod(operationName = "your_operation_name")
@WebResult(name = "your_response_name")
public YourDTORes your_method_name(@WebParam(name = "your") YourDTOReq request) {
YourDTORes response = new YourDTORes();
YourClassBD bd = null; // create a connection to database
Connection conn = null;
Statement st = null;
String sql = null;
try {
bd = new YourClassBD();
conn = bd.getYourMethodConnection();
sql = "select yourField from yourTable " +
"where yourField=2000 " +
"AND yourField = stuff";
st = conn.createStatement();
if(st.execute(sql)) {
ResultSet rs = st.getResultSet();
if(rs.next()) {
System.out.println("SUCCESS INSERT: " + rs.getString(1));
return response;
} else {
response.setMessageErr("ERROR.");
return response;
}
}
} catch(Exception e) {
response.setMessageErr("Service ERROR.");
e.printStackTrace();
return response;
}
return response;
}
}
The code above is a simple example for you connected to database. If you have problem let me know. Thxs.
I hope help you. :)
Upvotes: 1
Reputation: 1555
this is the sample for accessing database from jsp
http://www.roseindia.net/jsp/connect-jsp-mysql.shtml
Upvotes: 0