android +tomcat 数据库,如何在TomCat中为android应用程序创建mysql数据库?

这是Android中使用MySQL数据库的示例登录应用程序。从Android设备/仿真器连接到MySQL数据库我正在通过JSON处理应用程序向Servlet发送HTTP请求。所以这里有两个不同的项目。 查询创建MySQL数据库

CREATE TABLE `users` (

`id` int(10) unsigned NOT NULL auto_increment,

`uname` varchar(45) NOT NULL,

`password` varchar(45) NOT NULL,

PRIMARY KEY (`id`)

);

INSERT INTO `users` (`id`,`uname`,`password`) VALUES

(1,'admin','123');

注: 必须需要以下两个罐放置到项目的类路径。 1. JSON-简单1.1.1.jar 2. MySQL的连接器的Java-5.xxx-bin.jar LoginServlet.java

import dbConnection.DBConnectionHandler;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.sql.*;

import java.util.Enumeration;

import org.json.simple.JSONObject;

public class LoginServlet extends HttpServlet {

//

/**

* Handles the HTTP GET method.

* @param request servlet request

* @param response servlet response

* @throws ServletException if a servlet-specific error occurs

* @throws IOException if an I/O error occurs

*/

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//response.setContentType("text/html;charset=UTF-8");

JSONObject json = new JSONObject();

//ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());

Enumeration paramNames = request.getParameterNames();

String params[] = new String[2];

int i = 0;

while (paramNames.hasMoreElements()) {

String paramName = (String) paramNames.nextElement();

//System.out.println(paramName);

String[] paramValues = request.getParameterValues(paramName);

params[i] = paramValues[0];

//System.out.println(params[i]);

i++;

}

String sql = "SELECT uname, password FROM users where uname=? and password=?";

Connection con = DBConnectionHandler.getConnection();

try {

PreparedStatement ps = con.prepareStatement(sql);

ps.setString(1, params[0]);

ps.setString(2, params[1]);

ResultSet rs = ps.executeQuery();

if (rs.next()) {

json.put("info", "success");

} else {

json.put("info", "fail");

}

} catch (Exception e) {

e.printStackTrace();

}

//System.out.println(json);

response.setContentType("application/json");

response.setCharacterEncoding("UTF-8");

response.getWriter().write(json.toString());

}

/**

* Handles the HTTP POST method.

* @param request servlet request

* @param response servlet response

* @throws ServletException if a servlet-specific error occurs

* @throws IOException if an I/O error occurs

*/

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

DBConnectionHandler.java

public class DBConnectionHandler {

Connection con = null;

public static Connection getConnection() {

Connection con = null;

try {

Class.forName("com.mysql.jdbc.Driver");//Mysql Connection

} catch (ClassNotFoundException ex) {

Logger.getLogger(DBConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);

}

try {

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname", "root", "pass");//mysql database

} catch (SQLException ex) {

Logger.getLogger(DBConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);

}

return con;

}

}

activity_main .XML

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

android:padding="10dip" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginBottom="10dip"

android:text="LOGIN"

android:textSize="25dip"

android:textStyle="bold" />

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="User Name" />

android:id="@+id/txtUser"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="15dip"

android:text="Password" />

android:id="@+id/txtPass"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:password="true" />

android:id="@+id/button1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="20dip"

android:text="Login" />

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值