android - 如何: servlet get android form info and insert into mysql

标签 android mysql servlets

我正在尝试制作一个注册应用程序,它将通过 servlet 将我的注册信息保存到 apache tomcat 服务器上的 mysql 数据库。但我不知道我做错了什么。下面是我的代码。如果有人能指出我的错误那就太好了,谢谢!

安卓代码:

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        username = etxtUname.getText().toString();
        password = etxtPword.getText().toString();

        HttpPost post = new HttpPost("http://localhost:9999/practiseserver/sayhello");

        HttpClient client = new DefaultHttpClient();

        List<NameValuePair> nvp = new ArrayList<NameValuePair>(2);
        nvp.add(new BasicNameValuePair("username", username));
        nvp.add(new BasicNameValuePair("password", password));

        try {
            post.setEntity(new UrlEncodedFormEntity(nvp));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            HttpResponse response = client.execute(post);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

servlet 代码:

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

        // Set the response MIME type of the response message
        response.setContentType("text/html");
        // Allocate a output writer to write the response message into the
        // network socket
        PrintWriter out = response.getWriter();

        // Write the response message, in an HTML page 
        //to test if servlet is working
        try {
            out.println("<html>");
            out.println("<head><title>Hello, World</title></head>");
            out.println("<body>");
            out.println("<h1>Hello, world!</h1>"); // says Hello
            // Echo client's request information
            out.println("<p>Request URI: " + request.getRequestURI() + "</p>");
            out.println("<p>Protocol: " + request.getProtocol() + "</p>");
            out.println("<p>PathInfo: " + request.getPathInfo() + "</p>");
            out.println("<p>Remote Address: " + request.getRemoteAddr()
                    + "</p>");
            // Generate a random number upon each request
            out.println("<p>A Random Number: <strong>" + Math.random()
                    + "</strong></p>");
            out.println("</body></html>");
        } finally {
            out.close(); // Always close the output writer
        }
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String un = request.getParameter("username");
        String pw = request.getParameter("password");

        Connection conn = null;
          Statement stmt = null;

          // Step 1: Allocate a database Connection object

        try {
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:8888/ebookshop", "myuser", "msql.usernp2013");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        // Step 2: Allocate a Statement object within the Connection
        try {
            stmt = conn.createStatement();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Step 3: Execute a SQL SELECT query
        String sqlStr = "insert into registertable values (" + un + "," + pw + ");";

        try {
            ResultSet rSet = stmt.executeQuery(sqlStr);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

日志猫:

12-12 15:49:29.290: D/dalvikvm(3817): Late-enabling CheckJNI
12-12 15:49:29.370: E/Trace(3817): error opening trace file: No such file or directory (2)
12-12 15:49:29.370: D/ActivityThread(3817): setTargetHeapUtilization:0.25
12-12 15:49:29.370: D/ActivityThread(3817): setTargetHeapIdealFree:8388608
12-12 15:49:29.370: D/ActivityThread(3817): setTargetHeapConcurrentStart:2097152
12-12 15:49:29.480: D/libEGL(3817): loaded /system/lib/egl/libEGL_adreno200.so
12-12 15:49:29.480: D/libEGL(3817): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
12-12 15:49:29.490: D/libEGL(3817): loaded /system/lib/egl/libGLESv2_adreno200.so
12-12 15:49:29.490: I/Adreno200-EGL(3817): <qeglDrvAPI_eglInitialize:269>: EGL 1.4 QUALCOMM build:  (Merge)
12-12 15:49:29.490: I/Adreno200-EGL(3817): Build Date: 01/23/13 Wed
12-12 15:49:29.490: I/Adreno200-EGL(3817): Local Branch: BlueJBUpgrade
12-12 15:49:29.490: I/Adreno200-EGL(3817): Remote Branch: 
12-12 15:49:29.490: I/Adreno200-EGL(3817): Local Patches: 
12-12 15:49:29.490: I/Adreno200-EGL(3817): Reconstruct Branch: 
12-12 15:49:29.520: D/OpenGLRenderer(3817): Enabling debug mode 0
12-12 15:50:43.019: E/Trace(4297): error opening trace file: No such file or directory (2)
12-12 15:50:43.019: D/ActivityThread(4297): setTargetHeapUtilization:0.25
12-12 15:50:43.019: D/ActivityThread(4297): setTargetHeapIdealFree:8388608
12-12 15:50:43.019: D/ActivityThread(4297): setTargetHeapConcurrentStart:2097152
12-12 15:50:43.139: D/libEGL(4297): loaded /system/lib/egl/libEGL_adreno200.so
12-12 15:50:43.159: D/libEGL(4297): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
12-12 15:50:43.159: D/libEGL(4297): loaded /system/lib/egl/libGLESv2_adreno200.so
12-12 15:50:43.169: I/Adreno200-EGL(4297): <qeglDrvAPI_eglInitialize:269>: EGL 1.4 QUALCOMM build:  (Merge)
12-12 15:50:43.169: I/Adreno200-EGL(4297): Build Date: 01/23/13 Wed
12-12 15:50:43.169: I/Adreno200-EGL(4297): Local Branch: BlueJBUpgrade
12-12 15:50:43.169: I/Adreno200-EGL(4297): Remote Branch: 
12-12 15:50:43.169: I/Adreno200-EGL(4297): Local Patches: 
12-12 15:50:43.169: I/Adreno200-EGL(4297): Reconstruct Branch: 
12-12 15:50:43.219: D/OpenGLRenderer(4297): Enabling debug mode 0
12-12 15:51:17.486: W/IInputConnectionWrapper(4297): getExtractedText on inactive InputConnection
12-12 15:51:17.506: W/IInputConnectionWrapper(4297): getTextBeforeCursor on inactive InputConnection
12-12 15:51:17.506: W/IInputConnectionWrapper(4297): getTextAfterCursor on inactive InputConnection
12-12 15:51:17.506: W/IInputConnectionWrapper(4297): getSelectedText on inactive InputConnection
12-12 15:51:17.566: W/IInputConnectionWrapper(4297): getExtractedText on inactive InputConnection
12-12 15:51:17.666: W/IInputConnectionWrapper(4297): getTextBeforeCursor on inactive InputConnection
12-12 15:51:17.666: W/IInputConnectionWrapper(4297): getExtractedText on inactive InputConnection
12-12 15:51:17.666: W/IInputConnectionWrapper(4297): getTextBeforeCursor on inactive InputConnection
12-12 15:51:17.666: W/IInputConnectionWrapper(4297): getTextAfterCursor on inactive InputConnection
12-12 15:51:17.666: W/IInputConnectionWrapper(4297): getSelectedText on inactive InputConnection
12-12 15:58:22.289: W/IInputConnectionWrapper(4297): showStatusIcon on inactive InputConnection
12-12 15:58:35.103: W/IInputConnectionWrapper(4654): getExtractedText on inactive InputConnection
12-12 15:58:35.123: W/IInputConnectionWrapper(4654): getTextBeforeCursor on inactive InputConnection
12-12 15:58:35.123: W/IInputConnectionWrapper(4654): getTextAfterCursor on inactive InputConnection
12-12 15:58:35.123: W/IInputConnectionWrapper(4654): getSelectedText on inactive InputConnection
12-12 15:58:38.106: D/AndroidRuntime(4654): Shutting down VM
12-12 15:58:38.106: W/dalvikvm(4654): threadid=1: thread exiting with uncaught exception (group=0x40f96450)
12-12 15:58:38.136: E/AndroidRuntime(4654): FATAL EXCEPTION: main
12-12 15:58:38.136: E/AndroidRuntime(4654): android.os.NetworkOnMainThreadException
12-12 15:58:38.136: E/AndroidRuntime(4654):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1126)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:141)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at com.thatsotong.practiseserver.MainActivity.onClick(MainActivity.java:75)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at android.view.View.performClick(View.java:4128)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at android.view.View$PerformClick.run(View.java:17142)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at android.os.Handler.handleCallback(Handler.java:615)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at android.os.Looper.loop(Looper.java:213)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at android.app.ActivityThread.main(ActivityThread.java:4787)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at java.lang.reflect.Method.invokeNative(Native Method)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at java.lang.reflect.Method.invoke(Method.java:511)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
12-12 15:58:38.136: E/AndroidRuntime(4654):     at dalvik.system.NativeStart.main(Native Method)
12-12 15:58:39.357: I/Process(4654): Sending signal. PID: 4654 SIG: 9

最佳答案

你没有说你的问题是什么?

如果数据未插入数据库。我发现的一个问题是 -

ResultSet rSet = stmt.executeQuery(sqlStr);

仅当您有 select 语句时才使用executeQuery() 方法,因为它返回结果集。

但是你有一个插入查询,所以你应该使用 -

stmt.executeUpdate()(用于 INSERT、UPDATE 或 DELETE 语句)。它会返回一个整数,代表受影响的行数。

关于android - 如何: servlet get android form info and insert into mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20538129/

相关文章:

android - 如何显示某些图像的加载进度动画?

mysql - 无法在 heroku 上使用 ClearDB 运行 Play Framework 1.2.4 应用程序

java - 当 URL 路径中有特殊字符时,将 Apache ProxyPass 或 Mod_jk 与 Tomcat 结合使用

java - 应用程序关闭时调用方法

mysql - 如何识别MySQL中的default_week_format?

java - 修复将数据从一个 servlet 传递到另一个 servlet 的问题

android - 使用 MediaPlayer 时,有时没有声音

java - 为函数或方法创建位掩码参数

android - 如何在android中捕获设备后退按钮事件?

mysql - SQL 连接、分组和连接以获取匹配数据并查看(空)