我使用 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);
}
}