Soap web services on Tomcat to insert data to a database

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 :)
Mat SmithAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
What do you mean by "does not work"? Do you get any error messages?
Mat SmithAuthor 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.
girionisCommented:
You do not need to do anything else. Are you sure your method is called?
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Mat SmithAuthor 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 ?
girionisCommented:
Are you writing Android code? Which line has the error?
Mat SmithAuthor 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".
Mat SmithAuthor 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 ?
girionisCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Services

From novice to tech pro — start learning today.