今日学习内容:
package com.example.kj;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class PolicyDAO {
private final static String driver = "com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://192.168.43.17:3306/hbkj?useUnicode=true&characterEncoding=UTF-8";
private static String user="root";
private static String password="123456";
public static List queryByPage(int currentPage, int pageSize) {
Connection conn = getCon();
List ls = new ArrayList<>();
String sql = "SELECT * FROM policy LIMIT ?, ?";
int offset = (currentPage - 1) * pageSize;
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, offset);
ps.setInt(2, pageSize);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Policy policy = new Policy();
policy.id = rs.getString(1);
policy.category = rs.getString(4);
policy.document = rs.getString(6);
policy.form = rs.getString(7);
policy.name = rs.getString(2);
policy.organ = rs.getString(8);
policy.perdata = rs.getDate(11).toString();
policy.pubdata = rs.getDate(10).toString();
policy.range = rs.getString(5);
policy.viadata = rs.getDate(9).toString();
policy.text = rs.getString(19);
policy.type = rs.getString(3);
ls.add(policy);
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return ls;
}
public static Connection getCon(){
Connection connection=null;
try {
Class.forName(driver);
connection=DriverManager.getConnection(url,user,password);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
return connection;
}
public static List<Policy> queryAll(){
Connection conn=getCon();
List<Policy> ls=new ArrayList<>();
String sql="select * from policy";
try {
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while (rs.next()){
Policy policy=new Policy();
policy.id=rs.getString("id");
policy.category=rs.getString("category");
policy.document=rs.getString("document");
policy.form=rs.getString("form");
policy.name=rs.getString("name");
policy.organ=rs.getString("organ");
policy.perdata = rs.getDate(11).toString();
policy.pubdata = rs.getDate(10).toString();
policy.range=rs.getString("range");
policy.viadata = rs.getDate(9).toString();
policy.text=rs.getString("text");
policy.type=rs.getString("type");
ls.add(policy);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return ls;
}
public static List<Policy> searchPolicies(String policyName, String documentNumber, String publishingOrgan) {
Connection conn = getCon();
List<Policy> policies = new ArrayList<>();
String sql = "SELECT * FROM policy WHERE name LIKE ? AND document LIKE ? AND organ LIKE ?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, "%" + policyName + "%");
ps.setString(2, "%" + documentNumber + "%");
ps.setString(3, "%" + publishingOrgan + "%");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Policy policy = new Policy();
policy.name = rs.getString("name");
policy.document = rs.getString("document");
policy.organ = rs.getString("organ");
policy.pubdata = rs.getString("pubdata");
policy.category = rs.getString("category");
policies.add(policy);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return policies;
}
public static Policy getPolicyById(int policyId) {
Connection conn = getCon();
String sql = "SELECT * FROM policy WHERE id = ?";
Policy policy = null;
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, policyId);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
policy = new Policy();
policy.id = rs.getString("id");
policy.category = rs.getString("category");
policy.document = rs.getString("document");
policy.form = rs.getString("form");
policy.name = rs.getString("name");
policy.organ = rs.getString("organ");
policy.perdata = rs.getDate("perdata").toString();
policy.pubdata = rs.getDate("pubdata").toString();
policy.range = rs.getString("range");
policy.viadata = rs.getDate("viadata").toString();
policy.text = rs.getString("text");
policy.type = rs.getString("type");
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return policy;
}
}
通过调用里面的getPolicyById方法实现,点击policy_list_item中的查看详情按钮,可以展示数据库中的text内容
收起
为了实现在点击policy_list_item中的“查看详情”按钮后展示数据库中对应Policy的"text"内容,你需要在你的UI界面(可能是Android的Activity或Fragment)中处理这个逻辑。下面是一个简化的示例,说明如何在Android环境中调用PolicyDAO.getPolicyById()方法,并显示结果。
首先,确保你已经在你的项目中添加了相应的数据库驱动依赖(比如mysql-connector-java),并且已经正确设置了网络权限(如果数据库不是本地的)。
步骤1: 在UI界面中处理按钮点击事件
假设你有一个ListView或者RecyclerView来展示Policy列表项(policy_list_item布局中包含一个“查看详情”按钮)。你需要为每个item的“查看详情”按钮设置点击监听器。
示例代码(在Adapter中设置点击监听):
Java
// 在你的Adapter中
public class PolicyListAdapter extends RecyclerView.Adapter<PolicyListAdapter.PolicyViewHolder> {
// ...
List<Policy> policies; // 假设这是你的数据源
Context context;
public PolicyListAdapter(Context context, List<Policy> policies) {
this.context = context;
this.policies = policies;
}
// ...
@Override
public void onBindViewHolder(@NonNull PolicyViewHolder holder, int position) {
Policy policy = policies.get(position);
// 绑定其他数据...
// 设置详情按钮点击监听
holder.btnViewDetails.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int policyId = policy.getId(); // 假设getId()返回Policy的id
showPolicyDetails(policyId);
}
});
}
private void showPolicyDetails(int policyId) {
// 这里调用获取Policy详情的方法,并处理结果
Policy detailedPolicy = PolicyDAO.getPolicyById(policyId);
if (detailedPolicy != null) {
// 显示Policy的"text"内容,可能通过一个新的Activity、Dialog或者Toast
Toast.makeText(context, detailedPolicy.getText(), Toast.LENGTH_LONG).show();
// 或者启动一个新的Activity来详细展示
// Intent intent = new Intent(context, PolicyDetailActivity.class);
// intent.putExtra("policyText", detailedPolicy.getText());
// context.startActivity(intent);
} else {
Toast.makeText(context, "未能获取Policy详情", Toast.LENGTH_SHORT).show();
}
}
// ViewHolder等其他部分...
}
步骤2: (可选)创建PolicyDetailActivity展示详细内容
如果你选择使用一个新的Activity来展示Policy的详细内容,你需要创建一个PolicyDetailActivity,并在其布局文件中设计用于展示文本的UI元素(如TextView)。
PolicyDetailActivity.java 示例:
Java
public class PolicyDetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_policy_detail);
String policyText = getIntent().getStringExtra("policyText");
TextView textViewPolicyDetail = findViewById(R.id.textViewPolicyDetail);
textViewPolicyDetail.setText(policyText);
}
}
记得在你的AndroidManifest.xml中声明PolicyDetailActivity。
以上示例展示了基本的逻辑流程,根据你的具体需求和应用架构(如MVVM、MVP等),实现细节可能会有所不同。
标签:ps,getString,rs,Policy,5.22,policy,new From: https://www.cnblogs.com/szm123/p/18257036