网络菜鸟0924 2016-07-11 07:17 采纳率: 100%
浏览 3117
已采纳

Android直接连接mysql,并将数据用listview显示出来,代码一直有问题,求大神解决

这是代码
package com.example.wcms;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import com.example.girdviewtext.R;
public class MainActivity extends Activity {

private Connection con;
private Statement stat;
private ResultSet rs;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btn=(Button)findViewById(R.id.button1);  
    btn.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {
            sqlCon();
            sqlList();
        }

        private void sqlList() {
            // TODO 自动生成的方法存根
            ListView lv=(ListView)findViewById(R.id.lsit);
            List<Map<String,Object>> listItems= new ArrayList<Map<String,Object>>();
            try {
                while(rs.next())
                {
                    //System.out.println(rs.getString(1) + "\t" + rs.getString(2) + "\t" + rs.getString(3));
                    Map<String,Object> listItem=new HashMap<String,Object>();
                    listItem.put("mac",rs.getString(1));
                    listItem.put("pcname", rs.getString(2));
                    listItem.put("username", rs.getString(3));
                    listItems.add(listItem);
                }
                rs.close();
                stat.close();
                con.close();
            } catch (SQLException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            SimpleAdapter simpleAdapter=new SimpleAdapter(MainActivity.this,listItems,
                    R.layout.list_item,
                    new String[] {"mac","pcname","username"},
                    new int[]{R.id.mac,R.id.pcname,R.id.username});
            lv.setAdapter(simpleAdapter);

        }

        private void sqlCon() {
            // TODO 自动生成的方法存根
            try{
                //加载驱动程序
                Class.forName("com.mysql.jdbc.Driver").newInstance();

            }catch (Exception ex){ //无法加载驱动,则给出错误信息
                System.out.println("无法加载驱动程序" + ex.getMessage());

                return;
            }
            String url="jdbc:mysql://120.24.59.103:3306/xhw";
            String user="root";
            String password="huwei1221";

            //建立数据库连接,获得连接对象con
            try {
                con = DriverManager.getConnection(url, user, password);
                 //建立Statement对象,即是像数据库发送SQL语句的对象
                stat=con.createStatement();
                //建立 结果集,暂时存放执行SQL语句后产生的结果集合
                rs=stat.executeQuery("select * from PCInfo");
            } catch (SQLException e1) {
                // TODO 自动生成的 catch 块
                e1.printStackTrace();
            }
        }
    }); 
}

}
这是activity.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="连接数据库" />
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="horizontal"
android:layout_below="@+id/button1">
android:id="@+id/MAC"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Mac" />
android:id="@+id/PCname"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="PCname"
/>
android:id="@+id/UserName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Username"/>

<ListView
android:id="@+id/lsit"
android:layout_below="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

/>


这是list_item.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="连接数据库" />
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="horizontal"
android:layout_below="@+id/button1">
android:id="@+id/MAC"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Mac" />
android:id="@+id/PCname"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="PCname"
/>
android:id="@+id/UserName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Username"/>

<ListView
android:id="@+id/lsit"
android:layout_below="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

/>


1. 求解答

  • 写回答

2条回答 默认 最新

  • 新根 2016-07-12 01:39
    关注

    先封装一个jdbc的工具类,然后在工作线程中去通过jdbc操作mysql,实际这种方式很不安全的,这种需求一般是没网络情况下使用的。
    最好是通过联网发送请求,解析返回结果的方式来获取mysql上的数据。

    Class.forName("com.mysql.jdbc.Driver").newInstance();加载jdbc驱动,不需要构建Driver对象

    Statement替换为子类PrepareStatement,这个采用的是预处理方式

    "select * from PCInfo"最后不要用* 替代列,这样的话,不好明确ResultSet查询返回的有哪些数据

    ps:不清楚你那不出现问题,没说清连接mysql查询返回数据有问题,还是数据显示在listview上有问题(可以通过log查看哪步骤出错)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?