gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:56:06 25 4
gpt4 key购买 nike

我正在尝试制作一个注册应用程序,它将通过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com