首页在AndroidManifest.xml中添加访问数据库权限
在src中新建一个连接webservice的类,名字随意,这里叫做“HttpConnSoap”。基本上这个类是固定的,要改的大多数就是webservice端口地址,具体代码如下:
public class DBUtil {static boolean feeflag=false;ArrayList<String> arrayList=new ArrayList<String>();ArrayList<String> brrayList=new ArrayList<String>();ArrayList<String> crrayList=new ArrayList<String>();HttpConnSoap Soaptest=new HttpConnSoap(); public static Connection getConnection(){Connection con=null; try{System.out.println("111");Class.forName("org.gjt.mm.mysql.Driver");System.out.println("222");con=DriverManager.getConnection("jdbc:mysql://192.168.0.100:3306/test?useUnicode=true&characterEncoding=UTF-8","root","123456");System.out.println("333");}catch(Exception e){System.out.println("444");e.printStackTrace();}return con;}//查询学生信息public String[] selectStu(String StuNO){String ss[]=new String[8];String result=null;arrayList.clear();brrayList.clear();crrayList.clear();arrayList.add("StuNO");brrayList.add(StuNO);crrayList=Soaptest.GetWebServre("selectStu", arrayList, brrayList);ss[0]=crrayList.get(0);ss[1]=crrayList.get(1);ss[2]=crrayList.get(2);ss[3]=crrayList.get(3);ss[4]=crrayList.get(4);ss[5]=crrayList.get(5);ss[6]=crrayList.get(6);ss[7]=crrayList.get(7);return ss;}public List<HashMap<String, String>> selectStuAll() {List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();arrayList.clear();brrayList.clear();crrayList.clear();crrayList = Soaptest.GetWebServre("selectStuAll", arrayList, brrayList);for (int j = 0; j < crrayList.size(); j += 3) {HashMap<String, String> hashMap = new HashMap<String, String>();hashMap.put("S_Name", crrayList.get(j));hashMap.put("S_Age", crrayList.get(j + 1));hashMap.put("S_Sex", crrayList.get(j + 2));list.add(hashMap);}return list;}}
下面就是Android程序中调用了,这里用listview显示数据。
Android通过Webservice操作sqlserver数据库的相关知识,就给大家介绍这么多,后续还会给大家介绍相关知识,希望大家持续关注本站,谢谢。