这是代码
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. 求解答