1 Reply Latest reply: Oct 14, 2011 10:23 AM by gimbal2 RSS

    Insert data into mysql database using j2me

    893243
      I'm developing a code which can insert data into mysql using j2me my initial code is as below:

      package onlinecontactbackup;

      import java.io.DataInputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.PrintWriter;
      import java.sql.Connection;
      import java.sql.DriverManager;

      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;

      import com.mysql.jdbc.PreparedStatement;

      public class ConnectToDatabaseAndUpDateTables extends HttpServlet {
           
           public void init(){}


           public void doPost(HttpServletRequest request,HttpServletResponse response)
                     throws ServletException,IOException{
                
                DataInputStream in = new DataInputStream((InputStream)request.getInputStream());
                
                String DBData = in.readUTF();
                String user = in.readUTF();
                String pwd = in.readUTF();
                String msg ="jdbc:mysql://localhost:3306/"+DBData+","+user+","+pwd;
                
                try
                {
                     DBConnect(DBData.toLowerCase().trim(),user.toLowerCase().trim(),pwd.toLowerCase().trim());
                     msg += "100 Ok";
                     
                } catch(Throwable t){
                     msg += "200" + t.toString();
                     
                }
                
                response.setContentType("text/plain");
                response.setContentLength(msg.length());
                
                PrintWriter out = response.getWriter();
                out.println(msg);
                in.close();
                
                out.close();
                out.flush();
                
           }
           
           public void doGet(HttpServletRequest request,HttpServletResponse response)throws
           ServletException,IOException{
                
                doPost(request,response);
                
                          
           }
           
           private void DBConnect(String DBData,String user,String pass)throws Exception{
                
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/phonebookbackup" +DBData,user,pass);
                
                PreparedStatement psmt = null;
                
           
                
                
                psmt = (PreparedStatement) conn.prepareStatement("INSERT INTO 'RegistrationT'('FullNames','userName','Email','Password','VerifyPassword')"
                          +"values(?,?,?,?,?)");
                psmt.executeQuery();
                
                
           }
           

      }
      but it seem am stack on how to write the insert part of the code, can someone help on how to go about the problem
      mago..
        • 1. Re: Insert data into mysql database using j2me
          gimbal2
          user2870014 wrote:
                    psmt = (PreparedStatement) conn.prepareStatement("INSERT INTO 'RegistrationT'('FullNames','userName','Email','Password','VerifyPassword')"
                              +"values(?,?,?,?,?)");
                    psmt.executeQuery();
          but it seem am stack on how to write the insert part of the code, can someone help on how to go about the problem
          mago..
          How can you be stuck? You know which class (PreparedStatement) you want to have examples for, so ask Google. "java PreparedStatement example" and off you go.