Beruflich Dokumente
Kultur Dokumente
CLDC framework, do not support JDBC APIS. To connect to databases from devices t
hat
have CLDC configuration, you can call a Servlet from MIDLets. Connection to data
base,
running the queries and other functionalities need to be implemented in the Serv
let.
Passing the parameters to servlet like username, password can be done from MIDLe
ts.
The example below takes database name, username and password from MIDlet running
on
the J2ME device and passes it to the Servlet. The status of connection is passed
to MIDlet
import
import
import
import
import
java.io.*;
java.util.*;
javax.microedition.midlet.*;
javax.microedition.lcdui.*;
javax.microedition.io.*;
}
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 DBConn(this);
db.start();
db.connectDb(tb.getString(),tb1.getString(),tb2.getString());
}
}
public class DBConn implements Runnable {
MySQLConn midlet;
private Display display;
String db;
String user;
String pwd;
public DBConn(MySQLConn 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-1.0, Con
figuration/CLDC-1.0");
c.setRequestProperty("Content-Language","en-US");
c.setRequestMethod(HttpConnection.POST);
DataOutputStream os = (DataOutputStream)c.openDataOutput
Stream();
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.openDataInputStre
am();
//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 t
o 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);
}
};
}
import
import
import
import
import
import
java.io.*;
java.text.*;
java.util.*;
javax.servlet.*;
javax.servlet.http.*;
java.sql.*;