I have bought the core j2me sun series book and have adapted some code however the code does not seem to work. The error I get is
unreported exception java.io.IOException; must be caught or declared to be thrown
postInformation();
What does this mean?
Is this to do with the way i have written the URL "http://localhost/8000/midp/loginserv";
The servlet and database are both hosted on the same machine. i have compiled and seployed the servlet. I will appreciate any help. Thank you.
MIDLET CODE
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public void commandAction (Command c, Displayable s) {
if (c== cmdSelect){
postInformation();
}else if (c == cmdBack){
midlet.displayMainForm ();
}
}
private void postInformation() throws IOException {
HttpConnection http = null;
OutputStream oStrm = null;
InputStream iStrm = null;
boolean ret = false;
String url = "http://localhost/8000/midp/loginserv";
try {
http = (HttpConnection) Connector.open (url);
oStrm = http.openOutputStream ();
http.setRequestMethod (HttpConnection.POST);
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//http.setRequestProperty("Connection", "close");
byte data [] = ("username=" + userName.getString()).getBytes();
oStrm.write (data);
data = ("&password=" + passWord.getString()).getBytes();
oStrm.write(data);
oStrm.flush();
iStrm = http.openInputStream();
ret = processServerResponse(http, iStrm);
}
finally
{
if (iStrm != null)
iStrm.close();
if (oStrm != null)
oStrm.close();
if (http != null)
http.close ();
}if (ret == false)
System.out.println("There is something wrong");
}
private boolean processServerResponse (HttpConnection http, InputStream iStrm) throws IOException {
errorMsg = null;
if (http.getResponseCode()==HttpConnection.HTTP_OK)
{
int length = (int) http.getLength ();
String str;
if (length != -1)
{
byte servletData[] = new byte[length];
iStrm.read (servletData);
str = new String(servletData);
}
else
{
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int ch;
while ((ch= iStrm.read()) != -1)
bStrm.write(ch);
str = new String (bStrm.toByteArray());
bStrm.close();
}
return true;
}
else
errorMsg = new String (http.getResponseMessage());
return false;
}
}
SERVLET CODE
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class LoginServlet extends HttpServlet
{
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String username = req.getParameter("username"),
password = req.getParameter("password");
String balance = accountLookup(username, password);
if (balance == null)
{
res.sendError(res.SC_BAD_REQUEST, "Unable to locate account.");
return;
}
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.print(balance);
out.close();
}
/*--------------------------------------------------
* Lookup bank account balance in database
*-------------------------------------------------*/
private String accountLookup(String username, String password)
{
Connection con = null;
Statement st = null;
StringBuffer msgb = new StringBuffer("");
try
{
// These will vary depending on your server/database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc
dbc:testdb");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select members from testdb where username = " +username + "and password = '" + password + "'");
if (rs.next())
return rs.getString(1);
else
return null;
}
catch (Exception e)
{
return e.toString();
}
}
}