Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in connecting mysql database to JAX-WS  RSS feed

 
Salil Vishnu Kapur
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i want to create a jax-ws whose client will access it from android . So please guide me how to connect database to webservice

Diagramatical representation of my project

client(android) ------------------WebSevice----------------------MySql(database)

So the problem is coming in the later part of connecting webservice with the database .

when i run it , i get run time errors.

 
K. Tsang
Bartender
Posts: 3628
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what error did you get? A stack trace will help.

How are you connecting to the database in the code?
 
Salil Vishnu Kapur
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i implemented this links(http://theopentutorials.com/examples/java-ee/jax-ws/create-and-consume-web-service-using-jax-ws/) example and after that according to my requirement i added the code for connection to database as follows . But it gave run time error which also i am mentioning .


@WebService
public class calculator
{
public int add(int a, int b)
{
return (a + b);
}
public int sub(int a, int b) throws ClassNotFoundException, SQLException
{
Connection conn1=new connection().getConnection();
Statement stmt = null;
ResultSet rs = null;
int rowCount = -1;
rs = stmt.executeQuery("SELECT COUNT(*) FROM hospital_status");
// get the number of rows from the result set
rs.next();
rowCount = rs.getInt(1);

return rowCount;


}

class connection
{
public Connection getConnection() throws ClassNotFoundException, SQLException
{
Statement stmt = null;
ResultSet rs = null;
Connection conn;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.2.8/hospital_data";
String username = "root";
String password = "mysql";
int rowCount = -1;

Class.forName(driver);

conn = DriverManager.getConnection(url, username, password);



return conn;

}
}


**************************************************************
My web Service and my database are on the same PC
Run time errors(though this webService gets published but what i get as row count is -1 as declared by the variable i.e connection to the database on my pc is not made .)

[Error] A class/interface with the same name "com.open.calc.SQLException" is already in use.Use a class customization to resolve this conflict.
line 45 of http://localhost:8080/calcws/calculator?xsd=1

[ERROR] this error is caused beacuse on windows you cannot have both "SQLException.java" and "SQLException.java" in the same directory.

 
K. Tsang
Bartender
Posts: 3628
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Salil Vishnu Kapur wrote:
My web Service and my database are on the same PC
Run time errors(though this webService gets published but what i get as row count is -1 as declared by the variable i.e connection to the database on my pc is not made .)


Try localhost in the connection url. Also the getConnection() method can be static. Hence you don't need to "new <classname>".


Salil Vishnu Kapur wrote:
[Error] A class/interface with the same name "com.open.calc.SQLException" is already in use.Use a class customization to resolve this conflict.
line 45 of http://localhost:8080/calcws/calculator?xsd=1

[ERROR] this error is caused beacuse on windows you cannot have both "SQLException.java" and "SQLException.java" in the same directory.



Why aren't you using the java API's java.sql.SQLException?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!