1 Replies - 2348 Views - Last Post: 28 June 2012 - 02:44 AM

#1 KimSwagga  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-June 12

I want to display an image on my midlet from the database(Mysql)

Posted 27 June 2012 - 12:45 AM

this is my thread...

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Image;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Chaarles
*/
public class KampalaConct implements Runnable{

Sectors midlet;
Display display;
String url = "http://localhost:8080/Uganda/Kampala";
private String[] splitArray,content;
byte[] imageData;

public KampalaConct(Sectors midlet) {
this.midlet=midlet;
display=Display.getDisplay(midlet);
}

public void run() {
StringBuffer sb = new StringBuffer();
try {
HttpConnection conn = (HttpConnection) Connector.open(url,Connector.READ_WRITE);
conn.setRequestProperty("User-Agent", "Profile/MIDP-2.0, Configuration/CLDC-1.0");
conn.setRequestProperty("Content-Language", "en-US");
conn.setRequestMethod(HttpConnection.POST);
DataOutputStream os = (DataOutputStream) conn.openDataOutputStream();
os.writeUTF("blobAsBytes");
os.writeUTF("");
os.flush();
os.close();
// Get the response from the servlet page.
DataInputStream is = (DataInputStream) conn.openDataInputStream();
int ch;
sb = new StringBuffer();
while ((ch = is.read()) != -1) {
sb.append((char) ch);
}
sb.toString();
content = split(sb.toString(),":");
String image = content[0];
String info = content[1];
//imageData = new byte[]{Byte.parseByte(image)};
midlet.getKampShow();
midlet.kampShow.append(sb.toString(), null/*createImg(imageData)*/);
midlet.switchDisplayable(null, midlet.kampShow);
is.close();
conn.close();
} catch (IOException e) {
midlet.showAlert(e.getMessage());
midlet.switchDisplayable(null, midlet.alert);
}
}

public void start() {
Thread t=new Thread(this);
t.start();
}


public void connectDB(String label) {
midlet.switchDisplayable(null, midlet.kampShow);
}

public String [] split(String splitStr, String delimiter) {

StringBuffer token = new StringBuffer();
Vector tokens = new Vector();

// split
char[] chars = splitStr.toCharArray();
for (int i=0; i < chars.length; i++) {
if (delimiter.indexOf(chars[i]) != -1) {
// we bumbed into a delimiter
if (token.length() > 0) {
tokens.addElement(token.toString());
token.setLength(0);
}
}
else {
token.append(chars[i]);
}
}
// don't forget the "tail"...
if (token.length() > 0) {
tokens.addElement(token.toString());
}

// convert the vector into an array
splitArray = new String[tokens.size()];
for (int i=0; i < splitArray.length; i++) {
splitArray[i] = (String) tokens.elementAt(i);
}
return splitArray;

}

/*private Image createImg(byte[] B){

Image img = null;
try{
img = Image.createImage(b, 0, b.length-1);
}
catch(Exception e){
midlet.showAlert(e.getMessage());
midlet.switchDisplayable(null, midlet.alert);
}
return img;
}*/
public Image loadImage(String url)throws IOException{
HttpConnection hpc =null;
DataInputStream dis=null;
try{
hpc =(HttpConnection) Connector.open(url);
int length = (int) hpc.getLength();
byte[] data= new byte[length];
dis=new DataInputStream(hpc.openDataInputStream());
dis.readFully(data);
return Image.createImage(data,0,data.length);
}finally{
if(hpc!=null)
hpc.close();
if(dis!=null)
dis.close();
}
}
}
this is my servlet..................
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


import com.mysql.jdbc.Blob;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
*
* @author Chaarles
*/
@WebServlet(name = "Kampala", urlPatterns = {"/Kampala"})
public class Kampala extends HttpServlet {
// field delimiter

private static final String FLD_DELIM = ":";
// record delimiter
private static final String REC_DELIM = "*";
private byte[] bytes;

@Override
public void init() {
}

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/plain;charset=UTF-8");
PrintWriter out = response.getWriter();
DataInputStream in = new DataInputStream((InputStream) request.getInputStream());
String img = in.readUTF();

StringBuilder result = new StringBuilder();
String sql = null;
Statement st;
Connection conn = null;
try {
/* connecting to the MySQL database and retreiveing the results*/
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn =
(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/independence", "root", "");
st = (Statement) conn.createStatement();

//sql = "Select "+img+".Image,"+img+".Information FROM "+img+";";
sql= "Select Image,Information FROM Kampala WHERE ImgId='kla001'";
ResultSet rs = st.executeQuery(sql);
int resultRows = 0;
while (rs.next()) {
System.out.println("Cant get here");
//Image image=(Image) rs.getObject(1);
//resultRows++;
Blob blob = (Blob) rs.getBlob("Image");
int blobLength = (int) blob.length();
byte[] blobAsBytes=blob.getBytes(1, blobLength);
blob.free();
result.append(blobAsBytes)
.append(FLD_DELIM).append(rs.getString(2))
.append(FLD_DELIM).append(REC_DELIM);

}

if (resultRows == 0) {
result.append("no result from the database available \n try another entry");
}
} catch (Exception e) {
result.append("connection to database failed ").append(e.toString());
//e.printStackTrace();
}
response.setContentLength(result.length());
out.println(result.toString());
in.close();
out.close();
out.flush();
}

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: I want to display an image on my midlet from the database(Mysql)

#2 mi14chal  Icon User is offline

  • D.I.C Head

Reputation: 81
  • View blog
  • Posts: 202
  • Joined: 11-December 10

Re: I want to display an image on my midlet from the database(Mysql)

Posted 28 June 2012 - 02:44 AM

And could you describe your problem? Are you getting any error(s)?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1