河北省科技政策测app,连接MySQL数据库,模糊搜索
简历如下项目结构,并导入jar包,导入后才能链接Android Studio外部数据库

建立对应的policy实体类
package com.example.try4;
import android.os.Parcel;
import android.os.Parcelable;
public class Policy implements Parcelable {
private long id;
private String name;
private String type;
private String category;
private String range;
private String document;
private String form;
private String organ;
public Policy(long id, String name, String type, String category, String range, String document, String form, String organ) {
this.id = id;
this.name = name;
this.type = type;
this.category = category;
this.range = range;
this.document = document;
this.form = form;
this.organ = organ;
}
protected Policy(Parcel in) {
id = in.readLong();
name = in.readString();
type = in.readString();
category = in.readString();
range = in.readString();
document = in.readString();
form = in.readString();
organ = in.readString();
}
public static final Creator<Policy> CREATOR = new Creator<Policy>() {
@Override
public Policy createFromParcel(Parcel in) {
return new Policy(in);
}
@Override
public Policy[] newArray(int size) {
return new Policy[size];
}
};
public long getId() {
return id;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getCategory() {
return category;
}
public String getRange() {
return range;
}
public String getDocument() {
return document;
}
public String getForm() {
return form;
}
public String getOrgan() {
return organ;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
dest.writeString(name);
dest.writeString(type);
dest.writeString(category);
dest.writeString(range);
dest.writeString(document);
dest.writeString(form);
dest.writeString(organ);
}
}
下面,在DatabaseQueryTask文件夹中构建连接MySQL数据库的信息
package com.example.try4;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DatabaseQueryTask implements Runnable {
private TextView textView;
private String keyword;
private Handler handler;
public DatabaseQueryTask(TextView textView, String keyword) {
this.textView = textView;
this.keyword = keyword;
this.handler = new Handler(Looper.getMainLooper());
}
@Override
public void run() {
List<Policy> policies = new ArrayList<>();
try {
// 加载 MySQL 驱动
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, user, password);
// 构建 SQL 语句
String query = "SELECT `id`, `name`, `type`, `category`, `range`, `document`, `form`, `organ` FROM `tb_policy` WHERE `name` LIKE ?";
PreparedStatement statement = connection.prepareStatement(query);
// 设置模糊查询条件
statement.setString(1, "%" + keyword + "%");
// 执行查询
ResultSet resultSet = statement.executeQuery();
// 处理结果
while (resultSet.next()) {
long id = resultSet.getLong("id");
String name = resultSet.getString("name");
String type = resultSet.getString("type");
String category = resultSet.getString("category");
String range = resultSet.getString("range");
String document = resultSet.getString("document");
String form = resultSet.getString("form");
String organ = resultSet.getString("organ");
// 创建 Policy 对象并添加到列表中
Policy policy = new Policy(id, name, type, category, range, document, form, organ);
policies.add(policy);
}
// 关闭资源
resultSet.close();
statement.close();
connection.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
// 在主线程中更新 UI
handler.post(new Runnable() {
@Override
public void run() {
if (textView != null) {
LinearLayout linearLayout = (LinearLayout) textView.getParent();
linearLayout.removeAllViews();
for (final Policy policy : policies) {
// 创建一个 TextView 来显示每条记录的 name
TextView textViewName = new TextView(textView.getContext());
textViewName.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
textViewName.setPadding(16, 16, 16, 16);
textViewName.setText(policy.getName());
textViewName.setTextSize(16);
textViewName.setClickable(true);
textViewName.setBackgroundResource(android.R.drawable.list_selector_background);
// 设置点击事件,点击后跳转到详情页面
textViewName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(textView.getContext(), PolicyDetailActivity.class);
intent.putExtra("policy", policy);
textView.getContext().startActivity(intent);
}
});
// 添加到布局中
linearLayout.addView(textViewName);
}
}
}
});
}
}
这样数据库就算是连接上了,然后再进行事务逻辑代码的编写即可

浙公网安备 33010602011771号