1. 1新建Android的数据库和表。添加user表信息
2. 运行AndroidStudio,编写安卓访问数据库程序。
a) 导入volly jar包,建MainActivityxml文件布局如下
b) 在MainActivity里写人代码,利用get发送请求
package com.cll.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
public class MainActivity extendsAppCompatActivity {
Button btn1;
EditText et1;
RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 =(Button)findViewById(R.id.btn1);
et1 =(EditText)findViewById(R.id.et1);
requestQueue = Volley.newRequestQueue(this);
btn1.setOnClickListener(newView.OnClickListener() {
@Override
public void onClick(View view) {
StringRequeststringRequest =new StringRequest("http://www.baidu.com",new Response.Listener
@Override
public void onResponse(String s) {
et1.setText(s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
et1.setText("出错了");
}
});
requestQueue.add(stringRequest);
}
});
}
}
c) 运行效果
3. 编写Servlet和Java访问数据库程序
a) 导入MySQL数据库连接驱动和放到tamcat/bin目录下
b) 分别建Dbhelper、Dbcon类和LoginServlet
c) 类的代码如下
i. Dbhelperpackagemodel;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import constant.Dbcon;
public class DbHelper {
Connectionconn = null;
publicConnection Getconnect() {
Connection conn = null;
try{
Class.forName(Dbcon.driver);
conn= (Connection)DriverManager.getConnection(Dbcon.url,Dbcon.user,Dbcon.password);
}catch (ClassNotFoundException e) {
//TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Sorry,can`tfind the Driver!");
}catch(SQLException e) {
//TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Sorry,can`tDatabase");
}
finally{
System.out.println("数据库数据成功获取!!");
}
// //1.getConnection()方法,连接MySQL数据库!!
if(!conn.isClosed())
System.out.println("Succeededconnecting to the Database!");
returnconn;
}
/* publicvoid Result(){
conn= Getconnect();
try{
java.sql.Statement statement = conn.createStatement();
Stringsql = "select * from user";
java.sql.ResultSetrs = statement.executeQuery(sql);
Stringname = null;
Stringpwd = null;
while(rs.next()){
name= rs.getString("name");
pwd= rs.getString("pwd");
name= new String(name.getBytes("ISO-8859-1"),"gb2312");
System.out.println(pwd+ "\t" + name);
}
rs.close();
conn.close();
}catch (Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
publicString Result1(){
conn= Getconnect();
Stringnames ="";
Stringpwds ="";
try{
java.sql.Statement statement = conn.createStatement();
Stringsql = "select * from user";
java.sql.ResultSetrs = statement.executeQuery(sql);
Stringname[] = null;
Stringpwd[] = null;
inti=0;
while(rs.next()){
i++;
name[i]= rs.getString("name");
pwd[i]= rs.getString("pwd");
name[i]= new String(name[i].getBytes("ISO-8859-1"),"gb2312");
System.out.println(pwd+ "\t" + name);
}
rs.close();
conn.close();
for(i=0;i
if(name[i]!=null&&pwd[i]!=null){
names+= name[i];
pwds+=pwd[i];
}
}
}catch (Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
returnnames+pwds;
}*/
public static void main(String[] args) {
//newDbHelper().Result();
}
}
ii. Dbcon
package constant;
import com.mysql.jdbc.Connection;
public class Dbcon {
//publicfinal static Connection con;
publicfinal static String driver = "com.mysql.jdbc.Driver";
publicfinal static String url = "jdbc:mysql://127.0.0.1:3306/android";
publicfinal static String user = "root";
publicfinal static String password = "root";
}
iii. LoginServlet package controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.Connection;
import model.DbHelper;
/**
*Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extendsHttpServlet {
privatestatic final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequestrequest, HttpServletResponse response)
*/
protectedvoid doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
//TODO Auto-generated method stub
DbHelperdbhelp = new DbHelper();
Connectioncon = dbhelp.Getconnect();
if(con!=null)
response.getWriter().append("Servedat: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequestrequest, HttpServletResponse response)
*/
protectedvoid doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
//TODO Auto-generated method stub
doGet(request,response);
}
}
4修改安卓post访问地址"10.0.2.2:8080/First/LoginServlet"
5运行结果