1. The document discusses connecting a Java ME application to a MySQL database on a server using HTTP POST. It includes code to create user interface elements, send form data to a PHP file via HTTP, and display the response.
2. It also includes code snippets for creating a record store to store data locally in Java ME applications and displaying images.
3. Finally, it discusses concepts of web servers and using streams to connect and send/receive data over HTTP.
1 of 2
More Related Content
Q
1. 1
Koneksi php:
<?
$j = $_POST["j"];
$p = $_POST["p"];
$c=
mysql_connect("localhost","root","");
mysql_select_db("mobile",$c);
$sql= "insert into `buku`(judul,
penulis) values('$j','$p')";
if($run=mysql_query($sql,$c))
{
echo "ok <br>";
echo "Judul Buku :".$j."<br>";
echo "Penulis : ".$p."<br>";
}
else
{
echo mysql_error();
}
?>
Kirim data ke server :
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class SendData extends MIDlet
implements CommandListener {
Form f1, f2;
TextField Judul,Penulis;
Command submit, back;
StringItem si;
Display tampilan;
public SendData(){
f1 = new Form("HTTP
Connection");//judul form1
Judul = new TextField
("Judul","",40,TextField.ANY);
Penulis = new TextField
("Penulis","",40,TextField.ANY);
submit = new
Command("Submit",Command.OK,1);
//new command("label",jenis, prioritas)
f1.append(Judul);//aktifkan fungsi
form input
f1.append(Penulis);
f1.addCommand(submit);
f1.setCommandListener(this);
f2 = new Form("");
back=new
Command("back",Command.BACK,1)
;
si=new StringItem("Respon
Server","",1);
f2.append(si);
f2.setCommandListener(this);
}
public void submit(){
InputStream in = null;
String
url="http://localhost/koneksi.php";
HttpConnection c=null;
OutputStream out = null;
try{
c =
(HttpConnection)Connector.open(url);
c.setRequestMethod(HttpConnection.P
OST);
c.setRequestProperty("Content-
Type","application/x-www-form-
urlencoded");
out=c.openOutputStream();
byte data[];
data=("j="+Judul.getString()).getBytes
();
out.write(data);
data=("&p="+Penulis.getString()).getB
ytes();
out.write(data);
out.flush();
in=c.openInputStream();
StringBuffer buff= new
StringBuffer();
int i;
while((i=in.read())!=-1)
{
if (i!='n')
buff.append((char)i);
else
{
si.setText(buff.toString());
buff.delete(0,buff.length());
}
}
si.setText(si.getText()+buff.toString());
tampilan.setCurrent(f2);
}
catch(IOException e){
}
}
public void kembali(){
tampilan.setCurrent(f1);
}
public void startApp() {
tampilan=Display.getDisplay(this);
tampilan.setCurrent(f1);
}
public void pauseApp() {
}
public void destroyApp(boolean
unconditional) {
}
public void
commandAction(Command c,
Displayable d) {
//throw new
UnsupportedOperationException("Not
supported yet.");
if(c.equals(submit))
{
submit();
}
else
{
if(c.equals(back))
{
kembali();
}
}
}
}
User interface n comnand listener :
Class display ;
Display Tampilan;
Tampilan.getdisplay(this)
Tampilan setCurrent(displayable next)
Form f;
F = newform (“judul form”);
Text field;
Command exit
Exit= new
command(“label”,jenisurutan)
Tampil gambar :
Class tampil gambar extend midlet(){
Form f;
Display d;
Public tampil Gambar ()
{
F = new form (“tampil gambar”)
Try{
Image gbr=new image(“h.jpg”);
f.append(gbr);
}
Catch (io exception e)
{
}
}
Web server
Test.php
Text box tb
Tb=new Textbox(“judul”,”isi”,
panjang,jenis);
Tb.addcommand
Tb.set commandListener;
StreamConection C;
C=(StreamConnection)
connector.open(url);
http connection.con = null;
data InputStream in = null;
String URL = “.....”;
Try{
Con = (http connection)
connector.open(url);
Int panjang = (int) con.getLenght();
Byte[] buff = null;
If ( panjang = -1) {
Buff= new byte[panjang] // bikin
buffer
In =new Data Input Stream(con)
In.read Fully(buff);
}
2. 2
Membuat record store:
/*
* To change this template, choose
Tools | Templates
* and open the template in the editor.
*/
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class newDatabaseRMS extends
MIDletimplements CommandListener{
private Display display;
private Command createcmd = new
Command ("buat", Command.OK,1);
private Command exitCmd = new
Command("keluar",
Command.EXIT,2);
private Form f;
private TextField t;
private RecordStore myDb;
private Ticker tck;
public newDatabaseRMS(){
}
}
/**
* @author 1945
*/
public class recordstore extends
MIDlet {
public void startApp() {
display =
Display.getDisplay(this);
f = new Form("pemrograman
RMS");
t = new TextField("nama
Record
Store",null,32,TextField.ANY);
tck = new Ticker("");
f.append(t);
f.addCommand(createCmd);
f.addCommand(exitCmd);
f.addCommandListener(this);
f.setTicker(tck);
display.setCurrent(f);
}
public void buat Db(){
try{
myDb =
RecordStore.openRecordStore(t.getStri
ng(), true);
tck.setString ("Database" +
t.getString() + "berhasil di buat");
display.setCurrent(f);
}catch (RecordStoreException e ){
tck.setString ("ada kesalahan
pembuatan database");
display.setCurrent(f);
}
}
public void keluar(){
this.destroyApp(true);
}
public void CommandAction
(Command c, Displayable d){
String[b] = c.getLabel();
if (lbl.equals("keluar")){
keluar();
}else if (lbl.equals("but")){
butDb();
}
}
public void pauseApp() {
//do Nothing
}
public void destroyApp(boolean
unconditional) {
notifyDestroyed();
}
}