This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java Micro Edition and the fly likes Access Dbase with Http connection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Reply Bookmark "Access Dbase with Http connection" Watch "Access Dbase with Http connection" New topic
Author

Access Dbase with Http connection

Marika Ludmann
Greenhorn

Joined: Jan 04, 2003
Posts: 1
I am new to J2ME and have come across a problem regarding Http connection. I simply want to pass a parameter from a Palm to the server, this parameter is then used to query the database and the information is sent back to the Palm top.
I managed to pass the parameter to a JSP page, which I then changed and passed back to the Palm top. However when trying to use this parameter to query the database, the results are not sent back to the Palm top. Even though when I execute the JSP with the parameter attached to the end of the URL it works fine? I'll attach the code. If anyone could help I would appreciate it.
JSP:
name = (request.getParameter("name"));
name2 = name + "2";
type = name;
java.util.Date today = new java.util.Date();
out.println("Got: "+name);
out.println("Name2: "+name2);
out.println("Date&time: "+today);
String connectionURL = "jdbc:mysql://localhost:3306/CCS?user=marikaludmann;password=ludmann";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "", "");
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM VALVE_TYPE WHERE TYPE = '" + name + "'");
while (rs.next()) {
type = rs.getString("type");
out.println("type: "+type);
id = rs.getInt("valve_type_id");
out.println("ID: "+id);
}
rs.close();
%>
MIDlet:
public class InvokeJSPMidlet extends MIDlet implements CommandListener {
Display display = null;

TextField name = null;
Form form;
String url = "http://127.0.0.1:8080/ccs_midp/today.jsp";
static final Command callCommand = new Command("date?", Command.OK, 2);
static final Command clearCommand = new Command("clear", Command.STOP, 2);
String myname;

public InvokeJSPMidlet() {
display = Display.getDisplay(this);
name = new TextField("Name:", " ", 25, TextField.ANY);
form = new Form("Invoke JSP");
}
public void startApp() throws MIDletStateChangeException {
form.append(name);
form.addCommand(clearCommand);
form.addCommand(callCommand);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}

void invokeJSP(String url) throws IOException {
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
StringBuffer b = new StringBuffer();
TextBox t = null;
try {
c = (HttpConnection)Connector.open(url);
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("IF-Modified-Since", "25 Nov 2001 15:17:19 GMT");
c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-CA");
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

os = c.openOutputStream();
os.write(("name="+myname).getBytes());
// os.flush();
is = c.openDataInputStream();
int ch;
while ((ch = is.read()) != -1) {
b.append((char) ch);
System.out.print((char)ch);
}
t = new TextBox("Date", b.toString(), 1024, 0);
t.setCommandListener(this);
} finally {
if(is!= null) {
is.close();
}
if(os != null) {
os.close();
}
if(c != null) {
c.close();
}
}
display.setCurrent(t);
}
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
if(label.equals("clear")) {
destroyApp(true);
} else if (label.equals("date?")) {
myname = name.getString();
try {
invokeJSP(url);
}catch(IOException e) {}
}
}
}
Liam Quinn
Ranch Hand

Joined: Jan 18, 2002
Posts: 35
You need to URL-encode the name. See
http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
On the servlet, you need to escape apostrophes and backslashes before putting the name in your SQL. Precede each apostrophe or backslash with a backslash to escape it. If you don't do this, you have a SQL injection security hole.
You may also have better luck calling os.close() before you open the input stream in your MIDlet. Otherwise, the Nokia 7650 won't send your request properly.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Access Dbase with Http connection
 
Similar Threads
sending mail using servlet
HOW TO COMMUNICATE JAVA APPLICATION TO SERVLETS
Deadlock Problem - Please Help
the problem abt communication between serlvet & midlet
servlet -> midlet null problem