1

我使用 java 方法尝试通过创建表 tm7(name varchar(20)) 来实现将 servlet 写入数据库;但代码不知何故没有通过tomcat插入数据库。任何专家都可以保存这个??谢谢!!(对不起,这有点新)

my tomcat writeservlet file

public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    BufferedReader reader = new BufferedReader(new InputStreamReader(
            req.getInputStream()));
    String names;

    while ((names = reader.readLine()) != null) // read from mobile device
    {
        Statement stmt;
        String sgd = names;
        String name = sgd.toString();

        try {
            stmt = con.createStatement();
            stmt.executeUpdate("INSERT INTO tm7 VALUES ('" + name
                    + "') ");
        } catch (Exception e) {
        }

        System.out.println(name);
    }

    PrintWriter out = resp.getWriter();
    out.println(" server received");
}

}

我的主要 android java

 private class NetworkTask2 extends AsyncTask<String, Void, String>{
    String line="";
    protected String doInBackground(String... params) {
        String address=params[0];
        try{
        URL url = new URL(address);
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        con.setRequestMethod("POST");
        OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
        out.write(one.getText().toString());//get converted value
        //out.write(two.getText().toString());//get converted value
        out.flush();
        out.close();

        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
        line=br.readLine();// read back response from server
        }
    catch(IOException e){Log.e("error", "mm", e); }

        return line;
    }
    protected void onPostExecute(String result) {
        one.setText(result);
        //two.setText(result);
   }
}
4

1 回答 1

1

我想建议检测问题是在服务器端还是客户端。两种情况都很容易:

从Android中的调试器开始,看看是否被调用。从调试器服务器端应用程序开始,并检查是否在doPost方法中调用了什么,它流向何处。

修复错误并完成。

      try {
        stmt = con.createStatement();
        stmt.executeUpdate("INSERT INTO tm7 VALUES ('" + name
                + "') ");

     // add this lines:
     con.flush(); 
     con.close();

    } catch (Exception e) {           
        e.printStacktace() // at least
    }
于 2013-07-15T14:11:10.357 回答