Hi everyone, I need help in my school project which requires me to send data from a mobile application to a database table. The inputs for the table data is through textfield and when i press the submit button, it will send the input fields into the database table. Will be appreciated if anyone is there to help me.. thanks

Here are my current codes, I am only able to retreive data from the table but unable to send over.

Client

package web;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.HttpConnection;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;

public class EnrollmentClient extends MIDlet implements CommandListener {

    Display display;
    private boolean commandAvailable;
    CommandThread commandThread;
    Form registerForm;
    Form inputForm;
    Form outputForm;
    TextField idNum;
    TextField name;
    TextField nirc;
    TextField contact;
    TextField email;
    TextField l1r4;
    StringItem appResults;
    Command cmdBack;
    Command cmdQuit;
    Command cmdSend;
    Command cmdSelect;
    Command cmdSubmit;
    private static final String send =
            "jdbc:mysql://localhost:3306/student_info?"
            + "user=root&password=password";
    private static final String retrieve =
            "http://localhost:8084/EnrollmentServlet/EnrollmentServlet"
            + "?nirc=";
    List mainMenu;
    String[] options = {"Apply for Courses", "Check Application Status", "Exit"};

    public EnrollmentClient() {

        mainMenu = new List("Main Menu", Choice.IMPLICIT, options, null);
        mainMenu.setCommandListener(this);
        cmdSelect = new Command("Select", Command.SCREEN, 1);
        mainMenu.addCommand(cmdSelect);


        display = Display.getDisplay(this);

        registerForm = new Form("Registration Form");
        name = new TextField("Name:", null, 25, TextField.ANY);
        nirc = new TextField("NIRC:\n", null, 9, TextField.ANY);
        contact = new TextField("Contact:\n", null, 12, TextField.PHONENUMBER);
        email = new TextField("Email:\n", null, 20, TextField.EMAILADDR);
        l1r4 = new TextField("L1R4:\n", null, 9, TextField.NUMERIC);



        registerForm.append(name);
        registerForm.append(nirc);
        registerForm.append(contact);
        registerForm.append(email);
        registerForm.append(l1r4);

        cmdSubmit = new Command("Submit", Command.SCREEN, 1);
        registerForm.addCommand(cmdSubmit);
        registerForm.setCommandListener(this);


        inputForm = new Form("Check Application Status");
        idNum = new TextField("NIRC Number: ", null, 9, TextField.ANY);
        inputForm.append(idNum);
        cmdSend = new Command("Send", Command.SCREEN, 1);
        cmdQuit = new Command("Quit", Command.EXIT, 1);
        inputForm.addCommand(cmdSend);
        inputForm.addCommand(cmdQuit);
        inputForm.setCommandListener(this);

        outputForm = new Form("Application Status");
        appResults = new StringItem(null, null);
        outputForm.append(appResults);
        cmdBack = new Command("Back", Command.BACK, 1);
        outputForm.addCommand(cmdBack);
        outputForm.addCommand(cmdQuit);
        outputForm.setCommandListener(this);

        commandAvailable = false;
        commandThread = new CommandThread(this);
        commandThread.start();

    }

    public void startApp() {

        display.setCurrent(mainMenu);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {

        switch (mainMenu.getSelectedIndex()) {
            case 0:
//Go work on
                display.setCurrent(registerForm);
                break;

            case 1:
                display.setCurrent(inputForm);

                break;

            case 2:
                destroyApp(false);
                notifyDestroyed();
                break;

        }
        if (c == cmdQuit) {
            display.setCurrent(mainMenu);
        } else if (c == cmdSend) {
            synchronized (this) {
                commandAvailable = true;
                notify();
            }
        } else if (c == cmdSubmit) {
        }
        /*
        if (c == cmdExit) {
        destroyApp(false);
        notifyDestroyed();
        } else if (c == cmdBack) {
        idNum.setString(null);
        display.setCurrent(inputForm);
        appResults.setText(null);
        } else if (c == cmdOK) {
        synchronized (this) {
        commandAvailable = true;
        notify();
        }
        }
         */
    }

    class CommandThread extends Thread {

        MIDlet parent;
        boolean exit = false;

        public CommandThread(MIDlet parent) {
            this.parent = parent;
        }

        public void run() {
            while (true) {
                synchronized (parent) {
                    while (!commandAvailable) {
                        try {
                            parent.wait();
                        } catch (InterruptedException e) {
                        }
                    }
                    commandAvailable = false;
                }

                getAppStatus();
            }
        }

        public void getAppStatus() {
            HttpConnection conn = null;
            InputStream is = null;
            OutputStream os = null;
            byte[] receivedData = null;
            try {
                StringBuffer sb = new StringBuffer(retrieve);
                sb.append(idNum.getString());
                System.out.println("String sent: " + sb.toString());
                conn =
                        (HttpConnection) Connector.open(sb.toString());
                conn.setRequestMethod(HttpConnection.GET);
                conn.setRequestProperty("User-Agent",
                        "Profile/MIDP-1.0 Configuration/CLDC-1.0");
                conn.setRequestProperty("Content-type",
                        "application/x-www-form-urlencoded");
                is = conn.openInputStream();
                String contentType = conn.getType();
                int len = (int) conn.getLength();
                if (len > 0) {
                    receivedData = new byte[len];
                    int nb = is.read(receivedData);
                } else {
                    receivedData = new byte[1024];
                    int ch;
                    len = 0;
                    while ((ch = is.read()) != -1) {
                        receivedData[len++] = (byte) ch;
                    }
                }
                appResults.setText(new String(receivedData, 0, len));
                display.setCurrent(outputForm);
            } catch (IOException e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            } finally {
                try {
                    if (is != null) {
                        is.close();
                    }

                    if (os != null) {
                        os.close();
                    }

                    if (conn != null) {
                        conn.close();
                    }
                } catch (IOException e) {
                }
            }
        }
    }
}

Servlet

mport java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.*;

public class EnrollmentServlet extends HttpServlet {

    static final String dbURL = "jdbc:mysql://localhost:3306/student_info?"
            + "user=root&password=password";

    public void doSend(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        Connection conn = null;

        response.setContentType("text/plain");
        PrintWriter outResponse = response.getWriter();

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new ServletException("Unable to load JDBC driver");
        }

        try {



            String userInputname = request.getParameter("full_name");
            String userInputNIRC = request.getParameter("nirc");
            String userInputcontact = request.getParameter("contact");
            String userInputemail = request.getParameter("email");
            String userInputl1r4 = request.getParameter("l1r4");

            conn = DriverManager.getConnection(dbURL);
            PreparedStatement ps = conn.prepareStatement("INSERT INTO info VALUES (?,?,?,?,?)");
          
            ps.setString(2, userInputname);
            ps.setString(3, userInputNIRC);
            ps.setString(4, userInputcontact);
            ps.setString(5, userInputemail);
            ps.setString(6, userInputl1r4);
     
            ps.execute();

            conn.close();
          
        } catch (SQLException e) {
            throw new ServletException("SQL call failed");
        } catch (Exception e) {
            throw new ServletException(e.getMessage());
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    throw new ServletException("connection close failed");
                }
            }
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        Connection conn = null;

        response.setContentType("text/plain");
        PrintWriter outResponse = response.getWriter();

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new ServletException("Unable to load JDBC driver");
        }

        try {

            String NIRC = request.getParameter("nirc");
            System.out.println("NIRC = " + NIRC);
            conn = DriverManager.getConnection(dbURL);

            Statement stmt = conn.createStatement();

            String query = "SELECT * "
                    + "FROM info " + "WHERE nirc = '"
                    + NIRC + "'";



            ResultSet rs = stmt.executeQuery(query);
            if (rs.next()) {
                outResponse.println(rs.getString(2));
                outResponse.println(rs.getString(3));
                outResponse.println(rs.getString(7));

            } else {
                outResponse.println("Record Not Found");
            }
            conn.close();
        } catch (SQLException e) {
            throw new ServletException("SQL call failed");
        } catch (Exception e) {
            throw new ServletException(e.getMessage());
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    throw new ServletException("connection close failed");
                }
            }
        }
    }
}

Thank you

Any help here? Need help urgently thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.