开发者社区> 问答> 正文

android 通过tomcat 与mysql数据库连接报错 ?报错

想做一个无线点餐的安卓app 开桌把桌号传到数据库 但是servlet不太会 想请教为什么出错

这是activity传数据的主要代码
package com.systaresto_client;






import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;


import util.HttpUtil;
import com.systaresto_client.R;
import com.systaresto_client.R.layout;
import com.systaresto_client.R.menu;






import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


@SuppressLint("NewApi")
public class KaizhuoActivity extends Activity {
 String personNum;
 String tableID;


 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_kaizhuo);
 
 Button openTb = (Button)findViewById(R.id.openTbButton);//caidan
 openTb.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
 
 
 EditText personNumEt = (EditText) findViewById(R.id.personNumEditText02);
 personNum = personNumEt.getText().toString();
 EditText tableIDEt = (EditText) findViewById(R.id.tableNumEditText);
 tableID = tableIDEt.getText().toString(); 
  
 
 // 桌�?�
 if	( tableID.equals("1")){
 showDialog("该桌已开");
 }
 else
 {
 Intent intent = new Intent(); // 调用点菜方法
 // 桌号
 intent.setClass(KaizhuoActivity.this,JiacaiActivity.class);
 Bundle bundle = new Bundle(); 
 bundle.putString("tableID",tableID);
 bundle.putString("personNum",personNum);
 
 intent.putExtras(bundle);
 startActivity(intent); 
 
 List<BasicNameValuePair> params =new LinkedList<BasicNameValuePair>();
 params.add(new BasicNameValuePair("OrderNum", tableID));
 params.add(new BasicNameValuePair("Person",personNum));
 
 UrlEncodedFormEntity entity1=null;
 try {
 entity1 = new UrlEncodedFormEntity(params,HTTP.UTF_8);
 } catch (UnsupportedEncodingException e) {
 e.printStackTrace();
 }
 
 // 请求服务器Servlet的url
 String url = HttpUtil.BASE_URL+"servlet/KaizhuoServlet";
 // 获得HttpPost对象
 HttpPost request = HttpUtil.getHttpPost(url);
 // 为请求设置参数
 request.setEntity(entity1);
 // 获得返回结果
 String result= HttpUtil.queryStringForPost(request);
 System.out.print(result); 
 StrictMode
 .setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
 .detectDiskReads().detectDiskWrites()
 .detectNetwork() // or .detectAll() for all
 // detectable problems
 .penaltyLog().build());
 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
 .detectLeakedSqlLiteObjects() // 探测SQLite数据库操作
 .penaltyLog() // 打印logcat
 .penaltyDeath().build());
 finish();
 
 }
  
 }
 });
 
 Button mCancel = (Button)findViewById(R.id.fanhui);
 mCancel.setOnClickListener(new OnClickListener() {
 public void onClick(View v) {
 finish();//这个是关键
 }
 });
 
 Button mainMenu = (Button)findViewById(R.id.zhucaidan);
 mainMenu.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
 
 Intent i=new Intent(KaizhuoActivity.this, MainActivity.class);
 startActivity(i);
 KaizhuoActivity.this.startActivity(i);
 KaizhuoActivity.this.finish();
 }
 });
 }
 
 
 private void showDialog(String msg){
 AlertDialog.Builder builder = new AlertDialog.Builder(this);
 builder.setMessage(msg)
       .setCancelable(false)
       .setNegativeButton("取消", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
           }
       })
       .setPositiveButton("添菜", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
        	   EditText personNumEt = (EditText) findViewById(R.id.personNumEditText02);
 String personNum = personNumEt.getText().toString();
 EditText tableIDEt = (EditText) findViewById(R.id.tableNumEditText);
 String tableID = tableIDEt.getText().toString(); 
  
        	   
        	   Intent intent = new Intent(); // 调用点菜方法
 // 桌号
 intent.setClass(KaizhuoActivity.this,JiacaiActivity.class);
 Bundle bundle = new Bundle(); 
 bundle.putString("personNum",personNum);
 bundle.putString("tableID",tableID);
 intent.putExtras(bundle);
 startActivity(intent); 
 
           }
       });
 AlertDialog alert = builder.create();
 alert.show();
 }
 


 


 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.kaizhuo, menu);
 return true;
 }


 
}

 
 
 

 

 
 
 
这是开桌daoImpl package dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import dao.kaizhuoDao; import util.DBUtil; import entity.kaizhuo; public class KaizhuoDaoImpl implements kaizhuoDao { String TableId ; String PersonNum; public kaizhuo check(String OrderNum, String Person){ // 查询SQL语句 String sql1 =   " select OrderNum,Person,Montant"+ " from commandinfo7"+ " where OrderNum=? and Person=? "; // 添加SQL语句 // String sql2 = " insert into commandinfo7(OrderNum,Person)values(3,3) "; // 数据库连接工具类 DBUtil util = new DBUtil(); // 获得连接 Connection conn = util.openConnection(); try { // 获得预定义语句 PreparedStatement pstmt = conn.prepareStatement(sql1); // PreparedStatement pstmt2 = conn.prepareStatement(sql2); // 设置查询参数 pstmt.setString(1, OrderNum); pstmt.setString(2, Person); // pstmt2.setString(3, OrderNum); // pstmt2.setString(4, Person); // 执行查询 ResultSet rs = pstmt.executeQuery(); // 判断用户是否存在 if (rs.next()) { // 获得用户信息 // OrderNum = rs.getString(1); // Person = rs.getString(2); float Montant= rs.getFloat(3); // 封装用户信息 kaizhuo k = new kaizhuo(); k.setMontant(Montant); k.setOrderNum(OrderNum); k.setPerson(Person); return k; }else return null; } catch (SQLException e) { e.printStackTrace(); } finally { util.closeConn(conn); } return null; } // 保存点餐列表 public kaizhuo saveKaizhuo(kaizhuo kz) { // 添加SQL语句 String sql = " insert into commandinfo7(OrderNum,Person)values(?,?) "; // 数据库连接工具类 DBUtil util = new DBUtil(); // 获得连接 Connection conn = util.openConnection(); try { // 获得预定义语句 PreparedStatement pstmt = conn.prepareStatement(sql); // 设置参数 pstmt.setString(1, kz.getOrderNum()); pstmt.setString(2, kz.getPerson()); // 执行更新 pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { util.closeConn(conn); } return null; } public static void main(String[] args) { kaizhuoDao dao = new KaizhuoDaoImpl(); kaizhuo k = dao.check("3", "1"); System.out.println(k.getMontant()); kaizhuo kz = dao.saveKaizhuo(k); } }
这是servlet
package servlet;


import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.kaizhuoDao;
import dao.impl.KaizhuoDaoImpl;
import entity.kaizhuo;


public class KaizhuoServlet extends HttpServlet { 
 
 public KaizhuoServlet() {
    super();
    
 }
 public void destroy(){
 super.destroy();
 }
 
 private static final long serialVersionUID = 1L;
   
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


 response.setContentType("text/html");
 PrintWriter out = response.getWriter();
 // 桌号
 String OrderNum = request.getParameter("OrderNum");
 // 人数
 String Person = request.getParameter("Person");
 // 获得DAO接口
 kaizhuoDao dao = new KaizhuoDaoImpl(); 
 // 实例化订单类
 kaizhuo k = dao.check(OrderNum, Person);
 kaizhuo kz = new kaizhuo();
 kz.setOrderNum(OrderNum);
 kz.setPerson(Person);
 
 // 设置订单属性 
 k.setPerson(Person); 
 k.setOrderNum(OrderNum);


 dao.saveKaizhuo(kz);
 
 
 out.flush();
 out.close();
 
 }


 
 
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 doGet(request,response);
 }
public void init() throws ServletException{
}




}

 
 
 

 

 
 
 
 
开桌 dao
package dao;


import entity.kaizhuo;


public interface kaizhuoDao {
 // 开桌信息
 public kaizhuo check (String OrderNum ,String Person );


 public kaizhuo saveKaizhuo(kaizhuo kz);
}

 
 
 

 

 
 
 
entity开桌

package entity;


public class kaizhuo {
// 桌号
private String OrderNum;
// 人数
private String Person;
private float Montant;

public float getMontant() {
return Montant;
}
public void setMontant(float montant) {
Montant = montant;
}
public String getOrderNum() {
return OrderNum;
}
public void setOrderNum(String orderNum) {
OrderNum = orderNum;
}
public String getPerson() {
return Person;
}
public void setPerson(String person) {
Person = person;
}


}
DBUtil

package util;


import java.sql.Connection;  
import java.sql.DriverManager;  
  
import java.sql.SQLException;  
 
public class DBUtil {
/*
* 关闭数据库连接
*/
public void closeConn(Connection conn){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

/*
* 打开数据库连接
*/
public Connection openConnection() {


String url="jdbc:mysql://localhost:3306/systaresto?useUnicode=true&characterEncoding=utf-8";
String username="root";
String password="test";
try {
// prop.load(this.getClass().getClassLoader().getResourceAsStream(
// "DBConfig.properties"));
Class.forName("com.mysql.jdbc.Driver");



return DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}


return null;
}


}


展开
收起
爱吃鱼的程序员 2020-06-22 18:47:09 653 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    贴报错信息<divclass="ref">

    引用来自“铂金osc”的答案

    <divclass=ref_body>贴报错信息android.os.NetworkOnMainThreadException你确定这是android的报错信息?这是从logcat里copy出来的?找不到一个匹配的属性,貌似是web端的报错吧?android.os.NetworkOnMainThreadException
    改了一下变这个了不能在主线程里请求网络,这个找谷姐,答案超多<divclass="ref">

    引用来自“sd_sd”的答案

    <divclass=ref_body><divclass="ref">

    引用来自“铂金osc”的答案

    <divclass=ref_body>贴报错信息不入库,打断点,看是sql的问题还是android的问题搞定谢谢了一直没有提示的。。

    2020-06-22 18:47:26
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载

相关镜像