Hi Everybody,
I'm very new to J2ME, and I'm writing a sample code to retrieve the values from the database and display it on my mobile screen. I'm able to extract the data from the DB in an emulator. But when I try in the mobile I'm neither getting any error nor am I able to create in to the database. I have tried various mobiles which supports MIDP 2.0, but was not successful.
This is my midlet code:
package hello;
import java.io.*;
import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
public class HelloMIDlet extends MIDlet implements CommandListener {
private StreamConnection streamConnection = null;
private InputStream inputStream = null;
private String url =
"http://192.168.39.19:8084/ServletforTest/getConnection";
private Display display;
private Command exit = new Command("EXIT", Command.EXIT, 1);;
private Command connect = new Command("Connect", Command.SCREEN, 1);
private TextField tb;
private Form menu;
private TextField tb1;
private TextField tb2;
DB db;
private Command exitCommand;
private Form form;
private StringItem stringItem;
public HelloMIDlet() {
display = Display.getDisplay(this);
}
private void initialize() {
}
public void startMIDlet() {
// write pre-action user code here
switchDisplayable(null, getForm());
// write post-action user code here
}
public void resumeMIDlet() {
}
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
}
public void commandAction(Command command, Displayable displayable) {
if (displayable == form) {
if (command == exitCommand) {
exitMIDlet();
}
}
if (command == exit) {
destroyApp(false);
notifyDestroyed();
} else if (command == connect) {
db = new DB(this);
db.start();
}
}
public Command getExitCommand() {
if (exitCommand == null) {
exitCommand = new Command("Exit", Command.EXIT, 0);
}
return exitCommand;
}
public Form getForm() {
if (form == null) {
form = new Form("Welcome", new Item[] { getStringItem() });
form.addCommand(getExitCommand());
form.setCommandListener(this);
}
return form;
}
public StringItem getStringItem() {
if (stringItem == null) {
stringItem = new StringItem("Hello", "Hello, World!");
}
return stringItem;
}
public Display getDisplay () {
return Display.getDisplay(this);
}
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
public void startApp() {
displayMenu();
}
public void displayMenu() {
menu = new Form("Connect");
menu.addCommand(exit);
menu.addCommand(connect);
menu.setCommandListener(this);
display.setCurrent(menu);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public class DB implements Runnable {
HelloMIDlet midlet;
private Display display;
String db;
String user;
String pwd;
public DB( HelloMIDlet midlet) {
this.midlet = midlet;
display = Display.getDisplay(midlet);
}
public void start() {
Thread t = new Thread(this);
t.start();
}
public void run() {
StringBuffer sb = new StringBuffer();
try {
HttpConnection c = (HttpConnection)Connector.open(url);
c.setRequestProperty("User-Agent","Profile/MIDP-2.0, Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language","en-US");
c.setRequestMethod(HttpConnection.POST);
DataOutputStream os = (DataOutputStream)c.openDataOutputStream();
os.flush();
os.close();
InputStream is =c.openInputStream();
int len = (int)c.getLength();
int ch;
sb = new StringBuffer();
while ((ch = is.read()) != -1) {
sb.append((char)ch);
}
showAlert(sb.toString());
is.close();
c.close();
} catch (Exception e) {
showAlert(e.getMessage());
}
}
private void showAlert(String err) {
Alert a = new Alert("");
a.setString(err);
a.setTimeout(Alert.FOREVER);
display.setCurrent(a);
}
};
}
This is my servlet code:
package servlet;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import java.sql.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class getConnection extends HttpServlet {
private Connection conn;
public void init() {
}
// @Override
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
doPost(request,response);
}
//@Override
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
PrintWriter out = response.getWriter();
System.out.println("This is inside ");
DataInputStream in = new DataInputStream(
(InputStream)request.getInputStream());
String db ="system";
String user ="Stridus_demo";
String pwd = "Stridus_demo";
Statement stmt=null;
ResultSet rs=null;
String ur = "";
String message ="jdbcracle:thin:@122.166.39.27:1521:"+db+","+user+","+pwd;
try {
connect(db.toLowerCase().trim(),user.toLowerCase().trim(),
pwd.toLowerCase().trim());
stmt= conn.createStatement();
rs=stmt.executeQuery("select name from MOBILETESTGARUDA");
while(rs.next()){
ur = rs.getString(1)+" & " + ur;
}
response.setContentType("text/html");
response.setContentLength(ur.length());
out.println(ur);
} catch (Throwable t) {
message += "200 " + t.toString();
}
finally
{
try {
out.close();
out.flush();
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void connect(String db, String user,String pwd) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = DriverManager.getConnection("jdbcracle:thin:@122.166.39.27:1521:"+db,user,pwd);
}
}
As i have said this is working absolutely fine in the emulator, but not in the actual device.
Is there a problem in the code itself or I'm not having a proper mobile to execute this code or is there any particular settings that has to be changed in mobile to execute this particular code.
Thanks and Regards
Garudadwajan.R

racle:thin:@122.166.39.27:1521:"+db+","+user+","+pwd;
Reply With Quote



