This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Web Services and the fly likes how to retrive database table data with webService? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "how to retrive database table data with webService?" Watch "how to retrive database table data with webService?" New topic
Author

how to retrive database table data with webService?

sahar zare
Greenhorn

Joined: Jun 16, 2010
Posts: 3
hello
i want to retrive database table as service in java
i use Resultset,but give error it

error is:C:\Users\Documents\NetBeansProjects\QAProject1\nbproject\build-impl.xml:553: The module has not been deployed
this problem is related to the following location:
at javax.servlet.jsp.jstl.sql.Result
at private javax.servlet.jsp.jstl.sql.Result servicepac.jaxws.ShowOpenResponse._return
at servicepac.jaxws.ShowOpenResponse

}



//--------------
public class Logic {
DB db=new DB("localhost","testqa",3306,"root","123");

public Logic(){}
public Result showOpenq() {
db.connect();
String sql="SELECT * FROM questions where openq=true";
//sql=String.format(sql);
Result rs=db.ShowOpenq(sql);
db.disConnect();
return rs;
}
}
//--------------------
/---------------------

public class DB {

public String serverName;
public String dbName;
public int port;
public String userName;
public String passWord;

private Connection connection;
private Statement st;

public DB(String serverName,String dbName, int port, String userName,String passWord) {

this.serverName = serverName;
this.dbName = dbName;
this.port = port;
this.userName=userName;
this.passWord=passWord;

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}

public void connect(){
try {
String url = "jdbc:mysql://"+serverName+":"+port+"/"+dbName;
connection = DriverManager.getConnection(url,userName,passWord);
st = connection.createStatement();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}

public void disConnect(){
try {
st.close();
connection.close();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}

public Result ShowOpenq(String sql){
try {
st.executeQuery(sql);
} catch (SQLException ex) {
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
}
ResultSet rs = null;
try {
rs = st.executeQuery(sql);
} catch (SQLException ex) {
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
}
Result res= ResultSupport.toResult(rs);
try {
rs.close();
} catch (SQLException ex) {
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
}


return res;}
}
excuseme as bad english.
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
Do as follows:
- Query the database.
- From the result of the database query, extract the data of each row.
Place the data of one row into a Java bean that you have written yourself.
- Place all the instances of your Java bean containing row data into an array (NOT a list or any other data structure).
- Return the list from the web service method.

Be careful to close all result sets, connections etc related to the database.
Always remember that web services is not RMI and you do not pass serialized objects, but rather XML data containing a representation of an object, over "the wire".
Best wishes!


My free books and tutorials: http://www.slideshare.net/krizsan
sahar zare
Greenhorn

Joined: Jun 16, 2010
Posts: 3
hi thanks your answer
first excuseme as bad english
i dont understand good,but i change my code to it:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to retrive database table data with webService?