常量类:ConstantUtil
public class ConstantUtil {// 数据库名称
public static final String DATABASE_NAME = "user_manager.db"
// 数据库版本号
public static final int DATABASE_VERSION = 1
//表名
public static final String TABLE_NAME = "user_info"
//字段名
public static final String USER_ID = "userId"
public static final String USER_NAME = "username"
public static final String USER_PASSWORD = "password"
public static final String USER_ADDRESS = "address"
}
自定义SQLiteOpenHelper:MySQLiteOpenHelper
public class MySQLiteOpenHelper extends SQLiteOpenHelper {// 定义一个SQLiteDatabase对象,对表进行相应的 *** 作
private SQLiteDatabase mDatabase
public MySQLiteOpenHelper(Context context) {
super(context, ConstantUtil.DATABASE_NAME, null,
ConstantUtil.DATABASE_VERSION)
mDatabase = getWritableDatabase()
}
/*
* 创建表
*/
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//sql语句
String sql = "create table " + ConstantUtil.TABLE_NAME + "("
+ ConstantUtil.USER_ID + " integer primary key,"
+ ConstantUtil.USER_NAME + " text not null,"
+ ConstantUtil.USER_PASSWORD + " text not null,"
+ ConstantUtil.USER_ADDRESS + " text not null)"
db.execSQL(sql)
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
onCreate(db)
}
/**
* 添加数据
* @param cv
* @return
*/
public boolean insertData(ContentValues cv){
return mDatabase.insert(ConstantUtil.TABLE_NAME, null, cv)>0
}
/**
* 查询所有数据
* @return
*/
public List<Userinfo> queryData(){
List<Userinfo> userinfos=new ArrayList<Userinfo>()
//从数据库里查询数据
Cursor cursor=mDatabase.query(ConstantUtil.TABLE_NAME, null, null, null, null, null, null)
if(cursor!=null){
//取出数据
while (cursor.moveToNext()) {
Userinfo userinfo=new Userinfo()
userinfo.setUserId(cursor.getInt(0))
userinfo.setUsername(cursor.getString(1))
userinfo.setPassword(cursor.getString(2))
userinfo.setAddress(cursor.getString(3))
userinfos.add(userinfo)
}
}
return userinfos
}
}
主Activity
public class MainActivity extends Activity {// 控件
private TextView txtName, txtPwd, txtAddress
private EditText edtName, edtPwd, edtAddress
private ListView mListView
// 数据库对象
private MySQLiteOpenHelper mySQLiteOpenHelper
private UserinfoAdapter adapter
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
findView()
init()
}
private void findView() {
// TODO Auto-generated method stub
edtName = (EditText) findViewById(R.id.id_edit1)
edtPwd = (EditText) findViewById(R.id.id_edit2)
edtAddress = (EditText) findViewById(R.id.id_edit3)
mListView = (ListView) findViewById(R.id.id_listview)
}
private void init() {
// TODO Auto-generated method stub
mySQLiteOpenHelper = new MySQLiteOpenHelper(MainActivity.this)
}
public void onAction(View v) {
switch (v.getId()) {
case R.id.id_btn_add:
//添加数据
String userName=edtName.getText().toString()
String userPwd=edtPwd.getText().toString()
String userAdress=edtAddress.getText().toString()
//传入参数
ContentValues cv=new ContentValues()
//列名和值
cv.put(ConstantUtil.USER_NAME, userName)
cv.put(ConstantUtil.USER_PASSWORD, userPwd)
cv.put(ConstantUtil.USER_ADDRESS, userAdress)
//得到结果
boolean flag=mySQLiteOpenHelper.insertData(cv)
if (flag) {
Toast.makeText(MainActivity.this, "添加记录成功", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(MainActivity.this, "添加记录失败", Toast.LENGTH_SHORT).show()
}
break
case R.id.id_btn_query:
//查询数据
List<Userinfo> userinfos=mySQLiteOpenHelper.queryData()
if (adapter!=null) {
adapter=null
}
adapter=new UserinfoAdapter(userinfos)
mListView.setAdapter(adapter)
break
default:
break
}
}
//数据适配器
class UserinfoAdapter extends BaseAdapter{
List<Userinfo> userinfos
public UserinfoAdapter(List<Userinfo> _userinfos){
this.userinfos=_userinfos
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return userinfos.size()
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return userinfos.get(position)
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null){
convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.listview_item, null)
txtName=(TextView) convertView.findViewById(R.id.id_txt_name)
txtPwd=(TextView) convertView.findViewById(R.id.id_txt_pwd)
txtAddress=(TextView) convertView.findViewById(R.id.id_txt_address)
txtName.setText(userinfos.get(position).getUsername())
txtPwd.setText(userinfos.get(position).getPassword())
txtAddress.setText(userinfos.get(position).getAddress())
}
return convertView
}
}
}
完整源码下载地址(附数据库文件查询软件+运行效果图):
分类: 教育/科学 >>学习帮助问题描述:
希望大家多多帮忙!!!!!!
解析:
1,(index 用户登陆页面)
<!-- #include file="conn" -->
<!-- blog.soowooo 悠悠长假期 -->
<>
<head>
<meta -equiv="Content-Type" content="text/charset=gb2312">
<title>会员</title>
<style type=text/css>
<!--
body,td,th {
font-family: 宋体
font-size: 14px
}
-->
</style>
</head>
<body>
<center>
<p>会员注册系统</p>
<form name=form1 method="post" action="login">
<table width=34% border=0>
<tr>
<td width=33% height=30>用户名:</td>
<td width=67% height=30><input name=username type=text id="username" size="15"></td>
</tr>
<tr>
<td height=30>密 码:</td>
<td height=30><input name=password type=password id="password" size="15"></td>
</tr>
<tr>
<td colspan="2" align=center><input type=submit name=Submit value="确定">
<input type=reset name=Submit value="重置"></td>
</tr>
<tr>
<td colspan="2"><a href="reg" target="_self">注册</a></td>
</tr>
</table>
</form>
</center>
</body>
</>
2,(login 用户数据处理文件)
<!-- #include file="conn" -->
<%
'打开数据库判断用户是否存在,info为表名,username为字段名
set rsc=server.createobject("adodb.recordset")
sqlc="select * from info where username='"&request.Form("username")&"' and password='"&request.Form("password")&"'"
rsc.open sqlc,conn,1,1
session("username")=rsc("username")
session("password")=rsc("password")
session.Timeout=30
set rsc=nothing
response.Redirect("change")
'如果用户不存在,session("username")为空
%>
3,(change 用户信息修改页面)
<!-- #include file="conn" -->
<>
<head>
<meta -equiv="Content-Type" content="text/charset=gb2312">
<title>修改</title>
<style type=text/css>
<!--
body,td,th {
font-size: 14px
}
-->
</style></head>
<center>
<body>
<br>
<%
set rsc=server.createobject("adodb.recordset")
sqlc="select * from info where username='"&session("username")&"' and password='"&session("password")&"'"
rsc.open sqlc,conn,1,1
nr=rsc("password")
username=rsc("username")
password=rsc("password")
sex=rsc("sex")
qq=rsc("qq")
mail=rsc("mail")
add=rsc("add")
personalinfo=rsc("personalinfo")
vv=rsc("ntime")
set rsc=nothing
if nr="" then
response.Redirect("index")
end if
if strp(nr,request.Form("password"))=0 then
response.Write("欢迎你!"&request.Form("username"))
response.Write("你是在"&vv&"注册的")
session("username")=request.Form("username")
end if
if session("username")="" then
response.Redirect("index")
end if
%>
<form name=form1 method="post" action="change?ac=ch">
<table width=39% height=105 border=0 >
<tr>
<td width=27% height=30>用户名:</td>
<td width=73% height=30><input name=username type=text id="username" value="<%=username%>">
*</td>
</tr>
<tr>
<td height=30>密 码:</td>
<td height=30><input name=password type=text id="password" value="<%=password%>">
*</td>
</tr>
<tr>
<td height=30>性 别:</td>
<td height=30><input name=sex type=text id="sex" value="<%=sex%>"></td>
</tr>
<tr>
<td height=30>QQ:</td>
<td height=30><input name=qq type=text id="qq" value="<%=qq%>"></td>
</tr>
<tr>
<td height=30>Mail:</td>
<td height=30><input name=mail type=text id="mail" value="<%=mail%>"></td>
</tr>
<tr>
<td height=30>地 址:</td>
<td height=30><input name=add type=text id="add" value="<%=add%>"></td>
</tr>
<tr>
<td>介绍</td>
<td><textarea name=personalinfo cols="30" rows="6" id="personalinfo"><%=personalinfo%></textarea></td>
</tr>
<tr>
<td></td>
<td><input type=submit name=Submit value="修改">
<a href="change?se=y" target="_self">退出系统</a></td>
<% if strp(request.QueryString("se"),"y")=0 then
session("username")=""
response.Redirect("index")
end if
%>
</tr>
</table>
</form>
<%
if strp(request.QueryString("ac"),"ch")=0 then
set rs=server.createobject("adodb.recordset")
sql="select * from info where username='"&session("username")&"'"
rs.open sql,conn,1,3
rs("username")=request.Form("username")
rs("password")=request.Form("password")
rs("mail")=request.Form("mail")
rs("sex")=request.Form("sex")
rs("qq")=request.Form("qq")
rs("add")=request.Form("add")
rs("personalinfo")=request.Form("personalinfo")
rs.update
set rs=nothing
response.Write("修改完成!")
end if
%>
</body>
</center>
</>
4,(reg 新用户注册页面)
<>
<head>
<meta -equiv="Content-Type" content="text/charset=gb2312">
<title>用户注册</title>
<style type=text/css>
<!--
body,td,th {
font-family: 宋体
font-size: 14px
}
-->
</style>
</head>
<body>
<center>
用户注册<br>
<%
=request.QueryString("msg")
%>
<form name=form1 method="post" action="addnewdata?ac=adduser">
<table width=39% height=105 border=0 >
<tr>
<td width=27% height=30>用户名:</td>
<td width=73% height=30><input name=username type=text id="username">
*</td>
</tr>
<tr>
<td height=30>密码:</td>
<td height=30><input name=password type=password id="password">
*</td>
</tr>
<tr>
<td height=30>确定密码:</td>
<td height=30><input name=password2 type=password id="password2">
*</td>
</tr>
<tr>
<td height=30>性别:</td>
<td height=30><input name=sex type=text id="sex"></td>
</tr>
<tr>
<td height=30>QQ:</td>
<td height=30><input name=qq type=text id="qq"></td>
</tr>
<tr>
<td height=30>Mail:</td>
<td height=30><input name=mail type=text id="mail"></td>
</tr>
<tr>
<td height=30>地址:</td>
<td height=30><input name=add type=text id="add"></td>
</tr>
<tr>
<td>个人介绍</td>
<td><textarea name=personalinfo cols="30" rows="6" id="personalinfo"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type=submit name=Submit value="提交"></td>
</tr>
</table>
</form>
</center>
</body>
</>
5,(addnewdata 新用户注册数据处理文件)
<!-- #include file="conn" -->
<>
<head>
<meta -equiv="Content-Type" content="text/charset=gb2312">
<title>成功</title>
</head>
<body>
<%
ac=request.QueryString("ac")
msg="注册错误信息"
if request.Form("username")="" then
msg=msg&"<br>"&"用户名不能为空"
end if
if strp(cstr(request.Form("password")),cstr(request.Form("password2")))<>0 then
msg=msg&"<br>"&"两次密码输入不同"
end if
if len(request.Form("password"))<6 then
msg=msg&"<br>"&"密码太简单"
end if
if strp(msg,"注册错误信息")>0 then
response.Redirect("reg?msg="&msg)
end if
if ac="adduser" then
set rsc=server.createobject("adodb.recordset")
sql="select * from info where username='"&request.Form("username")&"'"
rsc.open sql,conn,1,1
ck=rsc("username")
set rsc=nothing
if ck<>"" then
msg=msg&"<br>"&"用户名被人注册"
response.Redirect("reg?msg="&msg)
end if
dsql="select * from info where id is null"
set rs=server.createobject("adodb.recordset")
rs.open dsql,conn,1,3
rs.addnew
rs("username")=request.Form("username")
rs("password")=request.Form("password")
rs("mail")=request.Form("mail")
rs("sex")=request.Form("sex")
rs("qq")=request.Form("qq")
rs("add")=request.Form("add")
rs("personalinfo")=request.Form("personalinfo")
rs("ntime")=now
rs.update
set rs=nothing
%>
<center>
<a href="index" target="_self">注册成功,点击登陆</a>
</center>
<%
end if
%>
</body>
</>
6,(conn 数据库连接文件)
<%
'连接数据库开始
dim conn,rs,sql
on error resume next
dbpath=server.mappath("userinfo.mdb")
set conn=server.createobject("adodb.connection")
conn.open "PROVIDER=Microsoft.jet.OLEDB.4.0data source="&dbpath
'创建记录对象
set rs=server.createobject("adodb.recordset")
%>
7,(userinfo.mdb ACCESS 数据库)
在ACCESS中建一个表,然后在这个表中建立字段名称
表名:info
字段名称 数据类型
id 自动编号
username 文本
password 文本
sex 文本
quest 文本
qq 文本
mail 文本
personalinfo 文本
ntime 文本
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)