Soap web services on Tomcat to insert data to a database

Mat Smith
Mat Smith used Ask the Experts™
on
Hello,

I generated soap web services using Eclipse JEE, which then I deployed ( .war) on Tomcat 7.0 on Centos 7 -  this works fine.
The aim is to insert a data received in an xml and insert it to an Oracle table.

I've added the below code to a java class file, which meant to do some insert statement ( which is valid ) , with hardcoded values, just to prove I can do it.

    public void InsertValue(String MessageId, String value){
    	try{

    	Connection con=null;
    	Class.forName("oracle.jdbc.driver.OracleDriver");


    	con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xxx", "xxx","xxx");
    	Statement s = con.createStatement();

    	s.execute("INSERT INTO ping_message (message_id,dstamp) VALUES ('1',To_char(SYSDATE))");
    	con.setAutoCommit(true);
    	s.close();
    	con.close();
    	}catch(Exception e){
    	System.out.println("ERROR: "+e.getMessage());
    	}
    	}
    

Open in new window


Unfortunately, this does not work. I wonder if this is because of the java code or Tomcat needs some db drivers in order to establish a connection the database ?

btw, my java skills are being developed as I type this :)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2018

Commented:
What do you mean by "does not work"? Do you get any error messages?

Author

Commented:
No, I don't get any errors. It just doesn't do the insert when calling a web service.
Basically my question at the moment is if my Java code looks OK  and if I need anything special on Tomcat to be able to receive a soap request and then insert the xml data to an oracle database....or it simply should work.
Distinguished Expert 2018

Commented:
You do not need to do anything else. Are you sure your method is called?
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Author

Commented:
Hi Girionis,

You made a very good point here. I put my java sql code to a wrong java class. Now it's in the right one, however, I'm getting now a java error
"void is an invalid type for the variable InsertValue"  and it pointed me to Interface DataSource

https://docs.oracle.com/javase/8/docs/api/javax/sql/DataSource.html

I understand that I have to rewrite "my" code ?
Distinguished Expert 2018

Commented:
Are you writing Android code? Which line has the error?

Author

Commented:
no, I'm not android. It's a dynamic web app ( web services ).
The line with the error is  public void InsertValue

package com.aaaa.WebServices;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class bbbbImpl
 implements com.aaaa.WebServices.cccc{
    public com.aaaa.WebServices.SendPing_WMSResponse sendPing_WMS(com.aaa.WebServices.SendPing_WMS parameters) throws java.rmi.RemoteException {
     
        public void InsertValue(String MessageId, String value){
        	try{
        	Connection con=null;
        	Class.forName("oracle.jdbc.driver.OracleDriver");
        	con=DriverManager.getConnection("jdbc:oracle:thin:@172.16.76.100:1521:dcs", "dcsdba","dcsabd");
        	Statement s = con.createStatement();
        	//Configure the SQL query as you want, the table must exists 
        	s.execute("INSERT INTO dcsdba.ping_message (message_id,dstamp) VALUES ('1',To_char(SYSDATE))");
        	con.setAutoCommit(true);
        	s.close();
        	con.close();
        	}catch(Exception e){
        	System.out.println("ERROR: "+e.getMessage());
        	}
        	}
    	
    	return null;
    }

}

Open in new window


The problem is with the public void InsertValue it says that "void is an invalid type for the variable InsertValue".

Author

Commented:
I think my issue is released this this

https://stackoverflow.com/questions/16119650/why-oracle-driver-is-needed-in-eclipse-build-path

I'm just starting learning java and Eclipse, so all of this stuff is new to me.  
Is someone able to explain to me what I need to do with a dependency of jar files and classpath ?
Distinguished Expert 2018
Commented:
Your code is wrong, you cannot nest a method inside another method. You either need:

package com.aaaa.WebServices;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class bbbbImpl
 implements com.aaaa.WebServices.cccc{
    public com.aaaa.WebServices.SendPing_WMSResponse sendPing_WMS(com.aaa.WebServices.SendPing_WMS parameters) throws java.rmi.RemoteException {
     
        	try{
        	Connection con=null;
        	Class.forName("oracle.jdbc.driver.OracleDriver");
        	con=DriverManager.getConnection("jdbc:oracle:thin:@172.16.76.100:1521:dcs", "dcsdba","dcsabd");
        	Statement s = con.createStatement();
        	//Configure the SQL query as you want, the table must exists 
        	s.execute("INSERT INTO dcsdba.ping_message (message_id,dstamp) VALUES ('1',To_char(SYSDATE))");
        	con.setAutoCommit(false);
        	s.close();
        	con.close();
        	}catch(Exception e){
        	System.out.println("ERROR: "+e.getMessage());
        	}
    	return null;
    }

}

Open in new window


or

package com.aaaa.WebServices;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class bbbbImpl
 implements com.aaaa.WebServices.cccc{
    
        public void InsertValue(String MessageId, String value){
        	try{
        	Connection con=null;
        	Class.forName("oracle.jdbc.driver.OracleDriver");
        	con=DriverManager.getConnection("jdbc:oracle:thin:@172.16.76.100:1521:dcs", "dcsdba","dcsabd");
        	Statement s = con.createStatement();
        	//Configure the SQL query as you want, the table must exists 
        	s.execute("INSERT INTO dcsdba.ping_message (message_id,dstamp) VALUES ('1',To_char(SYSDATE))");
        	con.setAutoCommit(false);
        	s.close();
        	con.close();
        	}catch(Exception e){
        	System.out.println("ERROR: "+e.getMessage());
        	}
    }

}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial