|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
accessing database from midlets n returning many records
hi,
Finally i solved my previous problem on my own. Now i want to know how to connect to a database and retrieve many records and display on a mobile, using j2me. bye. |
|
#2
|
|||
|
|||
|
Hi Vishalneo,
This is another topic of great interest.... Especially those who first get into J2ME... I also thought the same: query the DB right from within the MIDlet. For simplicity (again, J2ME is meant to be lightweight), you should consider creating a Servlet (assuming you're using Java on the server) that connects to the database, retrieves the records, and then passes it back to the MIDlet. So rather than having the MIDlet connect to your Oracle/MySQL/Whatever database, it calls the Servlet, which then queries the database and returns to the results to the calling client (MIDlet). Hope this helps... |
|
#3
|
|||
|
|||
|
Hi again,
Concerning the midlet problem i had earlier, resolved it. i used only 1 midlet and others are classes. My codes has become much more simpler, that's what Object 0riented progamming is I think! The problem still exists for the database issue. I have 4 functionalities in my application. Each one has specific search parameters to pass to the database. That's where i'm stuck. have you done something like this? If i succeed in solving for one of the functionality, i'll b able to solve the others in no time just like for the midlets and classes. bye |
|
#4
|
|||
|
|||
|
using classes
Hey,
Regarding using midlets as classes, does this mean you can access them within your main midlet? Please clarify. I am accessing a database for authentication, and then i want the user to view a list of other midlets to choose from. Any help would be most appreciated. Regards, Ven |
|
#5
|
|||
|
|||
|
midlet n classes
Hi,
The answer is yes. This is what OO programming is! You create only one midlet and many classes. Inside the midlet you have to instantiate the classes. Ex. if i have a class SearchEntity, inside the midlet i will have something like SearchEntity SE=new SearchEntity(); //constructor SE.anyfunction(..param); where anyfunction is a function defined in the class SearchEntity but beware the class must not have any display declarations, ie Display.getDisplay(this)... the anyfunction must return a Form , i.e public Form anyfunction(){ Form frmDisplay=new Form("my class name"); frmDisplay.append("anystring"); return this.frmDisplay; } then in your midlet where you instantiate the class and call the anyfunction you must have Form myForm; myForm=SE.anyfunction(); Display.getDisplay(this).setCurrent(myForm); as you will see, the form returned by anyfunction is 'assigned' to myForm It is here that you must call the midlet to Display the form. the class will not be able to display the form because it can't extend javax.microedition.midlet.*; Hope this helps, ask for help from morecream, i've sent him some docs bye, vishalneo |
|
#6
|
|||
|
|||
|
connecting to database
Thanks for that. Just in case you haven't solved your previoous problem, find below a snippet of code to connect to a mysql database.
HttpConnection http = null; InputStream iStrm = null; // Data is passed at the end of url for GET String url = "http://localhost:8080/Params/params" + "?" + "StudentID=" + studentID.getString() + "&" + "pin=" + pwd.getString(); try { http = (HttpConnection) Connector.open(url); http.setRequestMethod(HttpConnection.GET); if (http.getResponseCode() == HttpConnection.HTTP_OK) { try { iStrm = http.openInputStream(); } catch (Exception xxx) { System.out.println(xxx.toString()); } int length = (int) http.getLength(); if (length > 0) { byte servletData[] = new byte[length]; iStrm.read(servletData); waitForm.append("\n" + new String(servletData)); } else { fmMain.append("Invalid user name or pin"); } } And in the servlet, the code is as follows. public class ServletfromMIDlet extends HttpServlet{ public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { ResultSet rs = null; String stID = req.getParameter("StudentID"), pin = req.getParameter("pin"); if (stID == null || pin == null) { res.sendError(res.SC_BAD_REQUEST, "Unable to read parameters"); return; } Connection dbconn = null; res.setContentType("text/html"); PrintWriter out = res.getWriter(); try { String sDBDriverClass = "com.mysql.jdbc.Driver"; String sDBConnStr = "jdbc:mysql://localhost/testdb/user=root/password="; Class.forName( sDBDriverClass ); dbconn = DriverManager.getConnection( sDBConnStr ); if( dbconn == null ) { throw new Exception( "Failed to get JDBC connection from DriverManager" ); } Statement stList = dbconn.createStatement(); String sql = ""; sql = "SELECT name, surname, courseCode FROM studentDetails WHERE studentNumber = '" +stID +"' AND pin = '" +pin+"'"; ResultSet rsList = stList.executeQuery( sql ); if( rsList != null ) { while( rsList.next() ) { //depends on your table you are accessing out.println( "<b>Name</b>: " + rsList.getString( "name" ) + " - <b>Surname</b>: " + rsList.getString( "surname" ) + " -<b>Course</b>: " + rsList.getString("courseCode") + "<br>" ); } rsList.close(); } dbconn.close(); } catch( Exception e ) { System.err.println( "Exception getting DB connection: " + e.toString() ); try{ if( dbconn != null ) dbconn.close(); dbconn = null; } catch( SQLException sqle ) { System.err.println("Sql exception: " + sqle.toString()); } } } } Regards, Ven. |
|
#7
|
|||
|
|||
|
I wonder instead of using servlets, can i use a aspx pageto retrieve data from the database?And may i know roughly the codes to handle the aspx page as well as the codes to put inside the midlet?
|
|
#8
|
|||
|
|||
|
my project's complete
hi frankie,
thnks 4 z help. I've already completed my project now. i will b graduating this september. but at z same time, i've started doing an MSc to go further. by the way you know about GIS and J2ME coz i'm thinking of doing that 4 z MSc thesis. J2ME is 2 gr8. but how can u manipulate a map dynamically, i hope u know wat i mean. We can share our experience and problems to get a solution to any mobile application, wat do u think. |
|
#9
|
|||
|
|||
|
hi
any one know about Voice Recording? Anchal Quote:
|
|
#10
|
|||
|
|||
|
hi everyone...
i have some problems when connecting my midlet to database. It shows this: Warning: To avoid potential deadlock, operations that may block, such as networking, should be performed in a different thread than the commandAction() handler. please help me...thank u |
|
#11
|
|||
|
|||
|
Hi,
Can I use JDBC in my java program to access local database which is stored on PDA in DBMSs like HSQLDB or SQLIte. I m new to J2ME and developing java application to be deployed on PDA which wud access database on server as well as tht on PDA.As for the server part it appears(if i m not wrong) from the messages here that I 'll have to use midlets and servelets with JDBC. But what about local database on the PDA device.Can i use JDBC here too.If yes pls tell how.If no what r other options for this task. Kindly help!! Vishal and Frankie,expecting a response from u too. ![]() Thanks in advance. Kind regards. Bhushan. |
|
#12
|
|||
|
|||
|
Good People
I'm trying to implement the Music on demand Application. I'm trying to complete the Wireless Project for Mobile Music On Demand. The application have to Play Music, Download and save Music Files (Which I think it can work with two databases with the original files stored at one Databases and downloaded to another Databases by Copying from the first one and INSERTING to the second Database). But my problem is that I'm new to J2ME Environment though I've managed to Develop the prototype (only the GUI's). MAY YOU PLEASE ASSIST ME IF YOU KNOW HOW TO CONNECT TO DATABASE USING J2ME AND STORING AND RETRIEVING MUSIC FILES. May you pls reply with the snip of Code in Connecting to Databases Using Servlet. I am using MS Access Database but I can also try to use mysql if it can help. PLEASE ASSIST INT HIS REGARD, Thanks in advance for your flowing responses!!! |
|
#13
|
|||
|
|||
|
Quote:
Is it possible to RETRIEVE data from Databases and post (display) it back to the Servlet Text fields. For Example (For a user to modify his/her profile s/he have to view the previously entered information the s/he can further modify the information and resubmit it to update the existing data records, ). I know only how to do this with simple Java. Please assist. Thanking you in advance, Londy, |
|
#14
|
|||
|
|||
|
HI,
if u mean for j2me, then the simple trick is as follows: 1. make ur servlet retrieve all the fields from the database. 2. concatenate each fields with a special character, i use %, into a whole string. ex:name%surname%address%... 3. then send the entire string to the j2me client. 4. on the client side, de-concatenate the string into the original fields. 5. always make it return a fixed/known number of fields so that u can manipulate easily. 6. assign each field to a textfield. 7. edit the fields on the client. 8. then concatenate again all the fields into a string and send to the servlet to update the database. hope it helps, neo |
|
#15
|
|||
|
|||
|
im new to J2ME language n dont knw much abt it but i have chosen it as a tool for my final project so really needed help on urgent....
im making an application that connects to a db made in ACCESS and tells the user location of hospitals in a specific city. now wht i want to knw is all the basics of J2ME that would b needed to understand the connectivity n the language...since im new to it 2. i want to knw how the application wud connect to the internet to access the database. 3. i need to knw the way to query the database n show the user response according to his selection. plz i need help, n if some one can tell me abt the packages n class handling in J2ME, tht wud b highly appreciated. rough coding for the application is also welcomed..... from desparate programmer |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > accessing database from midlets n returning many records |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|