Connecting to database

Hi frieds!

I need your help.

I am trying to connect Midlet to mySQL and it works with Wireless Toolkit 2.5 . But when i am trying to do it trough my phone (Sony-Ericsson K700i - support MIDP-2.0 and CLDC-1.1) it isn't working. As if the servlet does not respond.

I can not find the problem.

Servlet:

import java.io.*;

import java.text.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

public class getConnection extends HttpServlet {

Statement statement;

ResultSet rs=null;

String bstr=null;

String bstr1=null;

String bstr2=null;

public void init() {

}

public void doPost(HttpServletRequest request,

HttpServletResponse response) throws ServletException,

IOException {

DataInputStream in = new DataInputStream(

(InputStream)request.getInputStream());

String db = in.readUTF();

String user = in.readUTF();

String pwd = in.readUTF();

String message = null;

try {

connect(db.toLowerCase().trim(),user.toLowerCase().trim(),

pwd.toLowerCase().trim());

message = "Name:"+bstr+" telephone:"+bstr1+" burthday:"+bstr2;

message += " conected";

} catch (Throwable t) {

message += " - unsuccessful - " + t.toString();

}

response.setContentType("text/plain");

response.setContentLength(message.length());

PrintWriter out = response.getWriter();

out.println(message);

in.close();

out.close();

out.flush();

}

public void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException,

IOException {

doPost(request,response);

}

public void connect(String db, String user,String pwd)

throws Exception {

Class.forName("com.mysql.jdbc.Driver");//.newInstance();

Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.10.10/"+db,user,pwd);

//bstr += "start ";

try{

statement = conn.createStatement();

rs = statement.executeQuery("SELECT * FROM people WHERE id=1");

} catch (SQLException e) {

System.err.println(e);

//bstr += e.toString();

}

try{

while (rs.next()) {

bstr=rs.getString(2);

bstr1=rs.getString(3);

bstr2=rs.getString(4);

}

statement.close();

} catch (SQLException e) {

//bstr += e.toString();

System.err.println(e);

System.exit(1);

}

}

}

Midlet:

import java.io.*;

import java.util.*;

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.io.*;

public class testMySQL extends MIDlet implements CommandListener {

private String username;

private String url = "http://valnas.no-ip.org:9090/ConnectMidletDB/db";

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;

public testMySQL() throws Exception {

display = Display.getDisplay(this);

}

public void startApp() {

displayMenu();

}

public void displayMenu() {

menu = new Form("Connect");

tb = new TextField("Please input database: ","",30,

TextField.ANY );

tb1 = new TextField("Please input username: ","",30,

TextField.ANY);

tb2 = new TextField("Please input password: ","",30,

TextField.PASSWORD);

menu.append(tb);

menu.append(tb1);

menu.append(tb2);

menu.addCommand(exit);

menu.addCommand(connect);

menu.setCommandListener(this);

display.setCurrent(menu);

}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

public void commandAction(Command command, Displayable screen) {

if (command == exit) {

destroyApp(false);

notifyDestroyed();

} else if (command == connect) {

db = new DB(this);

db.start();

db.connectDb(tb.getString(),tb1.getString(),tb2.getString());

}

}

public class DB implements Runnable {

testMySQL midlet;

private Display display;

String db;

String user;

String pwd;

public DB(testMySQL 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.1");

c.setRequestProperty("Content-Language","en-US");

c.setRequestMethod(HttpConnection.POST);

DataOutputStream os =

(DataOutputStream)c.openDataOutputStream();

os.writeUTF(db.trim());

os.writeUTF(user.trim());

os.writeUTF(pwd.trim());

os.flush();

os.close();

// Get the response from the servlet page.

DataInputStream is =(DataInputStream)c.openDataInputStream();

//is = c.openInputStream();

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());

}

}

/* This method takes input from user like db,user and pwd and pass

to servlet */

public void connectDb(String db,String user,String pwd) {

this.db = db;

this.user = user;

this.pwd = pwd;

}

/* Display Error On screen*/

private void showAlert(String err) {

Alert a = new Alert("");

a.setString(err);

a.setTimeout(Alert.FOREVER);

display.setCurrent(a);

}

};

}

Thanking you in advance!!!

Message was edited by:

valmur

[6167 byte] By [valmura] at [2007-11-27 11:19:57]
# 1

Did you try connecting to a servlet with a simple 'hello world' midlet?

I had the same issue as you and it turned out that although my midlet could speak to the servlet on wtk025 From my phone it simply wouldn't connect. Maybe try to get simple text from the servlet before trying a db connection.

(I still haven't solved my own issue but maybe this is a starting point )

Aharona at 2007-7-29 14:39:42 > top of Java-index,Java Mobility Forums,Java ME Technologies...