新增数据库连接

master
FinalJcsp 2 years ago
parent 72b1162d2f
commit be7a4d2eea

@ -12,6 +12,7 @@ import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button loginB, newUser;
EditText usernameET, passwordET;
SqliteDbManager instance;
private static final String correctName = "admin";
private static final String correctPassword = "rootadmin";
@ -23,14 +24,13 @@ public class MainActivity extends AppCompatActivity {
initView();
loginB.setOnClickListener(v -> {
if (submit()) {
System.out.println(usernameET.getText());
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG).show();
Intent intent = new Intent();
ComponentName componentName = new ComponentName(MainActivity.this, InfoActivity.class);
intent.setComponent(componentName);
intent.putExtra("username", usernameET.getText());
startActivity(intent);
}else{
} else {
Toast.makeText(MainActivity.this, "账号或密码输入错误", Toast.LENGTH_LONG).show();
}
});
@ -50,10 +50,13 @@ public class MainActivity extends AppCompatActivity {
usernameET = findViewById(R.id.username);
passwordET = findViewById(R.id.password);
newUser = findViewById(R.id.newUser);
instance = SqliteDbManager.getInstance();
}
boolean submit() {
return getUsername().equals(correctName) && getPassword().equals(correctPassword);
instance.setSqliteDbOpen(this);
return instance.queryTb("person", getUsername(), getPassword());
// return getUsername().equals(correctName) && getPassword().equals(correctPassword);
}
String getUsername() {

@ -43,6 +43,8 @@ public class MainActivity2 extends Activity {
private Button btnReg, btnYear, back;
private List<String> cityList;
SqliteDbManager instance = SqliteDbManager.getInstance();
//JSONObject对象处理一个一个的对象
//JSONObject对象处理一个一个集合或者数组
//保存带集合的json字符串
@ -75,6 +77,7 @@ public class MainActivity2 extends Activity {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
intent.putExtra("username", edtNewUserName.getText().toString());
intent.putExtra("password", edtPwd1.getText().toString());
this.register();
startActivity(intent);
});
builder.setNegativeButton("否", (dialog, which) -> isYes = false);
@ -103,8 +106,13 @@ public class MainActivity2 extends Activity {
});
}
private void register(){
String username = edtNewUserName.getText().toString();
String password = edtPwd1.getText().toString();
instance.insertTb("person", username, password);
}
void initView() {
instance.setSqliteDbOpen(this);
edtNewUserName = this.findViewById(R.id.edtNewUserName);
edtPwd1 = this.findViewById(R.id.edtPwd);
edtPwd2 = this.findViewById(R.id.edtSndPwd);

@ -1,15 +1,10 @@
package com.example.myapplication;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class PeopleActivity extends Activity {
private Button ylp, qxs, lsg, tyy, djx;
@ -19,7 +14,7 @@ public class PeopleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.people);
setContentView(R.layout.test);
initView();
ylp.setOnClickListener(v -> {
imageView.setImageResource(R.drawable.ylp);
@ -50,6 +45,6 @@ public class PeopleActivity extends Activity {
tyy = findViewById(R.id.tyy);
djx = findViewById(R.id.djx);
textView = findViewById(R.id.peopletext);
imageView = findViewById(R.id.peoplezp);
imageView = findViewById(R.id.peopleImage);
}
}

@ -0,0 +1,34 @@
package com.example.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.security.AccessControlContext;
public class PersonSQLiteOpenHelper extends SQLiteOpenHelper {
/**
*
* @param context
*/
public PersonSQLiteOpenHelper(Context context) {
super(context,"person",null,1);
}
/**
*
*/
@Override
public void onCreate(SQLiteDatabase db) {
// String sql="create table person (id integer primary key autoincrement,name varchar(10) not null default '')";
String sql="create table person(id integer primary key autoincrement,username varchar(50),password varchar(50),sex varchar(50),address varchar(50))";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}

@ -0,0 +1,134 @@
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class SqliteDbManager
{
private static SqliteDbManager mInstance = null;
private SQLiteDatabase mDb = null;
private PersonSQLiteOpenHelper mDbHelper = null;
public static SqliteDbManager getInstance()
{
if (mInstance == null)
{
mInstance = new SqliteDbManager();
}
return mInstance;
}
public void setSqliteDbOpen(Context context)
{
mDbHelper = new PersonSQLiteOpenHelper(context.getApplicationContext());
mDb = mDbHelper.getWritableDatabase();
}
public void insertTb(String tbName, String username, String password)
{
openDb();
//方法一:
ContentValues contentValues = new ContentValues();
contentValues.put("username",username);
contentValues.put("password",password);
// contentValues.put("sex","男");
mDb.insert(tbName,null,contentValues);
//方法二:
// mDb.execSQL("insert into "+tbName+" (username,password) values ('李四',20)");
// mDb.execSQL("insert into "+tbName+" (name,age,sex) values ('王五',22,'女')");
// mDb.execSQL("insert into "+tbName+" (name,age,sex) values ('哈利',21,'男')");
closeDb();
}
public void deleteTb(String tbName)
{
openDb();
//方法一:
mDb.delete(tbName,"name=?",new String[]{"zhangsan"});
//方法二:
mDb.execSQL("delete from "+tbName+" where name = '李四'");
closeDb();
}
public void updateTb(String tbName)
{
openDb();
//方法一:
ContentValues contentValues = new ContentValues();
contentValues.put("name","隔壁老王");
contentValues.put("sex","男");
mDb.update(tbName,contentValues,"name=?",new String[]{"王五"});
//方法二:
mDb.execSQL("update "+tbName+" set name = '哈利波特',age = '16' where name = '哈利'");
closeDb();
}
public boolean queryTb(String tbName, String username, String password)
{
openDb();
// //方法一:
// Cursor cursor = mDb.query(tbName, new String[]{"name","sex","age"}, "name=?", new String[]{"隔壁老王"}, null, null, null);
// //将光标移动到下一行从而判断该结果集是否还有下一条数据如果有则返回true没有则返回false
// if (null != cursor)
// {
// while (cursor.moveToNext())
// {
// String name = cursor.getString(cursor.getColumnIndex("name"));
// int age = cursor.getInt(cursor.getColumnIndex("age"));
// String sex = cursor.getString(cursor.getColumnIndex("sex"));
// Log.i("????","name = "+name+"; age = "+age+"; sex = "+sex);
// }
// cursor.close();
// }
//方法二使用sql语句
Cursor rawQuery = mDb.rawQuery("select * from " + tbName+" where username=? and password=?", new String[]{username,password});
// Cursor rawQuery = mDb.rawQuery("select * from " + tbName,null);
if (null != rawQuery)
{
while (rawQuery.moveToNext())
{
@SuppressLint("Range") String _id = rawQuery.getString(rawQuery.getColumnIndex("id"));
@SuppressLint("Range") String name = rawQuery.getString(rawQuery.getColumnIndex("username"));
// int age = rawQuery.getInt(rawQuery.getColumnIndex("age"));
// String sex = rawQuery.getString(rawQuery.getColumnIndex("sex"));
Log.i("????","_id = "+_id+"; name = "+name);
return true;
}
rawQuery.close();
}
closeDb();
return false;
}
/**
*
*/
private void openDb() {
if (this.mDbHelper != null) {
try {
mDb = mDbHelper.getWritableDatabase();
} catch (Exception e) {
mDb = mDbHelper.getReadableDatabase();
e.printStackTrace();
}
}
}
/**
*
*/
private void closeDb() {
try {
if (mDb != null) {
mDb.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

@ -1,120 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="10dp">
<!-- <ImageView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="match_parent"-->
<!-- android:src="@drawable/nike"></ImageView>-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/fl_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/ylp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:text="孔子"
android:textColor="@color/black"
android:textSize="20dp"></Button>
<Button
android:id="@+id/djx"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:text="宋庆龄"
android:textColor="@color/black"
android:textSize="20dp"></Button>
<Button
android:id="@+id/tyy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:text="宋美龄"
android:textColor="@color/black"
android:textSize="20dp"></Button>
<Button
android:id="@+id/lsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:text="胡适"
android:textColor="@color/black"
android:textSize="20dp"></Button>
<Button
android:id="@+id/qxs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:text="钱学森"
android:textColor="@color/black"
android:textSize="20dp"></Button>
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="@+id/fl_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="@color/nike">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/kz"
android:layout_width="100dp"
android:layout_height="130dp"
android:src="@drawable/kz"></ImageView>
<TextView
android:id="@+id/peopletext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="孔子公元前551年-公元前479年4月11日子姓孔氏名丘字仲尼春秋时期鲁国陬邑今山东省曲阜市祖籍宋国栗邑今河南省夏邑县中国古代伟大的思想家、政治家、教育家儒家学派创始人、“大成至圣先师”。"
android:textSize="15dp"></TextView>
</LinearLayout>
</FrameLayout>
<!-- <ListView-->
<!-- android:id="@+id/lv"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content" />-->
</LinearLayout>
</LinearLayout>

@ -96,7 +96,7 @@
android:orientation="vertical">
<ImageView
android:id="@+id/peoplezp"
android:id="@+id/peopleImage"
android:layout_width="100dp"
android:layout_height="130dp"
android:src="@drawable/ylp"></ImageView>
@ -111,10 +111,5 @@
</LinearLayout>
</FrameLayout>
<!-- <ListView-->
<!-- android:id="@+id/lv"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content" />-->
</LinearLayout>
</LinearLayout>

Loading…
Cancel
Save