Salil Vishnu Kapur

Greenhorn
+ Follow
since Dec 21, 2014
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Salil Vishnu Kapur

Use the following code by editing your table name and database name , i guess this would fix your problem .

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

public class CountRows_MySQL {

public static Connection getConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/octopus";
String username = "root";
String password = "root";

Class.forName(driver); // load MySQL driver
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}

public static int countRows(Connection conn, String tableName) throws SQLException {
// select the number of rows in the table
Statement stmt = null;
ResultSet rs = null;
int rowCount = -1;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName);
// get the number of rows from the result set
rs.next();
rowCount = rs.getInt(1);
} finally {
rs.close();
stmt.close();
}
return rowCount;
}

public static void main(String[] args) {
Connection conn = null;
try {
conn = getConnection();
String tableName = "myTable";
System.out.println("tableName=" + tableName);
System.out.println("conn=" + conn);
System.out.println("rowCount=" + countRows(conn, tableName));
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
} finally {
// release database resources
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}


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.

9 years ago
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.

9 years ago
hey mario !
I am also stuck on the same problem , did anyone sought it out for you ?
9 years ago