0
我想做一個註冊應用程序,將通過servlet將我的註冊信息保存到apache tomcat服務器上的mysql數據庫。但我不知道我做錯了什麼。以下是我的代碼。如果有人能指出我的錯誤會很好,謝謝!如何:servlet獲取android的表格信息並插入到mysql
的Android代碼:
@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();
}
}
的logcat:
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
我只是把我在網上發現的smth,並試圖改變它在這裏和那裏,不知道代碼做什麼。謝謝!我去試試吧! – thatsotong